51
Memory Management Virtual Memory By : Kaushik Vaghani Prepared By : Kaushik Vaghani

Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

  • Upload
    others

  • View
    38

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

Memory ManagementVirtual Memory

By : Kaushik Vaghani

Prepared By : Kaushik Vaghani

Page 2: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

Virtual Memory

• Background

• Page Fault

• Dirty Page / Dirty Bit

• Demand Paging

• Copy-on-Write

• Page Replacement

Prepared By : Kaushik Vaghani

Page 3: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 4: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 5: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

• 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

Page 6: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

Virtual Memory That is Larger Than Physical Memory

Prepared By : Kaushik Vaghani

Page 7: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 8: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 9: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

Shared Library Using Virtual Memory

Prepared By : Kaushik Vaghani

Page 10: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 11: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 12: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

Demand Paging

• Page is needed reference to it

– invalid reference abort

– not-in-memory bring to memory

Prepared By : Kaushik Vaghani

Page 13: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 14: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 15: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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 16: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

Page Table When Some Pages Are Not in Main Memory

Prepared By : Kaushik Vaghani

Page 17: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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 18: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 19: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

Steps in Handling a Page Fault

Prepared By : Kaushik Vaghani

Page 20: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 21: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

Before Process 1 Modifies Page C

Prepared By : Kaushik Vaghani

Page 22: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

After Process 1 Modifies Page C

Prepared By : Kaushik Vaghani

Page 23: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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 24: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 25: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

Need For Page Replacement

Prepared By : Kaushik Vaghani

Page 26: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 27: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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 28: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

Page Replacement

Prepared By : Kaushik Vaghani

Page 29: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 30: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

Graph of Page Faults Versus The Number of Frames

Prepared By : Kaushik Vaghani

Page 31: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 32: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 33: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 34: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 35: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 36: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 37: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 38: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

Use Of A Stack to Record Most Recent Page References

Prepared By : Kaushik Vaghani

Page 39: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 40: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 41: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

Second-Chance (clock) Page-Replacement Algorithm

Prepared By : Kaushik Vaghani

Page 42: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 43: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 44: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 45: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

Thrashing (Cont.)

Prepared By : Kaushik Vaghani

Page 46: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

Extra Studies

Prepared By : Kaushik Vaghani

Page 47: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

Operating System Examples

• Windows

• Solaris

Prepared By : Kaushik Vaghani

Page 48: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 49: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

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

Page 50: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

Solaris 2 Page Scanner

Prepared By : Kaushik Vaghani

Page 51: Memory Management Virtual Memory - WordPress.com · System libraries shared via mapping into virtual address space Virtual memory enables processes to share memory, that allows two

Thank You

Prepared By : Kaushik Vaghani