You are on page 1of 32

Chapter 9

Memory Management Policies:


UNIX
Topics
Allocating swap space
Freeing swap space
Swapping
Demand paging

THE DESIGN OF THE UNIX OPERATING SYSTEM


Maurice J. bach Prentice Hall
1
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

2
Data Structures for Process
Kernel Process Kernel Region
Table A Process Table

Per Process Region Table

Text

File Descriptor Table Data

Stack
U Area
3
Data Structure for Process (contd.)

per process
region table Kernel region table
u area

main memory
Kernel
process table

4
UNIX Memory Management
Policies

• Swapping
– Easy to implement
– Less system overhead

• Demand Paging
– Greater flexibility

5
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

6
Allocating Swap Space

Address Unit
Allocate 100 unit
1 10000 101 9900
Map

Allocate 50 unit

Allocate 100 unit


251 9750 151 9850

7
Freeing Swap Space
Address Unit 101 50
50 unit free at 101
251 9750 251 9750
Map

Case 1: Free resources fill a hole,


but not contiguous to any resources in the map

8
Freeing Swap Space
Address Unit 101 50
50 unit free at 101
251 9750 251 9750
Map
100 unit free at 1

1 150

251 9750

Case 2: Free resources fill a hole,


and immediately precedes an entry in the map
9
Freeing Swap Space
Address Unit 101 50
50 unit free at 101
251 9750 251 9750
Map
100 unit free at 1

1 150 Allocate 200 unit 1 150

451 9550 251 9750

300 unit free at 151 Case 3: Free resources fill


a hole, and completely
1 10000 fills the gap between
entries in the map 10
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

11
Swapping Process Out
• Memory  Swap device
• Kernel swap out when it needs memory
1. When fork() called for allocate child process
2. When brk() called for increase the size of
process
3. When process become larger by growth of its
stack
4. Previously swapped out process want to swap
in but not enough memory

12
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

13
Swapping Process Out

Virtual Addresses Physical Addresses Swap device


684
Text 0 278k
1k 432k
:
Data 65k 573k
66k 595k
690
:
Stack 128k 401k
:

Mapping process onto the swap device


14
Swapping Process In

Virtual Addresses Physical Addresses Swap device


684
Text 0 278k
1k 432k
:
Data 65k 573k
66k 595k
690
:
Stack 128k 401k
:

Swapping a process into memory


15
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

16
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

17
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

18
Demand Paging
• Bring a page into memory only when it is needed
– Less I/O needed
– Less memory needed
– Faster response
– More users

• Page is needed  reference to it


– invalid reference  abort
– not-in-memory  bring to memory
• Lazy swapper – never swaps a page into memory unless
page will be needed
– Swapper that deals with pages is a pager

19
Transfer of a Paged Memory to Contiguous Disk Space

20
Data Structure for Demand Paging

• Page table entry

• Disk block descriptors

• Page frame data table

• Swap use table

21
Page Table Entry and Disk Block
Descriptor
Region Page Table

Page Table Entry Disk Block Descriptor

Page Table Entry

Page address age Cp/wrt mod ref val prot

Disk Block Descriptor

Swap device Block num Type


22
Page Table Entry
• 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

23
Valid-Invalid Bit
• With each page table entry a valid–invalid bit is associated
(v  in-memory, i  not-in-memory)
• Initially valid–invalid bit is set to i on all entries
• Example of a page table snapshot:
Frame # valid-invalid bit
v
v
v
v
i
….

i
i
page table
• During address translation, if valid–invalid bit in page table entry
is I  page fault
24
Page Table When Some Pages Are Not in Main Memory

25
Demand Paging
• Fork in a paging System
• vfork function
• Creates a new process only to ‘exec’ a new
program
• No copy of parent’s address space for child (not
needed!)
• Before exec, child runs in “address space of
parent”
• Efficient in paged virtual memory
• Child runs first
• Parent waits until child ‘exec’ or ‘exit’
26
Demand Paging
• Exec in a Paging System in a paging
System

• The page-stealer process


• Kernel process that swaps out memory
pages that are no longer part of the working
set of a process

27
Demand Paging
• Page Fault
• If there is a reference to a page, first reference to that
page will trap to operating system:
page fault
1. Operating system looks at another table to decide:
– Invalid reference  abort
– Just not in memory
2. Get empty frame
3. Swap page into frame
4. Reset tables
5. Set validation bit = v
6. Restart the instruction that caused the page fault

28
Demand Paging
• Validity fault handler
• If a process attempts to access a page
whose valid bit is not set, it incurs a
validity fault and the kernel invokes the
validity fault handler
• Protection fault handler
• Another type of memory fault
• Process access a valid page but the
permission bits associated with the page
does not permit the access.

29
Demand Paging
• Protection fault can also occur when a
process attempts to write a page whose
COW bit is set during fork

30
A Hybrid System with Swapping
and Demand Paging
• Demand paging systems treat memory more
flexibly than swapping systems
• But in some situations the page stealer and
validity fault handler trash because of
shortage of memory
• If the sum of working sets of all processes
is greater than the physical memory on a
machine, the fault handler will sleep as it
cannot allocate pages for a process

31
A Hybrid System with Swapping
and Demand Paging
• The page stealer will not be able to steal
pages fast enough as all the pages are in the
working set
• System V kernel runs swapping and
demand paging algorithms to avoid trashing
problems
• Swapper swaps the process out until
memory becomes free

32

You might also like