0% found this document useful (0 votes)
74 views25 pages

Operating Systems Session 10 Memory Virtualization: Address Spaces, Memory API

Uploaded by

mine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views25 pages

Operating Systems Session 10 Memory Virtualization: Address Spaces, Memory API

Uploaded by

mine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

18CS2102R

Operating Systems
Session 10
Memory Virtualization : Address Spaces, Memory API

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

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

3
OS in The Early System
• Load only one process in memory.
• Poor utilization and efficiency
0KB
Operating System
Operating System
(code, data, etc.)
(code, data, etc.)
64KB

Current
Current
Program
Program
(code, data, etc.)
(code, data, etc.)

max
Physical Memory
4
Multiprogramming and Time Sharing
• Load multiple processes in memory. 0KB
Operating System
Operating System
(code, data, etc.)
• Execute one for a short while. 64KB (code, data, etc.)
Free
• Switch processes between them in memory. 128KB
Free
Process C
• Increase utilization and efficiency. (code, data, etc.)
192KB
Process B
(code, data, etc.)
256KB
• Cause an important protection issue. Free
Free
• Errant memory accesses from other processes 320KB
Process A
(code, data, etc.)
384KB
Free
Free
448KB
Free
Free
512KB
Physical Memory

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
Program Code
Program Code
1KB
Heap
Heap
2KB

(free)
(free)

15KB
Stack
Stack
16KB
Address Space
6
Address Space(Cont.)
• Code
• Where instructions live
Program Code
Program Code
• Heap
Heap
Heap
• Dynamically allocate memory.
• malloc in C language
• new in object-oriented language
(free)
(free)
• Stack
• Store return addresses or values.
• Contain local variables arguments to routines.
Stack
Stack

Address Space

7
Virtual Address
• Every address in a running program is virtual.
• OS translates the virtual address to physical address

#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
int main(int argc, char *argv[]){
printf("location of code : %p\n", (void *) main);
printf("location of code : %p\n", (void *) main);
printf("location of heap : %p\n", (void *) malloc(1));
printf("location of heap : %p\n", (void *) malloc(1));
int x = 3;
int x = 3;
printf("location of stack : %p\n", (void *) &x);
printf("location of stack : %p\n", (void *) &x);
return x;
return x;
}
}
A simple program that prints out addresses

8
Virtual Address(Cont.)
Address Space
0x400000
• The output in 64-bit Linux machine Code
Code
(Text)
location of code : 0x40057d 0x401000 (Text)
location of code : 0x40057d
location of heap : 0xcf2010 Data
location of heap : 0xcf2010 Data
location of stack : 0x7fff9ca45fcc
location of stack : 0x7fff9ca45fcc 0xcf2000
Heap
0xd13000 Heap

heap

(free)
(free)

stack

0x7fff9ca28000
Stack
0x7fff9ca49000 Stack

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

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


int *x = malloc(10 * sizeof(int));
printf(“%d\n”, sizeof(x));
printf(“%d\n”, sizeof(x));

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

int x[10];
int x[10];
printf(“%d\n”, sizeof(x));
printf(“%d\n”, sizeof(x));

40
40

11
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

12
Memory Allocating
2KB
pointer

heap

(free)
(free)
stack int *pi; // local variable

*pi
16KB
Address Space

2KB
allocated
2KB + 4 allocated
allocated
2KB + 8 allocated
allocated
2KB + 12 allocated
allocated pi = (int *)malloc(sizeof(int)* 4);
allocated
(free)
(free)
2KB *pi
16KB 2KB
Address Space
13
Memory Freeing
2KB
freed
2KB + 4 freed
freed
2KB + 8 freed
freed
2KB + 12 freed free(pi);
freed free(pi);
freed
(free)
(free)
2KB(invalid) *pi
16KB 2KB(invalid)
Address Space

2KB

heap

(free)
(free)
stack

2KB(invalid) *pi
16KB 2KB(invalid)
Address Space
14
Forgetting To Allocate Memory
• Incorrect code
char *src = “hello”; //character string constant
char *src = “hello”; //character string constant
char *dst; //unallocated
char *dst; //unallocated
strcpy(dst, src); //segfault and die
strcpy(dst, src); //segfault and die

hello\0
hello\0

heap
strcpy(dst, src); (free) unallocated
(free)
stack

*dst
*dst
*src
*src
Address Space

15
Forgetting To Allocate Memory(Cont.)
• Correct code
char *src = “hello”; //character string constant
char *src = “hello”; //character string constant
char *dst (char *)malloc(strlen(src) + 1 ); // allocated
char *dst (char *)malloc(strlen(src) + 1 ); // allocated
strcpy(dst, src); //work properly
strcpy(dst, src); //work properly

hello\0 hello\0
hello\0 hello\0
allocated hello\0
allocated hello\0

strcpy(dst, src);
strcpy(dst, src); heap heap
(free) (free)
(free) (free)
stack stack

*dst *dst
*dst *dst
*src *src
*src *src
Address Space Address Space

16
Not Allocating Enough Memory
• Incorrect code, but work properly
char *src = “hello”; //character string constant
char *src = “hello”; //character string constant
char *dst (char *)malloc(strlen(src)); // too small
char *dst (char *)malloc(strlen(src)); // too small
strcpy(dst, src); //work properly
strcpy(dst, src); //work properly
h
eh
6 bytes
strlen le
l l
ol
\0o
\0
‘\0’ is omitted 5 bytes hello\0
hello\0

strcpy(dst, src); heap


strcpy(dst, src); (free)
(free)
stack

*dst
*dst
*src
*src
Address Space
Forgetting to Initialize
• Encounter an uninitialized read
int *x = (int *)malloc(sizeof(int)); // allocated
int *x = (int *)malloc(sizeof(int)); // allocated
printf(“*x = %d\n”, *x); // uninitialized memory access
printf(“*x = %d\n”, *x); // uninitialized memory access

allocated
value used before allocated
value used before with value used
(free) with value used
(free) before
before

heap heap

(free) (free)
(free) (free)
stack stack

*x *x
*x *x
Address Space Address Space

18
Memory Leak
• A program runs out of memory and eventually dies.
unused
unused
: unused, but not freed

allocated unused unused


allocated unused unused
allocated unused
allocated unused
heap heap
unused
unused
heap
(free) allocated
(free) (free) allocated
(free)
stack (free)
stack (free)
*d
*d
*c
*c
*b *b
*b *b
*a *a *a
*a *a *a
Address Space Address Space Address Space

run out of memory

19
Dangling Pointer
 Freeing memory before it is finished using
 A program accesses to memory with an invalid pointer

*b *b free()
*b *b unreachable
*a *a
*a *a
dangling pointer

2KB 2KB
3KB 3KB
3KB 3KB
3KB 4KB 3KB
freed
4KB free(b) freed
4KB 4KB
NULL NULL
NULL NULL
Heap Heap

(free) (free)
(free) (free)
Stack Stack
*b 3KB *b 3KB
3KB 3KB
*a 2KB *a 2KB
2KB 2KB
Address Space Address Space
20
Other Memory APIs: calloc()
#include <stdlib.h>
#include <stdlib.h>
void *calloc(size_t num, size_t size)
void *calloc(size_t num, size_t size)

• 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

21
Double Free
• Free memory that was freed already.
int *x = (int *)malloc(sizeof(int)); // allocated
int *x = (int *)malloc(sizeof(int)); // allocated
free(x); // free memory
free(x); // free memory
free(x); // free repeatedly
free(x); // free repeatedly

2KB 2KB
allocated freed
allocated freed
Heap Heap
free(x) free(x)
(free)
(free)
(free)
(free)
Undefined Error

Stack Stack
2KB *x 2KB(invalid)
16KB 2KB 16KB 2KB(invalid) *x
Address Space Address Space

22
Other Memory APIs: realloc()
#include <stdlib.h>
#include <stdlib.h>
void *realloc(void *ptr, size_t size)
void *realloc(void *ptr, size_t size)

• 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

23
System Calls
#include <unistd.h>
#include <unistd.h>
int brk(void *addr)
int brk(void *addr)
void *sbrk(intptr_t increment);
void *sbrk(intptr_t increment);

• malloc library call use brk system call.


• brk is called to expand the program’s break.
• break: The location of the end of the heap in address space
• sbrk is an additional call similar with brk.
• Programmers should never directly call either brk or sbrk.

24
System Calls(Cont.)
#include <sys/mman.h>
#include <sys/mman.h>
void *mmap(void *ptr, size_t length, int port, int flags, int fd, off_t offset)
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.

25

You might also like