Upload
lediep
View
216
Download
0
Embed Size (px)
Citation preview
Operating Systems and Computer Networks
Memory Management II
Prof. Dr.-Ing. Axel HungerAlexander Maxeiner, M.Sc.
Institute of Computer EngineeringFaculty of Engineering
University Duisburg-Essen
Alexander Maxeiner, M.Sc. & Dr.-Ing. Pascal A. Klein
Alexander Maxeiner, M.Sc.University Duisburg-Essen
2Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Swapping
Alexander Maxeiner, M.Sc.University Duisburg-Essen
3Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Allocation
Protection from each other, Protecting OS
Translating logical addresses to physical
Swapping(if physical memory is too small for all processes)
Virtual memory (memory abstraction)
Sharing of Memory
Program 1
Free Space
Program 3
Program 2
Free Space
OS
Alexander Maxeiner, M.Sc.University Duisburg-Essen
4Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Fetch strategy
• When AND how much to load at a time (e.g.: prefetching)
Placement (or allocation) strategy
• Determination WHERE data is to be placed
Replacement strategy
• Determination WHICH area can be removed
Variations• Fixed partitions
• Variable partitions
• Segmentation
• paging
Memory Strategies
Program 1
Free Space
Program 3
Program 2
Free Space
OS
Modern PCs
Early computers,
AND embedded, small, etc...
Alexander Maxeiner, M.Sc.University Duisburg-Essen
5Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Set of physical addresses from 0 to end of memory
Memory changing commands work with physical addresses on machine level.
Translation of virtual addresses into physical ones depends on the software and memory allocation strategies.
Example: MOV REG1, 1000 (Assembler) moves the content of register (memory block) 1000 to register 1.
Should multiple programs share memory without boundaries, processes might swap/delete/override data crucial for other tasks.
Using main memory
Alexander Maxeiner, M.Sc.University Duisburg-Essen
6Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Divide all physical memory into a fixed set of contiguous partitions.
Place only one process at a time in any partition.
Partition boundaries limit the available memory for each process.
Process either entirely in main memory or not.
No sharing between processes.
Memory wasting:
– partition internal fragmentation
– Free partitions too small
Without virtual addressing – protection problems!
Without Swapping/virtual memory – size problems!
12 KiB
2 KiB
6 KiB
OS: 2 KiB
Fixed allocation
Alexander Maxeiner, M.Sc.University Duisburg-Essen
7Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Issue: memory (RAM) is too small
„swap“ data on slower but bigger memory
Swapper decides which processes should be in main memory
Memory Abstraction – Swapping
Free Space
OS
Alexander Maxeiner, M.Sc.University Duisburg-Essen
8Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Assumption: 180 KiB of Memory, OS: 76 KiB
Swapping – Example
Free Space:180 KiB
OS: 76 KiB
Alexander Maxeiner, M.Sc.University Duisburg-Essen
9Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Assumption: 180 KiB of Memory, OS: 76 KiB
Process A (30 KiB) is created or swapped in
Process B (50 KiB) is created or swapped in
Process C (20 KiB) is created or swapped in
Process D (20 KiB) is created or swapped in
Swapping – Example
Free Space:60 KiB
OS: 76 KiB
Process A:30 KiB
Process B:50 KiB
Process C:20 KiB
Process D:20 KiB
Alexander Maxeiner, M.Sc.University Duisburg-Essen
10Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Assumption: 180 KiB of Memory, OS: 76 KiB
Process A (30 KiB) is created or swapped in
Process B (50 KiB) is created or swapped in
Process C (20 KiB) is created or swapped in
Process D (20 KiB) is created or swapped in
Process A is swapped out to disk
Process C is swapped out to disk
What is the problem?
Swapping – Example
Free Space:60 KiB
OS: 76 KiB
Process B:50 KiB
Process D:20 KiB
Free Space:30 KiB
Free Space:20 KiB
Alexander Maxeiner, M.Sc.University Duisburg-Essen
11Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Memory Fragmentation
• Memory Compaction
Problem: CPU time
• Fixed size of partitions
Problem: growing memoryBigger partitions with room for grow
(here: heap)
• Variable size of partitions
Management with Bit-Maps or Linked Lists
Trade-off: Space requirements vs. time for allocation
Swapping Issue
Free Space
OS
Free Space
Process C
Process D
Alexander Maxeiner, M.Sc.University Duisburg-Essen
12Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Memory division into allocation units.
Corresponding bit for each allocation unit (0,1)telling whether it is free or not.
Example: Allocation units of 16 KiB
• For current 16 bits: 00110111 10011111
Trade-off: • Bit-Map size depends on allocation unit.
• searching for k consecutive 0-bits in map costly.
• fixed size.
• Fixed allocation can cause huge internal fragmentation.
Managing free space
with Bit-Maps
Free Space:32 KiB
OS: 80 KiB(76 used)
Process B:64 KiB
(50 used)
Process D:32 KiB
(20 used)
Free Space:32 KiB
Free Space:16 KiB
Alexander Maxeiner, M.Sc.University Duisburg-Essen
13Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Linked list of allocated andfree memory segments
Each record has
– Process ID/ Free (H: hole)
– Start address
– length
– pointer to next record
Managing free space
with Linked Lists
2H 3 5B 5 10H 2
12D 2 14H X6
Free Space:60 KiB
OS: 76 KiB
Process B:50 KiB
Process D:20 KiB
Free Space:30 KiB
Free Space:20 KiB
Alexander Maxeiner, M.Sc.University Duisburg-Essen
14Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Update Process:
– When a process is swapped out, neighbouring blocks need to be examined
– doubly linked lists(containing a pointer to the next AND previousnode)
Managing free space
with Linked Lists
2H 3 5P 5 10H 2
12P 2 14H X6
Free Space:60 KiB
OS: 20 KiB
Process B:50 KiB
Process D:20 KiB
Free Space:30 KiB
Free Space:20 KiB
Alexander Maxeiner, M.Sc.University Duisburg-Essen
15Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Virtual Memory
Alexander Maxeiner, M.Sc.University Duisburg-Essen
16Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Issue: By using physical addresses all executions are absolutely addressed NOT relatively!
Better memory management with logical addresses
Independency of actual physical location of data in physical memory
OS determines logical location of data
CPU instructions using virtual addresses(don‘t care about location)
Translated by hardware into physical address
Virtual Addressing
Program 1
Free Space
Program 3
Program 2
Free Space
OS
addressspace
addressspace
addressspace
Alexander Maxeiner, M.Sc.University Duisburg-Essen
17Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Software size much bigger than available RAM.
Each program‘s address space broken up into chunks
Pages
Page: contiguous range of addresses mapped onto physical memory.
Not all pages have to be in physical memory.
If OS notices, a program part is not in physical memory, it needs to be loaded and re-executed.
In Multiprogramming: While program is waiting for pieces of itself to be read in, CPU can execute another process.
Virtual Memory
Alexander Maxeiner, M.Sc.University Duisburg-Essen
18Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Page: Fixed size unit in virtual memory
Page frame: corresponding units in physical memory
Same size (here: 4KB)
Transparent to the program
Protection – a program cannot leave Virtual Address Space
Example: 64KB virtual address space, 32KB physical space, 4KB page size
• How many pages/page frames?
Answer: 16 virtual pages, 8 page frames
Paging
Alexander Maxeiner, M.Sc.University Duisburg-Essen
19Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Relationship between
virtual and physical memory
X
X
X
7X
X
6
2
X
4
5
X
3
0
1
virtual
address
space
0KB-4KB
4KB-8KB
...
60KB-64KB
28KB-32KB
0KB-4KB
4KB-8KB
...
page
frame
Addresses No.
4.096 - 8.191
Alexander Maxeiner, M.Sc.University Duisburg-Essen
20Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Relationship between
virtual and physical memory
X
X
X
7X
X
6
2
X
4
5
X
3
0
1
virtual
address
space
0KB-4KB
4KB-8KB
...
60KB-64KB
28KB-32KB
0KB-4KB
4KB-8KB
...
page
frame
Addresses No.
0 – 4.095
MOV REG, 0
MMU checks Virtual Memory
page „0“ corresponds to page frame 1
Address is changed to „4096“
Alexander Maxeiner, M.Sc.University Duisburg-Essen
21Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Relationship between
virtual and physical memory
X
X
X
7X
X
6
2
X
4
5
X
3
0
1
virtual
address
space
0KB-4KB
4KB-8KB
...
60KB-64KB
28KB-32KB
0KB-4KB
4KB-8KB
...
page
frame
Addresses No.
8.192 – 12.287
MOV REG, 8192
MMU checks VM
page „8192“ corresponds to page frame 3
Address is changed to „12288“
Alexander Maxeiner, M.Sc.University Duisburg-Essen
22Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Relationship between
virtual and physical memory
X
X
X
7X
X
6
2
X
4
5
X
3
0
1
virtual
address
space
0KB-4KB
4KB-8KB
...
60KB-64KB
28KB-32KB
0KB-4KB
4KB-8KB
...
page
frame
Addresses No.
20.480 – 24.576
MOV REG, 20500
MMU checks VM
page „20500“ corresponds to page
frame 4 with an offset of 20 Byte
(20.480 + 20)
Address is changed to „16404“
(16.384 + 20)
Alexander Maxeiner, M.Sc.University Duisburg-Essen
23Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Memory Management Unit (MMU)
p: page number
d: offset
r: page frame
Alexander Maxeiner, M.Sc.University Duisburg-Essen
24Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Virtual address has two parts: virtual page number and offset
Virtual page number is an index into a page table
Page table determines page frame number
Present/Absent bit if page is in physical memory
What happens, if program references an unmapped address? (page fault)
Paging – translating addresses
Alexander Maxeiner, M.Sc.University Duisburg-Essen
25Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Relationship between
virtual and physical memory
X
X
X
7X
X
6
2
X
4
5
X
3
0
1
virtual
address
space
0KB-4KB
4KB-8KB
...
60KB-64KB
28KB-32KB
0KB-4KB
4KB-8KB
...
page
frame
Addresses No.
12.288 – 16.383
MOV REG, 12288
MMU checks VM
page fault!
page fault!
Alexander Maxeiner, M.Sc.University Duisburg-Essen
26Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Relationship between
virtual and physical memory
X
X
X
7X
X
6
2
X
4
5
X
3
0
1
virtual
address
space
0KB-4KB
4KB-8KB
...
60KB-64KB
28KB-32KB
0KB-4KB
4KB-8KB
...
page
frame
Addresses No.
12.288 – 16.383
MOV REG, 12288
MMU checks VM
page fault!
Choose a rarely used page frame (in
phys. mem)
Save content on hard drive
page fault!
e.g.
...001010100011..
Alexander Maxeiner, M.Sc.University Duisburg-Essen
27Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Relationship between
virtual and physical memory
X
X
X
7X
X
X
2
X
4
5
6
3
0
1
virtual
address
space
0KB-4KB
4KB-8KB
...
60KB-64KB
28KB-32KB
0KB-4KB
4KB-8KBpage
frame
Addresses No.
12.288 – 16.383
MOV REG, 12288
MMU checks VM
page fault
Choose a rarely used page frame (in
phys. mem)
Save content on hard drive
Load requested page into phys. Mem
Restart trapped
instruction
page fault!
e.g.
...001101011100..
...
Alexander Maxeiner, M.Sc.University Duisburg-Essen
28Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Internal MMU operation
0 0 01 0 0 0 0 0 0 0 0 0 01 0
000
000
000111
000
101
000
000
000
011
100
000
110
001
010
0
0
0
10
1
0
0
0
1
1
1
1
1
10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
000 015
110
1 1 0 0 0 0 0 0 0 0 0 0 01 0
4 Bit 16 pages
3 Bit page
frame
12 Bit Offset 12 Bit 4096 byte
4 Bit page
number
present bit
Alexander Maxeiner, M.Sc.University Duisburg-Essen
29Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Strongly machine dependent
M/R – bit protocol access to the page
– M-bit is set when a program writes to a page
to know if it needs to be saved before physical space canbe reused
Structure of a page table entry
M R P Prot Page frame number
M – modified bit
R – reference bit
P – present/absent bit
Protection bits
Alexander Maxeiner, M.Sc.University Duisburg-Essen
30Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Strongly machine dependent
M/R – bit protocol access to the page
– R-bit is set when read/write access occurs
for decision if space should be used for overwriting
Structure of a page table entry
M R P Prot Page frame number
M – modified bit
R – reference bit
P – present/absent bit
Protection bits
Alexander Maxeiner, M.Sc.University Duisburg-Essen
31Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Strongly machine dependent
P – bit shows if page exists or not
Structure of a page table entry
M R P Prot Page frame number
M – modified bit
R – reference bit
P – present/absent bit
Protection bits
Alexander Maxeiner, M.Sc.University Duisburg-Essen
32Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Strongly machine dependent
Prot – bits are setting rights which operations are allowed on a page
– E.g.: read, write, execute
Structure of a page table entry
M R P Prot Page frame number
M – modified bit
R – reference bit
P – present/absent bit
Protection bits
Alexander Maxeiner, M.Sc.University Duisburg-Essen
33Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Easy to allocate memory
• Memory comes from a free list of fixed size chunks
• Allocating a page is just removing it from the list
• External fragmentation not a problem
Easy to swap out chunks of a program
• All chunks are the same size
• Use present bit to detect references to swapped pages
• Pages are a convenient multiple of the disk block size
Paging advantages
Alexander Maxeiner, M.Sc.University Duisburg-Essen
34Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Memory reference overhead
• 2 references per address lookup (page table, then memory)
Solution – use a hardware cache of lookups
Memory requirements to hold page tables can be significant
• 32 bit address space á 4KiB pages = 220
• 4 bytes/PTE = 4MiB per page table
• 25 processes = 100MiB just for page tables!
Solution – multilevel page tables
Paging disadvantages
Alexander Maxeiner, M.Sc.University Duisburg-Essen
35Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Observation, programs have most access on just a few pages
Hardware Implementation – TLB
Part of MMU
Small list of most important pages of a process (not more than 64)
e.g. variables used in a loop
Virtual page number, M-bit, Protect-bit, page frame, Validity
Access:
• check TLB, if needed page can be found
• If NOT: fetch entry from page table, write entry to TLB (overwrite older entries)
Translation Lookaside Buffer (TLB)
Alexander Maxeiner, M.Sc.University Duisburg-Essen
36Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
To be used for big virtual address spaces
Dividing a page table into multiple tables
Multilevel Page Table
P=1
P=0
P=0
...
P=1
1
2
0
1023
...
...
...
• Two 4MB tables
vs. of 1024*1024 entries
Alexander Maxeiner, M.Sc.University Duisburg-Essen
37Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Page fault! Load page into physical memory! Which page should be replaced?
Answer: pages which are rarely used!
Possible Algorithms:
– Not Recently Used (NRU)
– Least Recently Used (LRU)
– First In First Out (FIFO)
– Second Chance (SC)
Page Replacement Algorithms
Alexander Maxeiner, M.Sc.University Duisburg-Essen
38Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Using status bits – referenced (R) AND modified (M)
R-bit cleared periodically by OS
M-bit cleared when hard disk updated
Clearing random Entry of smallest available class:
Best entry class 0.
here: entry 1 or 3 is cleared!
Not Recently Used Algorithm (NRU)
M R P Prot Page framePage Index Offset0 1 1 111 10015 100010101011 0 1 111 10114 000001000010 0 1 111 01113 000110000011 0 1 111 00012 011100010010 0 1 111 10101 000000010011 1 1 111 01010 00110000001
Class 0: R=0, M=0Class 1: R=0, M=1Class 2: R=1, M=0Class 3: R=1, M=1
Alexander Maxeiner, M.Sc.University Duisburg-Essen
39Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Oldest page entry (that loaded first)
Sorted list ordered by loading time
Clear oldest entry no matter if referenced or modified
here: entry 5
First In First Out Algorithm (FIFO)
M R P Prot Page framePage Index Offset0 1 1 111 10015 100010101011 0 1 111 10114 000001000010 0 1 111 01113 000110000011 0 1 111 00012 011100010010 0 1 111 10101 000000010011 1 1 111 01010 00110000001
Loaded12
25612411626145
Alexander Maxeiner, M.Sc.University Duisburg-Essen
40Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Enhancement of FIFO (by using R-Bit)
Linked list ordered by loading time
Clear oldest entry if not referenced
If entry is referenced – clear R-bit and continue searching
here: entry 5 and 0 will be unreferenced AND loading time reset, then, entry 2 will be cleared and used
Second Chance Algorithm (SC)
M R P Prot Page framePage Index Offset0 1 1 111 10015 100010101011 0 1 111 10114 000001000010 0 1 111 01113 000110000011 0 1 111 00012 011100010010 0 1 111 10101 000000010011 1 1 111 01010 00110000001
Loaded12
25612411626145
Alexander Maxeiner, M.Sc.University Duisburg-Essen
41Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Clears page that has been unused for the longest time
Linked list ordered by access time
based on assumption, that pages used in the last few instructions will be used in the next few future instructions
Problem: Entries have to be updated every access linked-list must be reordered ;-(
here: entry 2 is cleared. (updated on hard drive first)
Least Recently Used Algorithm (LRU)
M R P Prot Page framePage Index Offset0 1 1 111 10015 100010101011 0 1 111 10114 000001000010 0 1 111 01113 000110000011 0 1 111 00012 011100010010 0 1 111 10101 000000010011 1 1 111 01010 00110000001
Loaded12
25612411626145
Access245256163116267245
Alexander Maxeiner, M.Sc.University Duisburg-Essen
42Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Questions?
Alexander Maxeiner, M.Sc.University Duisburg-Essen
43Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering
OSCN – Memory Management
Tanenbaum, Andrew S., “Modern Operating Systems”, 3rd edition, Pearson Education Inc, Amsterdam, Netherlands, 2008.
Tanenbaum, Andrew S., “Moderne Bertriebssysteme”, 3rd edition, Pearson Education Inc, Amsterdam, Netherlands, 2009.
Lee, Insup, „CsE 380 Computer Operating Systems“, Lecture Notes, University of Pennsylvania, 2002.
Snoeren, Alex C., „Lecture 10: Memory Management“, LectureNotes, UC San Diego, 2010.
Resources