0% found this document useful (0 votes)
3 views

11. Memory Virtualization Address Space and Memory API

The document discusses memory virtualization in operating systems, explaining how it creates an illusion of a full memory space for each process while ensuring isolation and efficiency. It covers concepts such as address spaces, memory allocation and deallocation using APIs like malloc() and free(), and common memory management issues like memory leaks and dangling pointers. Additionally, it provides examples of memory allocation and the importance of proper memory handling in programming.

Uploaded by

ksrinithya2
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

11. Memory Virtualization Address Space and Memory API

The document discusses memory virtualization in operating systems, explaining how it creates an illusion of a full memory space for each process while ensuring isolation and efficiency. It covers concepts such as address spaces, memory allocation and deallocation using APIs like malloc() and free(), and common memory management issues like memory leaks and dangling pointers. Additionally, it provides examples of memory allocation and the importance of proper memory handling in programming.

Uploaded by

ksrinithya2
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

21CS2109AA

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>

int main(int argc, char *argv[]){

printf("location of code : %p\n", (void *) main);


printf("location of heap : %p\n", (void *) malloc(1));
int x = 3;
printf("location of stack : %p\n", (void *) &x);

return x;
}

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]){

printf("location of code : %p\n", (void *) main);


printf("location of heap : %p\n", (void *) malloc(1));
int x = 3;
printf("location of stack : %p\n", (void *) &x);

return x;
}

A simple program that prints out addresses

© 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

location of code : 0x40057d 0x401000 D a ta

location of heap : 0xcf2010 Data


location of stack : 0x7fff9ca45fcc
0xcf2000 Heap

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>

void* malloc(size_t size)

• Allocate a memory region on the heap.


• Argument
• size_t size : size of the memory block(in bytes)
• size_t is an unsigned integer type.
• Return
• Success : a void type pointer to the memory block allocated by malloc
• Fail : a null pointer

© 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 = malloc(10 * sizeof(int));


printf(“%d\n”, sizeof(x));

• The actual size of ‘x’ is known at compile-time.


4

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>

void free(void* ptr)

• Free a memory region allocated by a call to malloc.


• Argument
• void *ptr : a pointer to a memory block allocated with malloc
• Return
• none

© 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

allocated pi = (int *)malloc(sizeof(int)* 4);


(fre e )

(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

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

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 )

strcpy(dst, src); heap heap


(free) (free)
stack stack
*dst *dst

* 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

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

‘\0’ is omitted 5 bytes hello\0


(fre e )

strcpy(dst, src);

strcpy(dst, src); heap


(free)
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
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

unused : unused, but not freed

a llo c a t e d unused unused

allocated unused unused


a llo c a t e d 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

run out of memory

© 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>

void *calloc(size_t num, size_t size)

• calloc() allocates the memory and also initializes the allocates


memory block to zero. If we try to access the content of these blocks
then we’ll get 0 whereas if u want to access the content of the block
which was allocated by malloc(), it will return garbage values.
• Allocate memory on the heap and zeroes it before returning.
• Argument
• size_t num : number of blocks to allocate
• size_t size : size of each block(in bytes)
• Return
• Success : a void type pointer to the memory block allocated by calloc
• Fail : a null pointer
© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 25
Double Free
int *x
free(x);
free(x);
= (int
//
//
*)malloc(sizeof(int));
free
free
memory
repeatedly
//
• Free memory that was freed already.
allocated

int *x = (int *)malloc(sizeof(int)); // allocated


free(x); // free memory
free(x); // free repeatedly

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>

void *realloc(void *ptr, size_t size)

• The realloc() function is used to resize allocated memory without


losing old data.
• Change the size of memory block.
• A pointer returned by realloc may be either the same as ptr or a new.
• Argument
• void *ptr: Pointer to memory block allocated with malloc, calloc or realloc
• size_t size: New size for the memory block(in bytes)
• Return
• Success: Void type pointer to the memory block
• Fail : Null pointer

© 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>

int brk(void *addr)


void *sbrk(intptr_t increment);

• malloc library call use brk system call.

• 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)

• mmap system call can create an anonymous memory region.

© 2022 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 30

You might also like