Memory Management 1 Memory Management “Multitasking without
Memory Management Policies
-
Upload
shivani-gujral -
Category
Documents
-
view
7 -
download
0
description
Transcript of Memory Management Policies
![Page 1: Memory Management Policies](https://reader035.fdocuments.us/reader035/viewer/2022081520/577cc7601a28aba711a0bf93/html5/thumbnails/1.jpg)
1
Memory Management Policies: UNIX
Chapter 9
THE DESIGN OF THE UNIX OPERATING SYSTEM
Maurice J. bach Prentice Hall
Allocating swap space
Freeing swap space
Swapping
Demand paging
Topics
![Page 2: Memory Management Policies](https://reader035.fdocuments.us/reader035/viewer/2022081520/577cc7601a28aba711a0bf93/html5/thumbnails/2.jpg)
2
Memory
• Primary memory is a precious resource that frequently cannot contain all active processes in the system
• The memory management system decides which processes should reside (at least partially) in main memory
• It monitors the amount of available primary memory and may periodically write processes to a secondary device called the swap device to provide more space in primary memory
• At a later time, the kernel reads the data from swap device back to main memory
![Page 3: Memory Management Policies](https://reader035.fdocuments.us/reader035/viewer/2022081520/577cc7601a28aba711a0bf93/html5/thumbnails/3.jpg)
3
Data Structures for Process
Text
Stack
DataFile Descriptor Table
Per Process Region Table
Kernel Process Table
Kernel Region TableA Process
U Area
![Page 4: Memory Management Policies](https://reader035.fdocuments.us/reader035/viewer/2022081520/577cc7601a28aba711a0bf93/html5/thumbnails/4.jpg)
4
Data Structure for Process (contd.)
u area
main memoryKernel process table
per process region table Kernel region table
![Page 5: Memory Management Policies](https://reader035.fdocuments.us/reader035/viewer/2022081520/577cc7601a28aba711a0bf93/html5/thumbnails/5.jpg)
5
UNIX Memory Management Policies
• Swapping– Easy to implement– Less system overhead
• Demand Paging– Greater flexibility
![Page 6: Memory Management Policies](https://reader035.fdocuments.us/reader035/viewer/2022081520/577cc7601a28aba711a0bf93/html5/thumbnails/6.jpg)
6
Swapping
• The swap device is a block device in a configurable section of a disk
• Kernel allocates contiguous space on the swap device without fragmentation
• It maintains free space of the swap device in an in-core table, called map
• The kernel treats each unit of the swap map as group of disk blocks
• As kernel allocates and frees resources, it updates the map accordingly
![Page 7: Memory Management Policies](https://reader035.fdocuments.us/reader035/viewer/2022081520/577cc7601a28aba711a0bf93/html5/thumbnails/7.jpg)
7
Allocating Swap Space
1 10000 101 9900
151 9750251 9850
Allocate 100 unit
Allocate 100 unit
Allocate 50 unit
Map
Address Unit
![Page 8: Memory Management Policies](https://reader035.fdocuments.us/reader035/viewer/2022081520/577cc7601a28aba711a0bf93/html5/thumbnails/8.jpg)
8
Freeing Swap Space
251 975050 unit free at 101
Map
Address Unit 101 50
251 9750
Case 1: Free resources fill a hole,
but not contiguous to any resources in the map
![Page 9: Memory Management Policies](https://reader035.fdocuments.us/reader035/viewer/2022081520/577cc7601a28aba711a0bf93/html5/thumbnails/9.jpg)
9
Freeing Swap Space
251 975050 unit free at 101
Map
Address Unit 101 50
251 9750
100 unit free at 1
1 150
251 9750
Case 2: Free resources fill a hole,
and immediately precedes an entry in the map
![Page 10: Memory Management Policies](https://reader035.fdocuments.us/reader035/viewer/2022081520/577cc7601a28aba711a0bf93/html5/thumbnails/10.jpg)
10
Freeing Swap Space
251 975050 unit free at 101
Allocate 200 unit
Map
Address Unit 101 50
251 9750
100 unit free at 1
1 150
251 9750
1 150
451 9550
1 10000
300 unit free at 151 Case 3: Free resources fill a hole, and completely fills the gap between entries in the map
![Page 11: Memory Management Policies](https://reader035.fdocuments.us/reader035/viewer/2022081520/577cc7601a28aba711a0bf93/html5/thumbnails/11.jpg)
11
Algorithm: Allocate Swap Space
• malloc( address_of_map, number_of_unit)– for (every map entry)
• if (current map entry can fit requested units)– if (requested units == number of units in entry)
» Delete entry from map– else
» Adjust start address of entry– return original address of entry
– return -1
![Page 12: Memory Management Policies](https://reader035.fdocuments.us/reader035/viewer/2022081520/577cc7601a28aba711a0bf93/html5/thumbnails/12.jpg)
12
Swapping Process Out
• Memory Swap device• Kernel swap out when it needs memory
1. When fork() called for allocate child process2. When called for increase the size of process3. When process become larger by growth of its
stack4. Previously swapped out process want to swap
in but not enough memory
![Page 13: Memory Management Policies](https://reader035.fdocuments.us/reader035/viewer/2022081520/577cc7601a28aba711a0bf93/html5/thumbnails/13.jpg)
13
Swapping Process Out
• The kernel must gather the page addresses of data at primary memory to be swapped out
• Kernel copies the physical memory assigned to a process to the allocated space on the swap device
• The mapping between physical memory and swap device is kept in page table entry
![Page 14: Memory Management Policies](https://reader035.fdocuments.us/reader035/viewer/2022081520/577cc7601a28aba711a0bf93/html5/thumbnails/14.jpg)
14
Swapping Process Out
Mapping process onto the swap device
:
128k 401k
:
66k 595k
65k 573k
:
1k 432k
0 278k
Virtual Addresses Swap device684
690
Text
Data
Stack
Physical Addresses
![Page 15: Memory Management Policies](https://reader035.fdocuments.us/reader035/viewer/2022081520/577cc7601a28aba711a0bf93/html5/thumbnails/15.jpg)
15
Swapping Process In
Swapping a process into memory
:
128k 401k
:
66k 595k
65k 573k
:
1k 432k
0 278k
Virtual Addresses Swap device684
690
Text
Data
Stack
Physical Addresses
![Page 16: Memory Management Policies](https://reader035.fdocuments.us/reader035/viewer/2022081520/577cc7601a28aba711a0bf93/html5/thumbnails/16.jpg)
16
Fork Swap
• There may not be enough memory when fork() called
• Child process swap out and “ready-to-run”
• Swap in when kernel schedule it
![Page 17: Memory Management Policies](https://reader035.fdocuments.us/reader035/viewer/2022081520/577cc7601a28aba711a0bf93/html5/thumbnails/17.jpg)
17
Expansion Swap
• It reserves enough space on the swap device to contain the memory space of the process, including the newly requested space
• Then it adjust the address translation mapping of the process
• Finally, it swaps the process out on newly allocated space in swapping device
• When the process swaps the process into memory, it will allocate physical memory according to new address translation map
![Page 18: Memory Management Policies](https://reader035.fdocuments.us/reader035/viewer/2022081520/577cc7601a28aba711a0bf93/html5/thumbnails/18.jpg)
18
Demand Paging
• Not all page of process resides in memory• Locality• When a process accesses a page that is not part
of its working set, it incurs a page fault.• The kernel suspends the execution of the
process until it reads the page into memory and makes it accessible to the process
![Page 19: Memory Management Policies](https://reader035.fdocuments.us/reader035/viewer/2022081520/577cc7601a28aba711a0bf93/html5/thumbnails/19.jpg)
19
Data Structure for Demand Paging
• Page table entry
• Disk block descriptors
• Page frame data table
• Swap use table
![Page 20: Memory Management Policies](https://reader035.fdocuments.us/reader035/viewer/2022081520/577cc7601a28aba711a0bf93/html5/thumbnails/20.jpg)
20
Page Table Entry and Disk Block Descriptor
Disk Block DescriptorPage Table Entry
Page address age Cp/wrt mod ref val prot
Region
Page Table Entry
Page Table
TypeBlock numSwap device
Disk Block Descriptor
![Page 21: Memory Management Policies](https://reader035.fdocuments.us/reader035/viewer/2022081520/577cc7601a28aba711a0bf93/html5/thumbnails/21.jpg)
21
• Contains the physical address of page and the following bits:– Valid: whether the page content legal– Reference: whether the page is referenced recently– Modify: whether the page content is modified– copy on write: kernel must create a new copy when a
process modifies its content (required for fork)– Age: Age of the page– Protection: Read/ write permission
Page Table Entry
![Page 22: Memory Management Policies](https://reader035.fdocuments.us/reader035/viewer/2022081520/577cc7601a28aba711a0bf93/html5/thumbnails/22.jpg)
22
Disk Block Descriptor
• Swap Device number as there may be several swap devices
• Block number that contains page
TypeBlock numSwap device