COS-4
COS-4
BLG 312E
Week -4
a) Round Robin
b) Shortest Job First
c) First in, First Out
d) Lottery Scheduling (with unequal number of tickets pre-distributed)
13. The Abstraction: Address Space
Operating System: Three Easy Pieces
Memory Virtualization
• Every address generated by a user program is a virtual address.
Operating
System
• Poor utilization and efficiency (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
Multiprogramming and Time Sharing
• Load multiple processes in memory.
0KB
Operating
O p e r a t in g
S y s te m
(c o d e , d a ta , e tc .)
System
• Execute one for a short while. 64KB Fre e
B Process B
(code, data, etc.)
256K
• Cause an important protection issue.
Fre e
B
Free
320K
• Errant memory accesses from other processes B Process A
(code, data, etc.)
384K Fre e
B
Free
448K Fre e
B
Free
512K
B
Physical
Memory
Address Space
0KB
P ro g ra m C o d e
Program Code
• OS creates an abstraction of physical memory. 1KB Heap
Heap
• The address space contains all about a running process. 2KB (fre e )
15K
S ta c k
B Stack
16K
B
Address Space (Cont.)
• Code
P ro g ra m C o d e
Program Code
• Where instructions live
Heap
Heap
• Heap (fre e )
Stack
• Contain local variables arguments to routines.
Virtual Address
• Every address in a running program is virtual and OS translates the
virtual address to physical address.
#include <stdio.h>
#include <stdlib.h>
return x;
}
#include <stdio.h>
#include <stdlib.h>
return x;
}
Data
0xcf2000 Heap
Heap
location of code : 0x40057d
location of heap : 0xcf2010
location of stack : 0x7fff9ca45fcc
(free)
stack
0x7fff9ca280
S ta c k
00 Stack
0x7fff9ca490
00
14. Memory API
Operating System: Three Easy Pieces
Memory API: malloc()
#include <stdlib.h>
int x[10];
printf(“%d\n”, sizeof(x));
int x[10];
printf(“%d\n”, sizeof(x));
40
40
Memory API: free()
#include <stdlib.h>
B pointer
hea
p
(free)
stac int *pi; // local variable
k
*pi
16K
B Address Space
2K a llo c a t e d
allocated
2KB + B
4 a llo c a t e d
allocated
2KB + 8 a llo c a t e d
allocated
2KB + a llo c a t e d
pi = (int *)malloc(sizeof(int)*
12 (fre e )
allocated 4);
(free)
2KB
2KB *pi
16K
B Address Space
Freeing Memory
2K fre e d
freed
2KB + B
4 fre e d
freed
2KB + 8 fre e d
freed free(pi);
2KB + fre e d
free(pi);
12 (fre e )
freed
(free)
2 K B ( in v a lid )
2KB(invalid) *pi
16K
B Address
Space
2K (fre e )
B
hea
p
(free)
stac
k
2 K B ( in v a lid )
2KB(invalid) *pi
16K
B Address
Space
Common Errors
• Forgetting to allocate memory
• Not allocating enough memory
• Forgetting to initialize allocated memory
• Forgetting to free memory
• Freeing memory before you are done with it
• Freeing memory repeatedly
• Calling free() incorrectly
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 )
hea
strcpy(dst, src); p
(free) unallocated
stac
k
*dst
* s rc
*dst
*src
Address
Space
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 )
strcpy(dst, src);
strcpy(dst, src);
hea hea
p
(free) p
(free)
stac stac
k k
*dst *dst
* s rc
*dst * s rc
*dst
*src *src
Address Address
Space 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
h
h
e
e
strlen l
l
6
l
l
bytes o
o
\0
\0
h e llo \0
strcpy(dst, src);
* s rc
*dst
*src
Address
Forgetting to Initialize Allocated
Memory
• Encounter an uninitialized read
int *x = (int *)malloc(sizeof(int)); // allocated
v a lu e u s e d a llo c a t e d
b e fo re w it h v a lu e
(fre e ) u s e d b e fo re
heap heap
(free) (free)
stac stac
k k
*x *x
*x *x
Address Address
Space Space
Forgetting to free memory (Memory
Leak)
• A program runs out of memory and eventually dies.
unused
(fre e )
allocated unused
unused
(fre e )
heap heap
unused
hea a llo c a t e d
(free) p allocated
(free) (fre e )
stac (free)
stac
*d
k *d
k
*c
*b *b
*c
*a *a
*b *a
*b
*a *a *a
Address Address Address
Space Space Space
*b
free()
*b
*b *b unreachable
*a
*a
*a
*a
dangling pointer
2K 3KB
2K 3KB
3KB 3KB
B B
3K 3K
4KB
fre e d
4KB freed
B free(b B
4K N U LL
) 4K N U LL
NULL NULL
B (fre e )
B (fre e )
Heap Heap
(free) (free)
Stack Stack
3KB 3KB
*b 2KB
3KB *b 2KB
3KB
*a 2KB *a 2KB
Address Address
Space Space
Freeing Memory Repeatedly
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)
) Undefined
(free) (free)
Error
Stack Stack
2KB 2 K B ( in v a lid )
2KB *x 2KB(invalid)
16KB 16KB *x
Address Address
Space Space
Other Memory APIs: calloc()
#include <stdlib.h>
void *calloc(size_t num, size_t size)
#include <stdlib.h>
#include <stdlib.h>
#include <unistd.h>
void func()
int x;
...
x = x + 3; // this is the line of code we are interested
in
• Presume that the address of ‘x’ has been place in ebx register.
• Load the value at that address into eax register.
• Add 3 to eax register.
• Store the value in eax back into memory.
Example: Address Translation
(Cont.) 0KB 128
P ro g ra m C o d e
movl 0x0(%ebx),%eax
132 addl 0x03,%eax
1KB 135 movl %eax,0x0(%ebx)
• Fetch instruction at address 128 2KB Heap
Program Code
14K 3000
S ta c k
B
15K 3000
B Stack
16K
B
Relocation Address Space
• What if the OS wants to place the process somewhere else in physical
memory, not at address 0?
• Note that the address space starts at address 0.
A Single Relocated Process
0K P ro g ra m C o d e
0KB
O p e r a t in g S y s t e m
B
Program Code Operating System
16K
Heap
( n o t in u s e )
B
Heap
(not in use)
(fre e )
32K Code
Code
B
Heap
(a llo c a te d
b u t n o t in u s e )
Heap
hea
Relocated
p (allocated
Process
(free) but not in use)
S ta c k
Stack
stac 48K ( n o t in u s e )
k B
S ta c k
(not in use)
Stack
64K
16K B Physical
B Address Memory
Space
Base and Bounds Register
0K P ro g ra m C o d e
0KB
O p e r a t in g S y s t e m
B
Program Code Operating System
16K
Heap
( n o t in u s e )
B
Heap
(not in use) base
register
(fre e )
32KB
32K Code
32KB
Code
B Heap
Heap
(a llo c a te d
b u t n o t in u s e )
hea
p (allocated
(free) but not in use)
S ta c k
Stack
stac 48K ( n o t in u s e )
k B
S ta c k
(not in use)
bounds Stack
64K
register
16KB
16KB B Physical
16K Address Memory
𝑝h𝑦𝑠𝑖𝑐𝑎𝑙𝑎𝑑𝑑𝑟𝑒𝑠𝑠=𝑣𝑖𝑟𝑡𝑢𝑎𝑙𝑎𝑑𝑑𝑟𝑒𝑠𝑠+𝑏𝑎𝑠𝑒
0≤𝑣𝑖𝑟𝑡𝑢𝑎𝑙𝑎𝑑𝑑𝑟𝑒𝑠𝑠<𝑏𝑜𝑢𝑛𝑑𝑠
𝑝h𝑦 𝑠𝑖𝑐𝑎𝑙𝑎𝑑𝑑𝑟𝑒𝑠𝑠=𝑣𝑖𝑟𝑡𝑢𝑎𝑙𝑎𝑑𝑑𝑟𝑒𝑠𝑠+𝑏𝑎𝑠𝑒
• Every virtual address must not be greater than bound and negative.
0≤𝑣𝑖𝑟𝑡𝑢𝑎𝑙 𝑎𝑑𝑑𝑟𝑒𝑠𝑠<𝑏𝑜𝑢𝑛𝑑𝑠
3 2 8 9 6 = 1 2 8 + 3 2 𝐾 𝐵 ( 𝑏𝑎𝑠𝑒)
4 7 𝐾 𝐵 = 1 5 𝐾 𝐵 + 3 2 𝐾 𝐵 ( 𝑏𝑎𝑠𝑒)
movl 0x0(%ebx),%eax
132 addl 0x03,%eax
128: movl 0x0(%ebx), %eax 1KB 135 movl %eax,0x0(%ebx)
Program Code
2KB
• Fetch instruction at address 128
Heap
3KB Heap
32896=128+ 32 𝐾𝐵(𝑏𝑎𝑠𝑒) 4KB (fre e )
14K 3000
S ta c k
B
15K 3000
B Stack
16K
Two ways of using Bounds Register
0K P ro g ra m C o d e
0KB
O p e r a t in g
S y s te m
B
Program Code Operating
System
16K
Heap
( n o t in u s e )
B
Heap
(not in use)
(fre e )
32K Code
Code
B
Heap
( a llo c a t e d
b u t n o t in u s e )
Heap
(allocated
(free) but not in use)
bound bound
s
S ta c k
Stack s
16KB 48KB
16KB 48K ( n o t in u s e )
48KB
B
S ta c k
(not in use)
Stack
64K
B Physical Memory
16K Address Space
B
OS Issues for Memory Virtualization
• The OS must take action to implement base-and-bounds approach.
• Three critical junctures:
• When a process starts running:
• Finding space for address space in physical memory
• When a process is terminated:
• Reclaiming the memory for use
• When context switch occurs:
• Saving and storing the base-and-bounds pair
OS Issues: When a Process Starts
Running
• The OS must find a room for a new address space.
• free list: A list of the range of the physical memory which are not in use.
0KB
O p e r a t in g S y s t e m
Operating System
The OS lookup the free list
16K ( n o t in u s e )
Free list B
(not in use)
16K
B
16K
32K Code
Code
B
B
Heap
use)
S ta c k
48K Stack
48K ( n o t in u s e )
B B
(not in use)
64K
B Physical
OS Issues: When a Process Is
Terminated
• The OS must put the memory back on the free list.
16KB
16K
B
16KB 16K
( n o t in u s e ) ( n o t in u s e )
16K
B B
(not in use) (not in use)
32KB
P ro c e s s A
32K
B
32KB ( n o t in u s e )
48K
B
48KB ( n o t in u s e )
(not in use)
48K 64KB
64KB
B
Physical Physical
Memory Memory
OS Issues: When Context Switch
Occurs
• The OS must save and restore the base-and-bounds pair.
• In process structure or process control block (PCB)
Process A PCB
…
O p e r a t in g S y s t e m O p e r a t in g S y s t e m
base : 32KB
0KB bounds :
Context Switching 0KB 48KB …
Operating System Operating System
16KB 16KB
( n o t in u s e ) ( n o t in u s e )
48KB
32KB
P ro c e s s A
C u rre n t ly R u n n in g
32KB 32KB
P ro c e s s A
48KB
Process A
bound Process A bound
Currently Running
s s
48KB
64KB
48KB
P ro c e s s B
48KB 48KB
P ro c e s s B
C u rre n t ly R u n n in g
64KB
Process B
Process B
Currently Running
64KB 64KB
Physical Physical
Memory Memory