Upload
others
View
38
Download
0
Embed Size (px)
Citation preview
Memory ManagementVirtual Memory
By : Kaushik Vaghani
Prepared By : Kaushik Vaghani
Virtual Memory
• Background
• Page Fault
• Dirty Page / Dirty Bit
• Demand Paging
• Copy-on-Write
• Page Replacement
Prepared By : Kaushik Vaghani
Objectives
• To describe the benefits of a virtual memorysystem
• To explain the concepts of demand paging,page-replacement algorithms, and allocation ofpage frames
• To discuss the principle of the working-setmodel
Prepared By : Kaushik Vaghani
Background• Code needs to be in memory to execute, but entire
program rarely used– Error code, unusual routines, large data structures like array,
list and tables.
• Entire program code not needed at same time
• Consider ability to execute partially-loaded program– Program no longer constrained by limits of physical memory
– Each program takes less memory while running -> more programs run at the same time
• Increased CPU utilization and throughput with no increase in response time or turnaround time
– Less I/O needed to load or swap programs into memory -> each user program runs fasterPrepared By : Kaushik Vaghani
• 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– More programs running concurrently– Less I/O needed to load or swap processes
• Virtual address space – logical view of how process is stored in memory– Typically this view is that a process begins at a certain logical address- say,
address 0 and exists in contiguous memory until end of space– Meanwhile, physical memory organized in page frames– MMU must map logical to physical
• Virtual memory can be implemented via:– Demand paging – Demand segmentation
Background
Prepared By : Kaushik Vaghani
Virtual Memory That is Larger Than Physical Memory
Prepared By : Kaushik Vaghani
Virtual-address Space• we allow for the heap to grow upward in
memory as it is used for dynamic memoryallocation.
• Similarly, we allow for the stack to growdownward in memory through successivefunction calls.
• The large blank space (or hole) between theheap and the stack is part of the virtual addressspace but will require actual physical pagesonly if the heap or stack grows.
• Virtual address spaces that include holes areknown as sparse address spaces.
• Enables sparse address spaces with holes leftfor growth, dynamically linked libraries, etc
Prepared By : Kaushik Vaghani
Virtual-address Space
• In addition to separating logical memory from physical memory,virtual memory allows files and memory to be shared by two ormore processes through page sharing. This leads to thefollowing benefits:
System libraries shared via mapping into virtual address
space
Virtual memory enables processes to share memory, that
allows two or more processes can communicate through
the use of shared memory.
Virtual memory can allow pages to be shared duringfork() system call, thus speeding up process
creation. Prepared By : Kaushik Vaghani
Shared Library Using Virtual Memory
Prepared By : Kaushik Vaghani
Demand Paging• How an executable program might be loaded form disk to
memory ?
• One option is to load entire program into memory at execution time– However, a problem with this approach is that we may not initially
need the entire program in memory.
• Or load a page into memory only when it is needed (Called Demand Paging)
– With demand-paged virtual memory, pages are only loaded when they are demanded during program execution; pages that are never accessed are thus never loaded into physical memory.
– Less memory needed
– Faster response
– More users’ processesPrepared By : Kaushik Vaghani
Demand Paging• A Demand Paging system is similar to
a paging system with swapping(diagram on right) where processesresides in secondary memory (disk).
• When we want to execute a process, we swap it into memory.
• Rather than swapping the entire process into memory, however, we use a lazy swapper.
• Lazy swapper – never swaps a page into memory unless page will be needed
– Swapper that deals with pages is a pager.
Fig. Transfer of a paged memory to contiguous disk space
Prepared By : Kaushik Vaghani
Demand Paging
• Page is needed reference to it
– invalid reference abort
– not-in-memory bring to memory
Prepared By : Kaushik Vaghani
Basic Concepts
• When a process is to be swapped in, the pager guesses which pageswill be used before process swapped out again.
• Instead of swapping in a whole process, the pager brings in onlythose pages into memory.
• How to determine pages that are in memory and are on disk.
• If pages needed are already memory resident
– No difference from non demand-paging
• If page needed and not memory resident
– Need to detect and load the page into memory from storage
• Without changing program behavior
• Without programmer needing to change code
Demand Paging
Prepared By : Kaushik Vaghani
Valid-Invalid Bit
• we need some form of hardware support to distinguishbetween the pages that are in memory and the pages thatare on the disk.
• The valid-invalid bit scheme is used for this pupose.
• With each page table entry a valid–invalid bit is associated
v in-memory – memory resident,
i the page either is not valid (not in logical address spaceof process) or is valid but is currently on the disk (not-in-memory).
• Initially valid–invalid bit is set to i on all entries.
Prepared By : Kaushik Vaghani
Valid-Invalid Bit
• Notice that marking a page invalidwill have no effect if the processnever attempts to access thatpage.
• During MMU address translation,if valid–invalid bit in page tableentry is i page fault
Example of a page table snapshot
Prepared By : Kaushik Vaghani
Page Table When Some Pages Are Not in Main Memory
Prepared By : Kaushik Vaghani
Page Fault
• Access to a page marked invalid causes a PageFault.
• The paging hardware, in translating the addressthrough the page table, will notice that theinvalid bit is set, causing a trap to the operatingsystem.
• This trap is the result of the operating system'sfailure to bring the desired page into memory.
• The procedure for handling this page fault isstraightforward
Prepared By : Kaushik Vaghani
Page Fault1. Operating system looks at an internal table to decide whether
the reference was a valid or an invalid in memory access.
2. If
– Invalid reference Terminate the process
– Valid reference but Just not in memory page it in.
3. Find free frame
4. Swap in page into frame via scheduled disk operation
5. Reset internal table kept with process and page table to indicate page now in memorySet validation bit = v
6. Restart the instruction that caused the page faultPrepared By : Kaushik Vaghani
Steps in Handling a Page Fault
Prepared By : Kaushik Vaghani
Copy-on-Write
• Copy-on-Write (COW) allows both parent and child processes to initially share the same pages in memory
– If either process modifies a shared page, only then is the page copied
• COW allows more efficient process creation as only modified pages are copied
Prepared By : Kaushik Vaghani
Before Process 1 Modifies Page C
Prepared By : Kaushik Vaghani
After Process 1 Modifies Page C
Prepared By : Kaushik Vaghani
What Happens if There is no Free Frame?
Page Replacement
• While a user process is executing, a page fault occurs.
• The operating system determines where the desired pageis residing on the disk but then finds that there are nofree frames on the free-frame list; all memory is in use byother process pages.
• Also in demand from the kernel, I/O buffers, etc
• Page replacement – find some page in memory, but notreally in use, page it out
• Same page may be brought into memory several timesPrepared By : Kaushik Vaghani
Page Replacement
• Prevent over-allocation of memory by modifying page-fault service routine to include page replacement
• Use modify (dirty) bit to reduce overhead of page transfers – only modified pages are written to disk
• Page replacement completes separation between logical memory and physical memory – large virtual memory can be provided on a smaller physical memory
Prepared By : Kaushik Vaghani
Need For Page Replacement
Prepared By : Kaushik Vaghani
Basic Page Replacement
• Page replacement takes the following approach.
• If no frame is free, we find one that is notcurrently being used and free it.
• We can free a frame by writing its contents toswap space and changing the page table (and allother tables) to indicate that the page is no longerin memory.
• We can now use the freed frame to hold the pagefor which the process faulted.
• We modify the page-fault service routine toinclude page replacement.
Prepared By : Kaushik Vaghani
Basic Page Replacement1. Find the location of the desired page on disk
2. Find a free frame:
a) If there is a free frame, use it
b) If there is no free frame, use a page replacement algorithm toselect a victim frame
c) Write victim frame to disk; change the page and frame tablesaccordingly.
3. Bring the desired page into the (newly) free frame; update the page and frame tables
4. Continue the process by restarting the instruction that caused the trap
Note now potentially 2 page transfers for page fault – increasing EAT (effective access time) Prepared By : Kaushik Vaghani
Page Replacement
Prepared By : Kaushik Vaghani
Page and Frame Replacement Algorithms
• Frame-allocation algorithm determines – How many frames to give each process– Which frames to replace
• Page-replacement algorithm– Want lowest page-fault rate on both first access and re-access
• Evaluate algorithm by running it on a particular string of memory references (reference string) and computing the number of page faults on that string– String is just page numbers, not full addresses– Repeated access to the same page does not cause a page fault– Results depend on number of frames available
• In all our examples, the reference string of referenced page numbers is
7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
Prepared By : Kaushik Vaghani
Graph of Page Faults Versus The Number of Frames
Prepared By : Kaushik Vaghani
First-In-First-Out (FIFO) Algorithm
• The simplest page-replacement algorithm is a first-in,first-out (FIFO) algorithm.
• A FIFO replacement algorithm associates with each pagethe time when that page was brought into memory.
• When a page must be replaced, the oldest page is chosen.
• We can create a FIFO queue to hold all pages in memory.
• We replace the page at the head of the queue.
• When a page is brought into memory, we insert it at thetail of the queue.
Prepared By : Kaushik Vaghani
First-In-First-Out (FIFO) Algorithm• Reference string:
7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
• 3 frames (3 pages can be in memory at a time per process)
15 page faults
Prepared By : Kaushik Vaghani
FIFO Illustrating Belady’s Anomaly
• Can vary by reference string: consider 1,2,3,4,1,2,5,1,2,3,4,5
Belady’s AnomalyAdding more frames can cause more page faults!
•Notice that the number offaults for four frames (ten) isgreater than the number offaults for three frames(nine)! This most unexpectedresult is known as Belady’sanamaly.
•for some page-replacementalgorithms, the page-faultrate may increase as thenumber of allocated framesincreases.
Prepared By : Kaushik Vaghani
Optimal Page Replacement Algorithm (OPT)
• Replace page that will not be used for longest period of time
– Lowest page fault rate of all algorithm
– Never suffer from Belady’s Anomaly
– 9 is optimal for the example
• Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
9 Page Fault
Prepared By : Kaushik Vaghani
Optimal Page Replacement Algorithm (Cont.)
• With only nine page faults, optimal replacement is much betterthan a FIFO algorithm, which results in fifteen faults.
(If we ignore the first three, which all algorithms must suffer,then optimal replacement is twice as good as FIFO replacement.)
• In fact, no replacement algorithm can process this referencestring in three frames with fewer than nine faults.
• Unfortunately, the optimal page-replacement algorithm isdifficult to implement, because it requires future knowledge ofthe reference string.
• As a result, the optimal algorithm is used mainly for comparisonstudies.
Prepared By : Kaushik Vaghani
Least Recently Used (LRU) Algorithm
• Use past knowledge rather than future
• Replace page that has not been used in the most amount of time
• Associate time of last use with each page
• 12 faults – better than FIFO but worse than OPT
• Generally good algorithm and frequently used
• But how to implement? – Counters, Stack
Prepared By : Kaushik Vaghani
LRU Algorithm (Cont.)• Counter implementation
– Every page entry has a counter; every time page is referenced through this entry, copy the clock into the counter
– When a page needs to be changed, look at the counters to find smallest value
• Search through table needed
• Stack implementation
– Keep a stack of page numbers:
– Page referenced:
• move it to the top
– But each update more expensive
• LRU and OPT are cases of stack algorithms that don’t have Belady’s Anomaly
Prepared By : Kaushik Vaghani
Use Of A Stack to Record Most Recent Page References
Prepared By : Kaushik Vaghani
LRU Approximation Algorithms• LRU needs special hardware and still slow
• Some system provide no hardware support, andFIFO algorithm must be used.
• Many systems provide some help, however, in theform of a reference bit.
• Reference bit
– With each page associate a bit, initially = 0
– When page is referenced. Bit set to 1
– After some time, we can determine which pages havebeen used and which have not been used by examiningthe reference bits, although we do not know the orderof use.
Prepared By : Kaushik Vaghani
LRU Approximation Algorithms
Second-chance algorithm
– Generally FIFO, plus hardware-provided reference bit
– If page to be replaced has
• Reference bit = 0 -> replace it
• reference bit = 1 then:
– set reference bit 0, leave page in memory
– replace next page, subject to same rules
Prepared By : Kaushik Vaghani
Second-Chance (clock) Page-Replacement Algorithm
Prepared By : Kaushik Vaghani
Enhanced Second-Chance AlgorithmNot Recently Used (NRU)
• Improve algorithm by using reference bit and modify bit (if available) in concert
• Take ordered pair (reference, modify)1. (0, 0) neither recently used not modified – best page to
replace2. (0, 1) not recently used but modified – not quite as good,
must write out before replacement3. (1, 0) recently used but clean – probably will be used again
soon4. (1, 1) recently used and modified – probably will be used
again soon and need to write out before replacement
• When page replacement called for, use the clock scheme but use the four classes replace page in lowest non-empty class Prepared By : Kaushik Vaghani
Counting Algorithms• Keep a counter of the number of references
that have been made to each page
• Least Frequently Used (LFU) Algorithm: replaces page with smallest count
• Most Frequently Used (MFU) Algorithm: based on the argument that the page with the smallest count was probably just brought in and has yet to be used
Prepared By : Kaushik Vaghani
Thrashing
• If a process does not have “enough” pages, the page-fault rate is very high– Page fault to get page– Replace existing frame– But quickly need replaced frame back– This leads to:
• Low CPU utilization• Operating system thinking that it needs to increase the degree
of multiprogramming• Another process added to the system
• Thrashing a process is busy swapping pages in and out
Prepared By : Kaushik Vaghani
Thrashing (Cont.)
Prepared By : Kaushik Vaghani
Extra Studies
Prepared By : Kaushik Vaghani
Operating System Examples
• Windows
• Solaris
Prepared By : Kaushik Vaghani
Windows• Uses demand paging with clustering. Clustering brings
in pages surrounding the faulting page
• Processes are assigned working set minimum and working set maximum
• Working set minimum is the minimum number of pages the process is guaranteed to have in memory
• A process may be assigned as many pages up to its working set maximum
• When the amount of free memory in the system falls below a threshold, automatic working set trimming is performed to restore the amount of free memory
• Working set trimming removes pages from processes that have pages in excess of their working set minimum
Prepared By : Kaushik Vaghani
Solaris • Maintains a list of free pages to assign faulting processes
• Lotsfree – threshold parameter (amount of free memory) to begin paging
• Desfree – threshold parameter to increasing paging
• Minfree – threshold parameter to being swapping
• Paging is performed by pageout process
• Pageout scans pages using modified clock algorithm
• Scanrate is the rate at which pages are scanned. This ranges from slowscan to fastscan
• Pageout is called more frequently depending upon the amount of free memory available
• Priority paging gives priority to process code pages
Prepared By : Kaushik Vaghani
Solaris 2 Page Scanner
Prepared By : Kaushik Vaghani
Thank You
Prepared By : Kaushik Vaghani