11. Memory Virtualization Address Space and Memory API
11. Memory Virtualization Address Space and Memory API
Operating Systems
Session 10
Memory Virtualization : Address Spaces, Memory API
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 1
Memory Virtualization
• What is memory virtualization?
• OS virtualizes its physical memory.
• OS provides an illusion memory space per each process.
• It seems to be seen like each process uses the whole memory .
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 2
Benefit of Memory Virtualization
• Ease of use in programming
• Memory efficiency in terms of times and space
• The guarantee of isolation for processes as well as OS
• Protection from errant accesses of other processes
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 3
OS in The Early System
• Load only one process in memory.
• Poor utilization and efficiency
0KB O p e r a t in g S y s t e m
(c o d e , d a ta , e tc .)
Operating System
(code, data, etc.)
64KB C u rre n t
P ro g ra m
(c o d e , d a ta , e tc .)
Current
Program
(code, data, etc.)
max
Physical Memory
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 4
Multiprogramming and Time Sharing
• Load multiple processes in memory. 0KB O p e r a t in g S y s t e m
(c o d e , d a ta , e tc .)
Operating System
• Execute one for a short while. 64KB Fre e
(code, data, etc.)
Free
• Switch processes between them in memory. 128KB
• Increase utilization and efficiency. Process C
(code, data, etc.)
192KB
Process B
(code, data, etc.)
• Cause an important protection issue. 256KB Fre e
Free
• Errant memory accesses from other processes 320KB
Process A
(code, data, etc.)
384KB Fre e
Free
448KB Fre e
Free
512KB
Physical Memory
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 5
Address Space
• OS creates an abstraction of physical memory.
• The address space contains all about a running process.
• That is consist of program code, heap, stack and etc.
0KB
P ro g ra m C o d e
Program Code
1KB Heap
Heap
2KB (fre e )
(free)
15KB
S ta c k
Stack
16KB
Address Space
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 6
Address Space(Cont.)
• Code
• Where instructions live P ro g ra m C o d e
Program Code
• Heap Heap
Heap
• Dynamically allocate memory. (fre e )
• malloc in C language
• new in object-oriented language
(free)
• Stack
• Store return addresses or values.
• Contain local variables arguments to routines. S ta c k
Stack
Address Space
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 7
Virtual Address
• Every address in a running program is virtual.
• OS translates the virtual address to physical address
#include <stdio.h>
#include <stdlib.h>
return x;
}
#include <stdio.h>
#include <stdlib.h>
return x;
}
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 8
Virtual Address(Cont.)
Address Space
• The output in 64-bit Linux machine 0x400000 Code
( Te x t )
Code
(Text)
location of code : 0x40057d
location of heap : 0xcf2010
location of stack : 0x7fff9ca45fcc
Heap
0xd13000 (fre e )
heap
(free)
stack
0x7fff9ca28000
S ta c k
Stack
0x7fff9ca49000
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 9
Memory API: malloc()
#include <stdlib.h>
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 10
sizeof()
• Routines and macros are utilized for size in malloc instead typing in a
number directly.
• Two types of results of sizeof with variables
• The actual size of ‘x’ is known at run-time.
int *x = malloc(10 * sizeof(int));
printf(“%d\n”, sizeof(x));
int x[10];
printf(“%d\n”, sizeof(x));
int x[10];
printf(“%d\n”, sizeof(x));
40
40
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 11
• When sizeof() is used with the data types such as int, float, char… etc
it simply returns the amount of memory is allocated to that data
types.
#include <stdio.h>
int main()
{
printf("%lu\n", sizeof(char));
printf("%lu\n", sizeof(int));
printf("%lu\n", sizeof(float));
printf("%lu", sizeof(double));
return 0;
}
Output:1 4 4 8
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 12
Memory API: free()
#include <stdlib.h>
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 13
Memory Allocating
2KB (fre e )
pointer
heap
(free)
stack int *pi; // local variable
*pi
16KB
Address Space
2KB a llo c a t e d
allocated
2KB + 4 a llo c a t e d
allocated
2KB + 8 a llo c a t e d
allocated
2KB + 12 a llo c a t e d
(free)
2KB
2KB *pi
16KB
Address Space
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 14
Memory Freeing
2KB fre e d
freed
2KB + 4 fre e d
freed
2KB + 8 fre e d
freed free(pi);
2KB + 12 fre e d
free(pi);
(fre e )
freed
(free)
2 K B ( in v a lid )
2KB(invalid) *pi
16KB
Address Space
2KB (fre e )
heap
(free)
stack
2 K B ( in v a lid )
2KB(invalid) *pi
16KB
Address Space
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 15
Forgetting To Allocate Memory
• Incorrect code
char *src = “hello”; //character string constant
char *dst; //unallocated
strcpy(dst, src); //segfault and die
h e llo \0
hello\0
(fre e )
heap
strcpy(dst, src); (free) unallocated
stack
*dst
* s rc
*dst
*src
Address Space
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 16
Forgetting To Allocate
Memory(Cont.)
• Correct code
char *src = “hello”; //character string constant
char *dst (char *)malloc(strlen(src) + 1 ); // allocated
strcpy(dst, src); //work properly
h e llo \0 h e llo \0
hello\0 hello\0
a llo c a t e d h e llo \0
allocated hello\0
(fre e ) (fre e )
* s rc
*dst * s rc
*dst
*src *src
Address Space Address Space
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 17
Not Allocating Enough Memory
• Incorrect code, but work properly
char *src = “hello”; //character string constant
char *dst (char *)malloc(strlen(src)); // too small
strcpy(dst, src); //work properly
h
h
e
e
strlen l
l
6 bytes
l
l
o
o
\0
\0
h e llo \0
strcpy(dst, src);
* s rc
*dst
*src
Address Space
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED
Forgetting to Initialize
• With this error, you call malloc() properly, but forget to fill in some
values into your newly-allocated data type.
• Don’t do this! If you do forget, your program will eventually
encounter an uninitialized read, where it reads from the heap some
data of unknown value.
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 20
Memory Leak
• A program runs out of memory and eventually dies.
unused
(fre e )
allocated unused
unused
(fre e )
heap heap
unused
heap a llo c a t e d
(free) allocated
(free) (fre e )
stack (free)
stack
*d
*c
*d
*b *b
*c
*a *a
*b *a
*b
*a *a *a
Address Space Address Space Address Space
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 21
Memory Leak
• Another common error is known as a memory leak, and it occurs
when you forget to free memory.
• In long-running applications or systems (such as the OS itself), this is
a huge problem, as slowly leaking memory eventually leads one to
run out of memory, at which point a restart is required
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 22
Dangling Pointer
• Sometimes a program will free memory before it is finished using it;
such a mistake is called a dangling pointer, and it, as you can guess, is
also a bad thing.
• The subsequent use can crash the program, or overwrite valid
memory.
• e.g., you called free(), but then called malloc() again to allocate
something else, which then recycles the errantly-freed memory.
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 24
#include <stdlib.h>
void *calloc(size_t num, size_t size)
Other Memory APIs: calloc()
#include <stdlib.h>
2KB a llo c a t e d
2KB fre e d
(fre e )
allocated (fre e )
freed
Heap Heap
free(x) free(x)
(free) (free) Undefined Error
Stack Stack
2KB 2 K B ( in v a lid )
2KB *x 2KB(invalid)
16KB 16KB *x
Address Space Address Space
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 26
Double Free
• Programs also sometimes free memory more than once; this is known
as the double free.
• The result of doing so is undefined.
• As you can imagine, the memory-allocation library might get confused
and do all sorts of weird things; crashes are a common outcome.
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 27
#include <stdlib.h>
void *realloc(void *ptr, size_t size)
Other Memory APIs: realloc()
#include <stdlib.h>
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 28
#include <unistd.h>
int brk(void *addr)
void *sbrk(intptr_t increment);
System Calls
#include <unistd.h>
• brk() and sbrk() change the location of the program break, which defines the
end of the process's data segment
• brk() sets the end of the data segment to the value specified by addr, sbrk()
increments the program's data space by increment bytes.
• Programmers should never directly call either brk or sbrk.
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 29
System Calls(Cont.)
#include <sys/mman.h>
void *mmap(void *ptr, size_t length, int port, int flags, int fd, off_t offset)
#include <sys/mman.h>
void *mmap(void *ptr, size_t length, int port, int flags, int fd, off_t offset)
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 30