• Like
9.Virtual Memory
Upcoming SlideShare
Loading in...5
×
Uploaded on

Senthilkanth,MCA.. …

Senthilkanth,MCA..
The following ppt's full topic covers Operating System for BSc CS, BCA, MSc CS, MCA students..
1.Introduction
2.OS Structures
3.Process
4.Threads
5.CPU Scheduling
6.Process Synchronization
7.Dead Locks
8.Memory Management
9.Virtual Memory
10.File system Interface
11.File system implementation
12.Mass Storage System
13.IO Systems
14.Protection
15.Security
16.Distributed System Structure
17.Distributed File System
18.Distributed Co Ordination
19.Real Time System
20.Multimedia Systems
21.Linux
22.Windows

More in: Education , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
801
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
107
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Chapter 9: Virtual MemoryChapter 9: Virtual Memory
  • 2. 9.2 Silberschatz, Galvin and GagneOperating System ConceptsChapter 9: Virtual MemoryChapter 9: Virtual MemoryBackgroundDemand PagingProcess CreationPage ReplacementAllocation of FramesThrashingDemand SegmentationOperating System Examples
  • 3. 9.3 Silberschatz, Galvin and GagneOperating System ConceptsBackgroundBackgroundVirtual memory – separation of user logical memory fromphysical memory.Only part of the program needs to be in memory for execution.Logical address space can therefore be much larger thanphysical address space.Allows address spaces to be shared by several processes.Allows for more efficient process creation.Virtual memory can be implemented via:Demand pagingDemand segmentation
  • 4. 9.4 Silberschatz, Galvin and GagneOperating System ConceptsVirtual Memory That is Larger Than PhysicalVirtual Memory That is Larger Than PhysicalMemoryMemory⇒
  • 5. 9.5 Silberschatz, Galvin and GagneOperating System ConceptsVirtual-address SpaceVirtual-address Space
  • 6. 9.6 Silberschatz, Galvin and GagneOperating System ConceptsShared Library Using Virtual MemoryShared Library Using Virtual Memory
  • 7. 9.7 Silberschatz, Galvin and GagneOperating System ConceptsDemand PagingDemand PagingBring a page into memory only when it is neededLess I/O neededLess memory neededFaster responseMore usersPage is needed ⇒ reference to itinvalid reference ⇒ abortnot-in-memory ⇒ bring to memory
  • 8. 9.8 Silberschatz, Galvin and GagneOperating System ConceptsTransfer of a Paged Memory to Contiguous DiskTransfer of a Paged Memory to Contiguous DiskSpaceSpace
  • 9. 9.9 Silberschatz, Galvin and GagneOperating System ConceptsValid-Invalid BitValid-Invalid BitWith each page table entry a valid–invalid bit is associated(1 ⇒ in-memory, 0 ⇒ not-in-memory)Initially valid–invalid but is set to 0 on all entriesExample of a page table snapshot:During address translation, if valid–invalid bit in page table entry is 0 ⇒page fault1111000Frame # valid-invalid bitpage table
  • 10. 9.10 Silberschatz, Galvin and GagneOperating System ConceptsPage Table When Some Pages Are Not in MainPage Table When Some Pages Are Not in MainMemoryMemory
  • 11. 9.11 Silberschatz, Galvin and GagneOperating System ConceptsPage FaultPage FaultIf there is ever a reference to a page, first reference will trap toOS ⇒ page faultOS looks at another table to decide:Invalid reference ⇒ abort.Just not in memory.Get empty frame.Swap page into frame.Reset tables, validation bit = 1.Restart instruction: Least Recently Usedblock moveauto increment/decrement location
  • 12. 9.12 Silberschatz, Galvin and GagneOperating System ConceptsSteps in Handling a Page FaultSteps in Handling a Page Fault
  • 13. 9.13 Silberschatz, Galvin and GagneOperating System ConceptsWhat happens if there is no free frame?What happens if there is no free frame?Page replacement – find some page in memory, but notreally in use, swap it outalgorithmperformance – want an algorithm which will result inminimum number of page faultsSame page may be brought into memory several times
  • 14. 9.14 Silberschatz, Galvin and GagneOperating System ConceptsPerformance of Demand PagingPerformance of Demand PagingPage Fault Rate 0 ≤ p ≤ 1.0if p = 0 no page faultsif p = 1, every reference is a faultEffective Access Time (EAT)EAT = (1 – p) x memory access+ p (page fault overhead+ [swap page out ]+ swap page in+ restart overhead)
  • 15. 9.15 Silberschatz, Galvin and GagneOperating System ConceptsDemand Paging ExampleDemand Paging ExampleMemory access time = 1 microsecond50% of the time the page that is being replaced has been modifiedand therefore needs to be swapped outSwap Page Time = 10 msec = 10,000 msecEAT = (1 – p) x 1 + p (15000)1 + 15000P (in msec)
  • 16. 9.16 Silberschatz, Galvin and GagneOperating System ConceptsProcess CreationProcess CreationVirtual memory allows other benefits during process creation:- Copy-on-Write- Memory-Mapped Files (later)
  • 17. 9.17 Silberschatz, Galvin and GagneOperating System ConceptsCopy-on-WriteCopy-on-WriteCopy-on-Write (COW) allows both parent and child processes toinitially share the same pages in memoryIf either process modifies a shared page, only then is the pagecopiedCOW allows more efficient process creation as only modified pagesare copiedFree pages are allocated from a pool of zeroed-out pages
  • 18. 9.18 Silberschatz, Galvin and GagneOperating System ConceptsPage ReplacementPage ReplacementPrevent over-allocation of memory by modifying page-fault serviceroutine to include page replacementUse modify (dirty) bit to reduce overhead of page transfers –only modified pages are written to diskPage replacement completes separation between logical memoryand physical memory – large virtual memory can be provided on asmaller physical memory
  • 19. 9.19 Silberschatz, Galvin and GagneOperating System ConceptsNeed For Page ReplacementNeed For Page Replacement
  • 20. 9.20 Silberschatz, Galvin and GagneOperating System ConceptsBasic Page ReplacementBasic Page Replacement1. Find the location of the desired page on disk2. 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 frame3. Read the desired page into the (newly) free frame. Update thepage and frame tables.4. Restart the process
  • 21. 9.21 Silberschatz, Galvin and GagneOperating System ConceptsPage ReplacementPage Replacement
  • 22. 9.22 Silberschatz, Galvin and GagneOperating System ConceptsPage Replacement AlgorithmsPage Replacement AlgorithmsWant lowest page-fault rateEvaluate algorithm by running it on a particular string ofmemory references (reference string) and computing thenumber of page faults on that stringIn all our examples, the reference string is1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
  • 23. 9.23 Silberschatz, Galvin and GagneOperating System ConceptsGraph of Page Faults Versus The Number ofGraph of Page Faults Versus The Number ofFramesFrames
  • 24. 9.24 Silberschatz, Galvin and GagneOperating System ConceptsFirst-In-First-Out (FIFO) AlgorithmFirst-In-First-Out (FIFO) AlgorithmReference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 53 frames (3 pages can be in memory at a time per process)4 framesFIFO Replacement – Belady’s Anomalymore frames ⇒ more page faults1231234125349 page faults12312351245 10 page faults44 3
  • 25. 9.25 Silberschatz, Galvin and GagneOperating System ConceptsFIFO Page ReplacementFIFO Page Replacement
  • 26. 9.26 Silberschatz, Galvin and GagneOperating System ConceptsFIFO Illustrating Belady’s AnomalyFIFO Illustrating Belady’s Anomaly
  • 27. 9.27 Silberschatz, Galvin and GagneOperating System ConceptsOptimal AlgorithmOptimal AlgorithmReplace page that will not be used for longest period of time4 frames example1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5How do you know this?Used for measuring how well your algorithm performs12346 page faults4 5
  • 28. 9.28 Silberschatz, Galvin and GagneOperating System ConceptsOptimal Page ReplacementOptimal Page Replacement
  • 29. 9.29 Silberschatz, Galvin and GagneOperating System ConceptsLeast Recently Used (LRU)Least Recently Used (LRU)AlgorithmAlgorithmReference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5Counter implementationEvery page entry has a counter; every time page is referencedthrough this entry, copy the clock into the counterWhen a page needs to be changed, look at the counters todetermine which are to change123544 35
  • 30. 9.30 Silberschatz, Galvin and GagneOperating System ConceptsLRU Page ReplacementLRU Page Replacement
  • 31. 9.31 Silberschatz, Galvin and GagneOperating System ConceptsLRU Algorithm (Cont.)LRU Algorithm (Cont.)Stack implementation – keep a stack of page numbers in a doublelink form:Page referenced: move it to the top requires 6 pointers to be changedNo search for replacement
  • 32. 9.32 Silberschatz, Galvin and GagneOperating System ConceptsUse Of A Stack to Record The Most Recent PageUse Of A Stack to Record The Most Recent PageReferencesReferences
  • 33. 9.33 Silberschatz, Galvin and GagneOperating System ConceptsLRU Approximation AlgorithmsLRU Approximation AlgorithmsReference bitWith each page associate a bit, initially = 0When page is referenced bit set to 1Replace the one which is 0 (if one exists). We do not know theorder, however.Second chanceNeed reference bitClock replacementIf page to be replaced (in clock order) has reference bit = 1then: set reference bit 0 leave page in memory replace next page (in clock order), subject to same rules
  • 34. 9.34 Silberschatz, Galvin and GagneOperating System ConceptsSecond-Chance (clock) Page-ReplacementSecond-Chance (clock) Page-ReplacementAlgorithmAlgorithm
  • 35. 9.35 Silberschatz, Galvin and GagneOperating System ConceptsCounting AlgorithmsCounting AlgorithmsKeep a counter of the number of references that have beenmade to each pageLFU Algorithm: replaces page with smallest countMFU Algorithm: based on the argument that the page withthe smallest count was probably just brought in and has yetto be used
  • 36. 9.36 Silberschatz, Galvin and GagneOperating System ConceptsAllocation of FramesAllocation of FramesEach process needs minimum number of pagesExample: IBM 370 – 6 pages to handle SS MOVE instruction:instruction is 6 bytes, might span 2 pages2 pages to handle from2 pages to handle toTwo major allocation schemesfixed allocationpriority allocation
  • 37. 9.37 Silberschatz, Galvin and GagneOperating System ConceptsFixed AllocationFixed AllocationEqual allocation – For example, if there are 100 frames and 5processes, give each process 20 frames.Proportional allocation – Allocate according to the size of processmSspamsSpsiiiiii×===∑==forallocationframesofnumbertotalprocessofsize5964137127564137101271064212≈×=≈×====aassmi
  • 38. 9.38 Silberschatz, Galvin and GagneOperating System ConceptsPriority AllocationPriority AllocationUse a proportional allocation scheme using priorities ratherthan sizeIf process Pi generates a page fault,select for replacement one of its framesselect for replacement a frame from a process withlower priority number
  • 39. 9.39 Silberschatz, Galvin and GagneOperating System ConceptsGlobal vs. Local AllocationGlobal vs. Local AllocationGlobal replacement – process selects a replacementframe from the set of all frames; one process can take aframe from anotherLocal replacement – each process selects from only itsown set of allocated frames
  • 40. 9.40 Silberschatz, Galvin and GagneOperating System ConceptsThrashingThrashingIf a process does not have “enough” pages, the page-fault rate isvery high. This leads to:low CPU utilizationoperating system thinks that it needs to increase the degree ofmultiprogramminganother process added to the systemThrashing ≡ a process is busy swapping pages in and out
  • 41. 9.41 Silberschatz, Galvin and GagneOperating System ConceptsThrashing (Cont.)Thrashing (Cont.)
  • 42. 9.42 Silberschatz, Galvin and GagneOperating System ConceptsDemand Paging and ThrashingDemand Paging and ThrashingWhy does demand paging work?Locality modelProcess migrates from one locality to anotherLocalities may overlapWhy does thrashing occur?Σ size of locality > total memory size
  • 43. 9.43 Silberschatz, Galvin and GagneOperating System ConceptsLocality In A Memory-Reference PatternLocality In A Memory-Reference Pattern
  • 44. 9.44 Silberschatz, Galvin and GagneOperating System ConceptsWorking-Set ModelWorking-Set Model∆ ≡ working-set window ≡ a fixed number of page referencesExample: 10,000 instructionWSSi (working set of Process Pi) =total number of pages referenced in the most recent ∆ (variesin time)if ∆ too small will not encompass entire localityif ∆ too large will encompass several localitiesif ∆ = ∞ ⇒ will encompass entire programD = Σ WSSi ≡ total demand framesif D > m ⇒ ThrashingPolicy if D > m, then suspend one of the processes
  • 45. 9.45 Silberschatz, Galvin and GagneOperating System ConceptsWorking-set modelWorking-set model
  • 46. 9.46 Silberschatz, Galvin and GagneOperating System ConceptsKeeping Track of the Working SetKeeping Track of the Working SetApproximate with interval timer + a reference bitExample: ∆ = 10,000Timer interrupts after every 5000 time unitsKeep in memory 2 bits for each pageWhenever a timer interrupts copy and sets the values of allreference bits to 0If one of the bits in memory = 1 ⇒ page in working setWhy is this not completely accurate?Improvement = 10 bits and interrupt every 1000 time units
  • 47. 9.47 Silberschatz, Galvin and GagneOperating System ConceptsPage-Fault Frequency SchemePage-Fault Frequency SchemeEstablish “acceptable” page-fault rateIf actual rate too low, process loses frameIf actual rate too high, process gains frame
  • 48. 9.48 Silberschatz, Galvin and GagneOperating System ConceptsMemory-Mapped FilesMemory-Mapped FilesMemory-mapped file I/O allows file I/O to be treated as routinememory access by mapping a disk block to a page in memoryA file is initially read using demand paging. A page-sized portion ofthe file is read from the file system into a physical page.Subsequent reads/writes to/from the file are treated as ordinarymemory accesses.Simplifies file access by treating file I/O through memory ratherthan read() write() system callsAlso allows several processes to map the same file allowing thepages in memory to be shared
  • 49. 9.49 Silberschatz, Galvin and GagneOperating System ConceptsMemory Mapped FilesMemory Mapped Files
  • 50. 9.50 Silberschatz, Galvin and GagneOperating System ConceptsMemory-Mapped Files in JavaMemory-Mapped Files in Javaimport java.io.*;import java.nio.*;import java.nio.channels.*;public class MemoryMapReadOnly{// Assume the page size is 4 KBpublic static final int PAGE SIZE = 4096;public static void main(String args[]) throws IOException {RandomAccessFile inFile = new RandomAccessFile(args[0],"r");FileChannel in = inFile.getChannel();MappedByteBuffer mappedBuffer =in.map(FileChannel.MapMode.READ ONLY, 0, in.size());long numPages = in.size() / (long)PAGE SIZE;if (in.size() % PAGE SIZE > 0)++numPages;
  • 51. 9.51 Silberschatz, Galvin and GagneOperating System ConceptsMemory-Mapped Files in Java (cont)Memory-Mapped Files in Java (cont)// we will "touch" the first byte of every pageint position = 0;for (long i = 0; i < numPages; i++) {byte item = mappedBuffer.get(position);position += PAGE SIZE;}in.close();inFile.close();}}The API for the map() method is as follows:map(mode, position, size)
  • 52. 9.52 Silberschatz, Galvin and GagneOperating System ConceptsOther Issues -- PrepagingOther Issues -- PrepagingPrepagingTo reduce the large number of page faults that occurs at processstartupPrepage all or some of the pages a process will need, beforethey are referencedBut if prepaged pages are unused, I/O and memory was wastedAssume s pages are prepaged and α of the pages is used Is cost of s * α save pages faults > or < than the cost ofprepagings * (1- α) unnecessary pages? α near zero ⇒ prepaging loses
  • 53. 9.53 Silberschatz, Galvin and GagneOperating System ConceptsOther Issues – Page SizeOther Issues – Page SizePage size selection must take into consideration:fragmentationtable sizeI/O overheadlocality
  • 54. 9.54 Silberschatz, Galvin and GagneOperating System ConceptsOther Issues – TLB ReachOther Issues – 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 page faults.Increase the Page Size. This may lead to an increase infragmentation as not all applications require a large page sizeProvide Multiple Page Sizes. This allows applications thatrequire larger page sizes the opportunity to use them withoutan increase in fragmentation.
  • 55. 9.55 Silberschatz, Galvin and GagneOperating System ConceptsOther Issues – Program StructureOther Issues – Program StructureProgram structureInt[128,128] data;Each row is stored in one pageProgram 1for (j = 0; j <128; j++)for (i = 0; i < 128; i++)data[i,j] = 0;128 x 128 = 16,384 page faultsProgram 2for (i = 0; i < 128; i++)for (j = 0; j < 128; j++)data[i,j] = 0;128 page faults
  • 56. 9.56 Silberschatz, Galvin and GagneOperating System ConceptsOther Issues – I/O interlockOther Issues – I/O interlockI/O Interlock – Pages must sometimes be locked intomemoryConsider I/O. Pages that are used for copying a file froma device must be locked from being selected for evictionby a page replacement algorithm.
  • 57. 9.57 Silberschatz, Galvin and GagneOperating System ConceptsReason Why Frames Used For I/O Must Be InReason Why Frames Used For I/O Must Be InMemoryMemory
  • 58. 9.58 Silberschatz, Galvin and GagneOperating System ConceptsOperating System ExamplesOperating System ExamplesWindows XPSolaris
  • 59. 9.59 Silberschatz, Galvin and GagneOperating System ConceptsWindows XPWindows XPUses demand paging with clustering. Clustering brings in pagessurrounding the faulting page.Processes are assigned working set minimum and workingset maximumWorking set minimum is the minimum number of pages the processis guaranteed to have in memoryA process may be assigned as many pages up to its working setmaximumWhen the amount of free memory in the system falls below athreshold, automatic working set trimming is performed torestore the amount of free memoryWorking set trimming removes pages from processes that havepages in excess of their working set minimum
  • 60. 9.60 Silberschatz, Galvin and GagneOperating System ConceptsSolarisSolarisMaintains a list of free pages to assign faulting processesLotsfree – threshold parameter (amount of free memory) to beginpagingDesfree – threshold parameter to increasing pagingMinfree – threshold parameter to being swappingPaging is performed by pageout processPageout scans pages using modified clock algorithmScanrate is the rate at which pages are scanned. This ranges fromslowscan to fastscanPageout is called more frequently depending upon the amount offree memory available
  • 61. 9.61 Silberschatz, Galvin and GagneOperating System ConceptsSolaris 2 Page ScannerSolaris 2 Page Scanner
  • 62. End of Chapter 9End of Chapter 9