Docsity

Virtual Memory Management: Page Replacement Algorithms and Thrashing - Prof. Prasad Kulkar, Study notes of Operating Systems

Toggle sidebar
bg1
1


■Background
■Demand Paging
■Copy-on-Write
■Page Replacement
■Allocation of Frames
■Thrashing
■Memory-Mapped Files
■Allocating Kernel Memory
■Other Considerations
■Operating-System Examples
2


■Virtual memory – separation of user logical memory
from physical memory
●only part of the program needs to be in memory for execution
●logical address space can therefore be much larger than
physical address space
●allows address spaces to be shared by several processes
●allows for more efficient process creation
■Benefits of not requiring the entire program in memory
before execution
●programs can be larger than the physical address space
●more programs can be resident in memory at the same time
●faster program startup
3


■Space between heap and
stack is not used, unless the
heap or stack grows
●virtual memory does not require
physical pages for such holes
■Shared memory
●pages can be easily shared by
virtual memory
●shared libraries can be mapped
read-only into address space of
each process
●shared memory IPC can be
implemented easily
●parent pages can be shared with
child during fork()
4


pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Virtual Memory Management: Page Replacement Algorithms and Thrashing - Prof. Prasad Kulkar and more Study notes Operating Systems in PDF only on Docsity!

1

^  

^       

n^ Background n^ Demand Paging n^ Copy-on-Write n^ Page Replacement n^ Allocation of Frames n^ Thrashing n^ Memory-Mapped Files n^ Allocating Kernel Memory n^ Other Considerations n^ Operating-System Examples

^  

^ ^ ^       

n^ Virtual memory

  • separation of user logical memory

from physical memory^ l^ only part of the program needs to be in memory for execution^ l^ logical address space can therefore be much larger thanphysical address space^ l^ allows address spaces to be shared by several processes^ l^ allows for more efficient process creation n Benefits of not requiring the entire program in memorybefore execution^ l^ programs can be larger than the physical address space^ l^ more programs can be resident in memory at the same time^ l^ faster program startup

3

^  

^       

n^ Space between heap andstack is not used, unless theheap or stack grows^ l^ virtual memory does not requirephysical pages for such

holes

n^ Shared memory^ l^ pages can be easily shared byvirtual memory^ l^ shared libraries can be mappedread-only into address space ofeach process^ l^ shared memory IPC can beimplemented easily^ l^ parent pages can be shared withchild during

fork()

^  

^       

5

^  

^       

^ 

n^ Bring a page into memory only when it is needed^ l^ less I/O needed^ l^ less memory needed^ l^ faster response^ l^ more applications simultaneously resident in memory n^ Page is needed

⇒^ reference to it

l^ invalid reference

⇒^ abort l^ not-in-memory

⇒^ bring to memory

n^ Lazy swapper

  • never swaps a page into memory

unless page will be needed^ l^ Swapper that deals with pages is a

pager

^  

^       

^  

7

^  

^       

n^ Each page table entry associated with a valid–invalid bit^ l^ ( v^ ⇒

in-memory,

i^ ⇒^ not-in-memory)

l^ initially set to invalid for all page table entries n Example of a page table snapshot: n Valid–invalid bit in page table set to

i^ during address

translation causes a

page fault

v vv v i …. i i Frame #^ valid-invalid bit page table

^  

^       

%^ 

13

^  

^       

^  . 

 ^

n^ Original process creation^ l^ fork() system called duplicates parent address space for child^ l^ demand paging with copy-on-write allow sharing address spaces n^ Copy-on-Write (COW) allows both parent and childprocesses to initially

share^ the same pages in memory

l^ shared pages marked as copy-on-write pages l^ if either process modifies a shared page, then the page is copied l^ only the modified pages are copied n COW allows more efficient process creation as onlymodified pages are copied n Free pages are allocated from a

pool^ of zeroed-out

pages

^  

^       

^  /

^  

^       

%^ '

n^ Review of demand paging^ l^ separates logical memory from physical memory^ l^ allows logical address space > physical address space^ l^ enables a greater degree of multiprogramming^ ^ higher CPU utilization and throughput^ l^ allows fast process startup n^ Drawbacks of demand paging^ l^ may increase later individual memory access times^ l^ potential for

over-allocation

of physical memory

n^ Over-allocation of memory^ l^ currently active processes may reference more pages than roomin physical memory^ l^ pages from active processes may need to be

replaced

^  

^       

n^ Page replacement is needed when a page fault occurs,and there are no free frames to allocate^ l^ terminate user process?^ l^ swap out an entire process?^ l^ find some page in memory, hopefully not really in use, and swapit out? n^ Same page may be brought into memory several times

17

^  

^       

1.^ Find the location of the desired page on disk 2.^ Find a free frame:- If there is a free frame, use it- If there is no free frame, use a page replacementalgorithm to select a

victim^ frame

3.^ Bring the desired page into the (newly) free frame;update the page and frame tables 4.^ Restart the process n^ Reducing page fault overhead^ l^ use

modify bit

to with each frame l^ only copy the replacement page to memory, if modified

^  

^       

19

^  

^       

^

n^ Criteria^ l^ get the lowest page fault rate n^ Page replacement schemes^ l^ first in first out (FIFO)^ l^ optimal page replacement^ l^ least recently used (LRU)^ l^ LRU approximation algorithms^ l^ counting based replacement n^ Evaluation of replacement algorithm^ l^ simulate it on a particular string of memory

page^ references

l^ compute the number of page faults on the reference string n In all our examples, the reference string is:^ 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1

^  

^       

25

^  

^       

0^

^

^

n^ Counter implementation^ l^ every page table entry has a

time-of-use

field

l^ copy the clock into the time-of-use field on every page access l^ for replacement, find the page with the smallest time-of-use field l^ replacement algorithm requires search of the full page table l^ each memory access needs additional write to time-of-use field l^ counter overflow?

^  

^       

0^

^

^

n^ Stack implementation^ l^ keep a stack of page numbers in a linked list^ l^ move page to top of stack when referenced^ l^ find replacement page from bottom of stack^ l^ each update is more expensive due to stack operations^ l^ no search for replacement

27

^  

^       

-^ 

^

n^ Reference bit algorithm^ l^ associate reference bit with each page^ l^ set bit when page is references; clear all bits occasionally^ l^ replace page that has reference bit = 0 (if one exists)^ l^ cannot store the order of accesses in one bit! n^ Record reference bit algorithm^ l^ each page associated with a 8-bit field for recording reference bit^ l^ at regular intervals^ ^ right shift 8-bit field^ ^ move reference bit into highest order bit^ l^ page with lowest value in 8-bit field is the LRU page^ l^ Example: (reference bit, 8-bit field)^ ^ (1, 10101100)

(0, 1101110) ^ (0, 11001101)

(0, 01100110)

^  

^       

-^ 

^

n^ Second-chance algorithm^ l^ single reference (RF) bit^ l^ basic FIFO replacement algo^ l^ find replacement page^ ^ proceed, if RF=0^ ^ give page 2

nd^ chance, if RF= and move onto next page,clear reference bit l circular queue of pages l pointer indicates nextreplacement page l what is all bits are set?

29

^  

^       

.^  $

^

n^ Keep a counter of the number of references that havebeen made to each page. n^ Least frequently used^ l^ replace page with the smallest count^ l^ (most active page should have largest count) n^ Most frequently used^ l^ replace page with the largest count^ l^ (page with smallest count was brought in last and will beaccessed next) n^ Not very popular algorithms

^  

^       

^

n^ Optimizations by maintaining a list of

victim^ frames

n^ Scheme 1^ l^ bring new page into one of victim frame slots^ l^ then, copy-out the replacement page to memory^ l^ memory access to new page does not wait for copy-out n^ Scheme 2^ l^ copy-out modified pages to disk when paging device is idle^ l^ replacement is now faster n^ Scheme 3^ l^ remember which page is in each victim frame^ l^ if new page is in one of victim frame, then use directly^ l^ works well if replacement algorithm replaces a page that isneeded after afterwards

31

^  

^       

$^ 

^ +'

n^ How many frames in physical memory should the OSallocate to each process? n^ Minimum number of frames^ l^ is there a

minimum^

number of frames a process needs? l^ depends of the maximum number of pages any instruction in theISA can reference at a time^ ^ what can happen if we do not provide this minimum? l^ direct addressing load/store need 2 frames^ ^ one for instruction, and one for memory reference l^ one-level indirect addressing needs 3 frames^ ^ one for instruction, one for address, and one for memory reference l^ PDP-11 needs 6 frames, IBM 370 needs 8 frames l^ should there be a maximum number?

^  

^       

'^

$^ 

^

n^ Equal allocation^ l^ divide

m^ frames equally among

n^ processes

l^ each process gets about

m/n^ frames

l^ may give some process more frames than it needs! n Proportional allocation l^ each process may need different amounts of memory l^ allocate memory proportional to process size l^ Example:

^    

 

  

 

×= = =∑ = =

  

    

 ^  =  =   =   ≈×=  ^ ≈×=  

37

^  

^       

#^   '0  n^ Page faults spike at the start of every new programworking set (also called a

phase )

l^ page fault rate falls until the next program phase l^ behavior repeats

^  

^       

n^ Page-Fault frequency scheme^ l^ working set model is needs several assumptions and iscomplicated^ l^ measure the page fault rate routinely^ l^ establish “acceptable” page-fault rate^ ^ If actual rate too low, process loses frame or increase processes^ ^ If actual rate too high, process gains frame, or suspend processes

39

^  

 ^  ' ^ ^       

n^ Memory-mapped file I/O allows file I/O to be treated asroutine memory access by

mapping

a disk block to a

page in memory^ l^ removes the need to read(), write() system calls^ l^ converts disk access to memory access^ l^ simplifies disk access for the user n Mechanism details^ l^ a file is initially read using demand paging^ l^ a page-sized portion of the file is read from the file system into aphysical frame^ l^ subsequent reads/writes to/from the file are treated as ordinarymemory accesses

^  

 ^  ' ()*^ ^       

n^ Allows several processes to map the same file allowingthe pages in memory to be shared

41

^  

 ^  ' (3*^ ^       

n^ Examples of OS using memory-mapped files^ l^ Solaris, Unix, and Linux provide the

mmap()^ system call to

create a memory-mapped file l files accessed using

open(), read(), write()

are mapped to kernel

address space in Solaris l Windows NT and XP use memory mapped files to accomplishshared memory

^  

 ^   4^ ^       

n^ Special I/O instructions may be available to transfer dataand control messages to the I/O controller n^ Memory-mapped I/O^ l^ I/O device registers mapped to logical memory address spaces^ l^ convenient and fast n^ There may be a control bit to indicate if data is availablein the data register^ l^ if CPU polls the control bit, then method of operation is called^ programmed I/O^ l^ if device sends interrupt to CPU to indicate data availability, thenmode of operation is

interrupt driven I/O

43

^  

^       

$^ 5  

n^ Kernel memory often allocated from a free memory pool^ l^ not using demand paging n^ Reasons for treating kernel memory allocation differently^ l^ some kernel memory needs to be contiguous^ l^ attempt to minimize memory waste due to internal fragmentation^ ^ kernel requests memory for structures of varying size^ ^ do not use paging system n^ Strategies for managing free memory^ l^ buddy system^ l^ slab allocator

^  

^       

n^ Power-of-2 allocator^ l^ satisfies requests in unitssized as power of 2^ l^ request rounded up to nexthighest power of 2^ l^ when smaller allocationneeded than is available,current chunk split into twobuddies of next-lowerpower of 2^ ^ continue until appropriatesized chunk available n^ Example:^ l^ 21kb requested from 256kb

^  

^       

n^ Program structure^ l^ being aware of the paging structure can help performance^ l^ example

^ ^

l^ Each row is stored in one page l^ program 1: 128 * 128 = 16, 384 page faults^   

l^ Program 2: 128 page faults^