0% found this document useful (0 votes)
8 views29 pages

3 1 Memory

The document discusses memory virtualization in operating systems, explaining how it creates an illusion of memory space for each process and enhances programming ease, memory efficiency, and process isolation. It covers various memory management concepts, including multiprogramming, address space, virtual addresses, and memory APIs like malloc, free, calloc, and realloc. Additionally, it highlights common memory management issues such as memory leaks, dangling pointers, and the importance of proper memory allocation and deallocation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views29 pages

3 1 Memory

The document discusses memory virtualization in operating systems, explaining how it creates an illusion of memory space for each process and enhances programming ease, memory efficiency, and process isolation. It covers various memory management concepts, including multiprogramming, address space, virtual addresses, and memory APIs like malloc, free, calloc, and realloc. Additionally, it highlights common memory management issues such as memory leaks, dangling pointers, and the importance of proper memory allocation and deallocation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Operating Systems

Topic: Memory

Endadul Hoque
Acknowledgement

• Youjip Won (Hanyang University)

• OSTEP book – by Remzi and Andrea Arpaci-Dusseau


(University of Wisconsin)
Memory Virtualization
• What is memory virtualization?
– OS virtualizes its physical memory.
– OS provides an illusion of memory space per process.
– It seems each process uses the whole memory .
Benefit of Memory Virtualization
• Ease of use in programming
• Memory efficiency in terms of time and space
• The guarantee of isolation for processes as well
as OS
– Protection from errant accesses of other processes
OS in The Early System
• Load only one process in memory.
– Poor utilization and efficiency 0KB
Operating System
(code, data, etc.)
64KB

Current
Program
(code, data, etc.)

max
Physical Memory
Multiprogramming and Time Sharing
0KB
• Load multiple processes in memory. Operating System
(code, data, etc.)
64KB
– Execute one for a short while.
Free
– Switch between processes in memory. 128KB
Process C
– Increase utilization and efficiency. (code, data, etc.)
192KB
Process B
(code, data, etc.)
• Cause an important protection issue. 256KB
Free
– Errant memory accesses 320KB
Process A
from other processes (code, data, etc.)
384KB
Free
448KB
Free
512KB
Physical Memory
Address Space
• OS creates an abstraction of physical memory.
– The address space contains the content of 0KB
Program Code
an active (precisely, alive) process.
1KB
– That consists of program code, data, Data + BSS
heap, stack and etc. 2KB
Heap
3KB

(free)

15KB
Stack
16KB
Address Space
Address Space
• Code 0KB
Program Code
– Where instructions live 1KB
Data + BSS
• Heap 2KB
Heap
– Dynamically allocate memory. 3KB
• malloc in C language
• new in object-oriented language (free)
• Stack
– Store return addresses or values.
15KB
– Contain local variables arguments Stack
to routines/functions/methods. 16KB
Address Space
Virtual Address
• Every address in a running process 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;
}

A simple program that prints out addresses


Virtual Address
Address Space
0x55daf920000
• The output in 64-bit Linux machine Code
0x55daf980000
(Text)
location of code : 0x55daf92ed6fa Data + BSS
location of heap : 0x55daf9cda670
0x55daf9c0000
location of stack : 0x7ffd44c580b4 Heap
0x55daf9f0000

heap

(free)

stack

0x7ffd44000000
Stack
0x7ffd44c00000
MEMORY API
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
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));

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


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

40
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
Memory Allocating
2KB
pointer

heap

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

*pi
16KB
Address Space

2KB
allocated
2KB + 4
allocated
2KB + 8
allocated
2KB + 12
allocated pi = (int *)malloc(sizeof(int)* 4);

(free)

2KB *pi
16KB
Address Space
Memory Freeing
2KB
freed
2KB + 4
freed
2KB + 8
freed
2KB + 12 free(pi);
freed

(free)

2KB(invalid) *pi
16KB
Address Space

2KB

heap

(free)
stack

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

hello\0

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

stack

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

hello\0 hello\0

allocated hello\0

strcpy(dst, src); heap heap


(free) (free)
stack stack

*dst *dst
*src *src
Address Space Address Space
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

‘\0’ is omitted h
e
strlen l
6 bytes l
o
\0

5 bytes hello
\0

strcpy(dst, src); heap


(free)
stack

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

value used allocated


before with value used
(free) before

heap heap

(free) (free)

stack stack

*x *x
Address Space Address Space
Memory Leak
• A program runs out of memory and eventually dies.
unused : unused, but not freed

allocated unused unused

allocated unused

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

run out of memory


Dangling Pointer
• Freeing memory before it is finished using
– A program accesses to memory with an invalid pointer
*b *b free()
unreachable
*a *a

dangling pointer

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

(free) (free)

Stack Stack
*b 3KB *b 3KB
*a 2KB *a 2KB
Address Space Address Space
Other Memory APIs: calloc()
#include <stdlib.h>

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
Double Free
• Free memory that was freed already.
int *x = (int *)malloc(sizeof(int)); // allocated
free(x); // free memory
free(x); // free repeatedly

2KB 2KB
allocated freed
Heap Heap
free(x) free(x)
Undefined
(free) (free)
Error
Stack Stack
2KB *x 2KB(invalid)
16KB 16KB *x
Address Space Address Space
Other Memory APIs: realloc()
#include <stdlib.h>

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
System Calls
#include <unistd.h>

int brk(void *addr)


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.
System Calls
#include <unistd.h>

int brk(void *addr)


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.
Reading Material

• Chapter 13-14 of OSTEP book – by Remzi and Andrea


Arpaci-Dusseau (University of Wisconsin)
http://pages.cs.wisc.edu/~remzi/OSTEP/vm-intro.pdf
http://pages.cs.wisc.edu/~remzi/OSTEP/vm-api.pdf
Questions?

You might also like