0% found this document useful (0 votes)
4 views47 pages

COS-4

The document covers key concepts in computer operating systems, focusing on memory virtualization, address space, and memory management APIs like malloc, free, calloc, and realloc. It discusses the benefits of memory virtualization, such as ease of programming and process isolation, and highlights common errors in memory allocation and management. Additionally, it reviews multiprogramming and time-sharing techniques to improve memory utilization and efficiency.

Uploaded by

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

COS-4

The document covers key concepts in computer operating systems, focusing on memory virtualization, address space, and memory management APIs like malloc, free, calloc, and realloc. It discusses the benefits of memory virtualization, such as ease of programming and process isolation, and highlights common errors in memory allocation and management. Additionally, it reviews multiprogramming and time-sharing techniques to improve memory utilization and efficiency.

Uploaded by

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

Computer Operating Systems

BLG 312E
Week -4

Prof. Dr. Kemal Bıçakcı


Review of Last Week
Review Question
• Which one of these scheduling algorithms has the lowest average
response time?

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.

• What is memory virtualization?


• OS virtualizes its physical memory.
• OS provides an illusion of memory space per each process.
• It seems to be seen like each process uses the whole memory.
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
OS in The Early Systems
0KB
• Load only one process in memory.
O p e r a t in g
S y s te m
(c o d e , d a ta , e tc .)

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

(code, data, etc.)


Free
• Switch processes between them in memory. 128K
B Process C
• Increase utilization and efficiency. 192K
(code, data, etc.)

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 )

• Consists of program code, heap, stack and etc.


(free)

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 )

• Dynamically allocate memory.


• malloc in C language (free)
• new in object-oriented language
• Stack
• Store return addresses or values. S ta c k

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>

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


Virtual Address (Cont.) 0x400000 Code
( Te x t )
Address Space
Code
(Text)
• The output in 64-bit Linux machine
0x401000 D a ta

Data
0xcf2000 Heap

Heap
location of code : 0x40057d
location of heap : 0xcf2010
location of stack : 0x7fff9ca45fcc

location of code : 0x40057d 0xd13000 (fre e )

location of heap : 0xcf2010


location of stack : 0x7fff9ca45fcc
heap

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

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 (confusing) results of sizeof with variables:
int *x = malloc(10 * sizeof(int));
printf(“%d\n”, sizeof(x));

int *x = malloc(10 * sizeof(int));


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

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

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

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
Allocating Memory
2K (fre e )

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

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

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

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

‘\0’ is omitted 5 bytes hello\0


(fre e )

strcpy(dst, src);

strcpy(dst, src); hea


p
(free)
stac
k
*dst

* s rc
*dst
*src
Address
Forgetting to Initialize Allocated
Memory
• Encounter an uninitialized read
int *x = (int *)malloc(sizeof(int)); // allocated

printf(“*x = %d\n”, *x); // uninitialized memory access

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


printf(“*x = %d\n”, *x); // uninitialized memory access

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

value used allocated


before with value
(fre e )
(free) (fre e )
used before

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

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

run out of memory


Freeing memory before you are
done
(Dangling Pointer)
 A program accesses to memory with an invalid pointer

*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

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>

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
Other Memory APIs: realloc()
#include <stdlib.h>
void *realloc(void *ptr, size_t size)

#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
malloc() and free(): Are they system
calls?
System Calls
#include <unistd.h>
int brk(void *addr)
void *sbrk(intptr_t increment);

#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.
15. Address Translation
Operating System: Three Easy Pieces
Memory Virtualizing with Efficiency
and Control
• Memory virtualizing takes a similar strategy known as limited direct
execution (LDE) for efficiency and control.
• In memory virtualizing, efficiency and control are attained by
hardware support.
• e.g., registers, TLBs (Translation Look-aside Buffers), page-table
Address Translation
• Hardware transforms a virtual address to a physical address.
• The desired information is actually stored in a physical address.

• The OS must get involved at key points to set up the hardware.


• The OS must manage memory to sensibly intervene.
Example: Address Translation
• Code in C language
void func()
int x;
...
x = x + 3; // this is the line of code we are interested
in

void func()
int x;
...
x = x + 3; // this is the line of code we are interested
in

• Load a value from memory


• Increment it by three
• Store the value back into memory
Example: Address Translation
(Cont.)
• Same Code in Assembly
128 : movl 0x0(%ebx), %eax ; load 0+ebx into eax
132 : addl $0x03, %eax ; add 3 to eax register
135 : movl %eax, 0x0(%ebx) ; store eax back to mem

128 : movl 0x0(%ebx), %eax ; load 0+ebx into eax


132 : addl $0x03, %eax ; add 3 to eax register
135 : movl %eax, 0x0(%ebx) ; store eax back to mem

• 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

• Execute this instruction (load from address 15KB) 3KB Heap


4KB
• Fetch instruction at address 132
(fre e )

• Execute this instruction (no memory reference) heap

• Fetch the instruction at address 135


(free)
• Execute this instruction (store to address 15 KB)
stack

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≤𝑣𝑖𝑟𝑡𝑢𝑎𝑙𝑎𝑑𝑑𝑟𝑒𝑠𝑠<𝑏𝑜𝑢𝑛𝑑𝑠

Dynamic (Hardware based)


Relocation
• When a program starts running, the OS decides where in physical
memory a process should be loaded.
• Set the base register a value.

𝑝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 𝐾 𝐵 ( 𝑏𝑎𝑠𝑒)

Relocation and Address Translation


0KB 128
P ro g ra m C o d e

128: movl 0x0(%ebx), %eax

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 )

• Execute this instruction heap

• Load from address 15KB


(free)
47 𝐾𝐵=15 𝐾𝐵+32 𝐾𝐵(𝑏𝑎𝑠𝑒)
stack

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

(allocated but not in


use)
Heap
(allocated but not in
48K
B

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.

Free list 0KB


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

Free list 0KB


Operating System Operating System
16K
B

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

48K Process A 32K (not in use)


B B
48KB
( n o t in u s e )

48KB ( n o t in u s e )

(not in use) 48K


B

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

(not in use) base (not in use) base


32KB

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

You might also like