page fault
page fault
page fault
- No tags were found...
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Virtual-address Space
Shared Library Using Virtual Memory
Demand PagingBring a <strong>page</strong> into memory only when it is needed Less I/O needed Less memory needed Faster response More usersPage is needed reference to it invalid reference abort not-in-memory bring to memoryLazy swapper – never swaps a <strong>page</strong> into memory unless <strong>page</strong> willbe needed Swapper that deals with <strong>page</strong>s is a <strong>page</strong>r
Transfer of a Paged Memory to Contiguous DiskSpace
Valid-Invalid BitWith each <strong>page</strong> 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 entriesFrame #Example of a <strong>page</strong> table snapshot:During address translation, if valid–invalid bit in <strong>page</strong> table entryis I <strong>page</strong> <strong>fault</strong>….<strong>page</strong> tablevalid-invalid bitvvvviii
Page Table When Some Pages Are Not in MainMemory
Page Fault If there is a reference to a <strong>page</strong>, first reference tothat <strong>page</strong> will trap to operating system:<strong>page</strong> <strong>fault</strong>1. Operating system looks at another table to decide: Invalid reference abort Just not in memory2. Get empty frame3. Swap <strong>page</strong> into frame4. Reset tables5. Set validation bit = v6. Restart the instruction that caused the <strong>page</strong> <strong>fault</strong>
Steps in Handling a Page Fault
Performance of Demand Paging Page Fault Rate 0 p 1.0 if p = 0 no <strong>page</strong> <strong>fault</strong>s if p = 1, every reference is a <strong>fault</strong>Effective Access Time (EAT)EAT = (1 – p) x memory access+ p (<strong>page</strong> <strong>fault</strong> overhead+ [swap <strong>page</strong> out ]+ swap <strong>page</strong> in+ restart overhead)
Demand Paging ExampleMemory access time = 200 nanosecondsAverage <strong>page</strong>-<strong>fault</strong> service time = 8 millisecondsEAT = (1 – p) x 200 + p (8 milliseconds)= (1 – p x 200 + p x 8,000,000= 200 + p x 7,999,800If one access out of 1,000 causes a <strong>page</strong> <strong>fault</strong>, thenEAT = 8.2 microseconds.This is a slowdown by a factor of 40!!
Copy-on-WriteCopy-on-Write (COW) allows both parent and child processes toinitially share the same <strong>page</strong>s in memoryIf either process modifies a shared <strong>page</strong>, only then is the <strong>page</strong>copiedCOW allows more efficient process creation as only modified<strong>page</strong>s are copiedFree <strong>page</strong>s are allocated from a pool of zeroed-out <strong>page</strong>s
Before Process 1 Modifies Page C
After Process 1 Modifies Page C
What happens if there is no free frame? Page replacement – find some <strong>page</strong> in memory, butnot really in use, swap it out algorithm performance – want an algorithm which will result inminimum number of <strong>page</strong> <strong>fault</strong>s Same <strong>page</strong> may be brought into memory severaltimes
Page ReplacementPrevent over-allocation of memory by modifying <strong>page</strong>-<strong>fault</strong>service routine to include <strong>page</strong> replacement Use modify (dirty) bit to reduce overhead of <strong>page</strong> transfers –only modified <strong>page</strong>s are written to diskPage replacement completes separation between logicalmemory and physical memory – large virtual memory can beprovided on a smaller physical memory
Need For Page Replacement
Basic Page Replacement1. Find the location of the desired <strong>page</strong> on disk2. Find a free frame:• If there is a free frame, use it• If there is no free frame, use a <strong>page</strong> replacement algorithm to select avictim frame3. Read the desired <strong>page</strong> into the (newly) free frame. Update the<strong>page</strong> and frame tables.4. Restart the process
Page Replacement
Page Replacement Algorithms Want lowest <strong>page</strong>-<strong>fault</strong> rate Evaluate algorithm by running it on aparticular string of memory references(reference string) and computing thenumber of <strong>page</strong> <strong>fault</strong>s on that string In all our examples, the reference string is1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
Graph of Page Faults Versus The Number ofFrames
First-In-First-Out (FIFO) Algorithm Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 53 frames (3 <strong>page</strong>s can be in memory at a time per process)11454 frames232312349 <strong>page</strong> <strong>fault</strong>s11542215 10 <strong>page</strong> <strong>fault</strong>s3324 4 3Belady’s Anomaly: more frames more <strong>page</strong> <strong>fault</strong>s
FIFO Page Replacement
FIFO Illustrating Belady’s Anomaly
Optimal AlgorithmReplace <strong>page</strong> that will not be used for longest period of time4 frames example1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 51426 <strong>page</strong> <strong>fault</strong>s34 5How do you know this?Used for measuring how well your algorithm performs
Optimal Page Replacement
Least Recently Used (LRU) Algorithm Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 511115222223554444333Counter implementation Every <strong>page</strong> entry has a counter; every time <strong>page</strong> isreferenced through this entry, copy the clock into thecounter When a <strong>page</strong> needs to be changed, look at thecounters to determine which are to change
LRU Page Replacement
Use Of A Stack to Record The Most Recent PageReferences
LRU Approximation AlgorithmsReference bit With each <strong>page</strong> associate a bit, initially = 0 When <strong>page</strong> is referenced bit set to 1 Replace the one which is 0 (if one exists). We do not know the order,however.Second chance Need reference bit Clock replacement If <strong>page</strong> to be replaced (in clock order) has reference bit = 1 then:• set reference bit 0• leave <strong>page</strong> in memory• replace next <strong>page</strong> (in clock order), subject to same rules
Second-Chance (clock) Page-ReplacementAlgorithm
Enhanced Second-Chance Additional modify bit. (0, 0): neither recently used nor modified (0,1): not recently used but modified (1,0): recently used but clean (1,1): recently used and modified More scanning time
Counting AlgorithmsKeep a counter of the number of references that have beenmade to each <strong>page</strong>LFU Algorithm: replaces <strong>page</strong> with smallest countMFU Algorithm: based on the argument that the <strong>page</strong> with thesmallest count was probably just brought in and has yet to beused
Allocation of FramesEach process needs minimum number of <strong>page</strong>sExample: IBM 370 – 6 <strong>page</strong>s to handle SS MOVE instruction: instruction is 6 bytes, might span 2 <strong>page</strong>s 2 <strong>page</strong>s to handle from 2 <strong>page</strong>s to handle toTwo major allocation schemes fixed allocation priority allocation
Fixed AllocationEqual allocation – For example, if there are 100 framesand 5 processes, give each process 20 frames.Proportional allocation – Allocate according to the size ofprocesssS saii size of processim total number of allocation forpipiframessi mSm 64ssaai212 10 12710 64 5137127 64 59137
Priority Allocation Use a proportional allocation scheme usingpriorities rather than size If process P i generates a <strong>page</strong> <strong>fault</strong>, select for replacement one of its frames select for replacement a frame from a processwith lower priority number
Global vs. Local Allocation Global replacement – process selects areplacement frame from the set of allframes; one process can take a framefrom another Local replacement – each process selectsfrom only its own set of allocated frames
ThrashingIf a process does not have “enough” <strong>page</strong>s, the <strong>page</strong>-<strong>fault</strong>rate is very high. This leads to: low CPU utilization operating system thinks that it needs to increase the degreeof multiprogramming another process added to the systemThrashing a process is busy swapping <strong>page</strong>s in and out
Thrashing (Cont.)
Demand Paging and Thrashing Why does demand paging work?Locality model Process migrates from one locality to another Localities may overlap Why does thrashing occur? size of locality > total memory size
Locality In A Memory-ReferencePattern
Working-Set Model working-set window a fixed number of <strong>page</strong> referencesExample: 10,000 instruction WSS i (working set of Process P i ) =total number of <strong>page</strong>s referenced in the most recent (varies intime) if too small will not encompass entire locality if too large will encompass several localities if = will encompass entire programD = WSS i total demand framesif D > m ThrashingPolicy if D > m, then suspend one of the processes
Working-set model
Page-Fault Frequency SchemeEstablish “acceptable” <strong>page</strong>-<strong>fault</strong> rate If actual rate too low, process loses frame If actual rate too high, process gains frame
Working Sets and Page Fault Rates
Memory-Mapped FilesMemory-mapped file I/O allows file I/O to be treated asroutine memory access by mapping a disk block to a <strong>page</strong> inmemoryA file is initially read using demand paging. A <strong>page</strong>-sizedportion of the file is read from the file system into a physical<strong>page</strong>. Subsequent reads/writes to/from the file are treated asordinary memory accesses.
Memory-Mapped Files (cont.)Simplifies file access by treating file I/O through memoryrather than read() write() system callsAlso allows several processes to map the same file allowing the<strong>page</strong>s in memory to be shared
Memory Mapped Files
Buddy System Allocates memory from fixed-size segmentconsisting of physically-contiguous <strong>page</strong>s Memory allocated using power-of-2 allocator Satisfies requests in units sized as power of 2 Request rounded up to next highest power of 2 When smaller allocation needed than is available,current chunk split into two buddies of next-lower powerof 2• Continue until appropriate sized chunk available
Buddy System Allocator
Slab AllocatorAlternate strategySlab is one or more physically contiguous <strong>page</strong>sCache consists of one or more slabsSingle cache for each unique kernel data structure Each cache filled with objects – instantiations of the data structureWhen cache created, filled with objects marked as freeWhen structures stored, objects marked as usedIf slab is full of used objects, next object allocated from emptyslab If no empty slabs, new slab allocatedBenefits include no fragmentation, fast memory requestsatisfaction
Slab Allocation
Prepaging Prepaging To reduce the large number of <strong>page</strong> <strong>fault</strong>s that occurs atprocess startup Pre<strong>page</strong> all or some of the <strong>page</strong>s a process will need, beforethey are referenced But if pre<strong>page</strong>d <strong>page</strong>s are unused, I/O and memory waswasted Assume s <strong>page</strong>s are pre<strong>page</strong>d and α of the <strong>page</strong>s is used• Is cost of s * α save <strong>page</strong>s <strong>fault</strong>s > or < than the cost of prepagings * (1- α) unnecessary <strong>page</strong>s?• α near zero prepaging loses
Page Size Page size selection must take into consideration: Fragmentation table size I/O overhead locality
TLB ReachTLB Reach - The amount of memory accessible from the TLBTLB Reach ~ (TLB Size) X (Page Size)Ideally, the working set of each process is stored in the TLB.Otherwise there is a high degree of <strong>page</strong> <strong>fault</strong>s.Increase the Page Size. This may lead to an increase infragmentation as not all applications require a large <strong>page</strong> sizeProvide Multiple Page Sizes. This allows applications that requirelarger <strong>page</strong> sizes the opportunity to use them without an increasein fragmentation.
Program StructureProgram structure Int[128,128] data; Each row is stored in one <strong>page</strong> Program 1for (j = 0; j
I/O interlockI/O Interlock – Pages must sometimes be locked into memoryConsider I/O. Pages that are used for copying a file from adevice must be locked from being selected for eviction by a<strong>page</strong> replacement algorithm.
Reason Why Frames Used For I/O Must BeIn Memory
Operating System Examples Windows XP Solaris
Windows XPUses demand paging with clustering. Clustering brings in<strong>page</strong>s surrounding the <strong>fault</strong>ing <strong>page</strong>.Processes are assigned working set minimum and workingset maximumWorking set minimum is the minimum number of <strong>page</strong>s theprocess is guaranteed to have in memory
Windows XP (cont.)A process may be assigned as many <strong>page</strong>s up to its workingset maximum. < working-set max : <strong>page</strong> <strong>fault</strong>s →allocation from free <strong>page</strong>s. >= working set max: <strong>page</strong> <strong>fault</strong>s →local <strong>page</strong> replacementWhen the amount of free memory in the system falls below athreshold, automatic working set trimming is performed torestore the amount of free memory.Working set trimming removes <strong>page</strong>s from processes that have<strong>page</strong>s in excess of their working set minimum.
SolarisMaintains a list of free <strong>page</strong>s to assign <strong>fault</strong>ing processesLotsfree – threshold parameter (amount of free memory) to beginpagingDesfree – threshold parameter to increasing pagingMinfree – threshold parameter to being swappingPaging is performed by <strong>page</strong>out process
Solaris (cont.)Pageout scans <strong>page</strong>s using modified clock algorithmScanrate is the rate at which <strong>page</strong>s are scanned. This rangesfrom slowscan to fastscanPageout is called more frequently depending upon the amountof free memory available
Solaris 2 Page Scanner
END OF CHAPTER 9