Unit 5
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • Thank you very much!
    You just saved me!
    :D
    (y)
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
1,234
On Slideshare
1,234
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
16
Comments
1
Likes
2

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. UNIT VMEMORY MANAGEMENTBasics of memory management,Swapping, Virtual memory,Page replacement algorithm,FIFO,second chance PR, clock PR, Least recently used, working set PR,WS clock PR,Design issues for Paging systems,Design issues for Paging systems,OS involvement with paging,page fault handling,Segmentation
  • 2. MEMORY MANAGEMENTObjective of ChapterWhat is Memory.Students are able to understand Concept of Swapping .Concept of Paging ,Segmentation.Concept of Paging ,Segmentation.Different type of Page replacement algorithms
  • 3. BASICS OF MEMORY MANAGEMENTMemory management is the function of a computeroperating system responsible for managing the computersprimary memory.The memory management function keeps track of the statusof each memory location, either allocated or free.of each memory location, either allocated or free.Something is needed to manage the movement of programsbetween O.S. and memory. The part of the operating systemthat does this is known as the memory manager.
  • 4. MEMORY MANAGEMENT REQUIREMENTSRelocationProtectionSharingLogical OrganizationPhysical Organization
  • 5. BASIC MEMORY MANAGEMENTWithout swapingMonoprogrammingMultiprogramming with fixed partitionsWith swaping and paging
  • 6. MONOPROGRAMMINGSimplest scheme possibleOnly one program will be executed at a timeThe OS copies the program from disk to memory andexecutes it. Once finished, the OS is ready to accept a newexecutes it. Once finished, the OS is ready to accept a newcommand from the userNew commands overwrite the old program in memory withthe new
  • 7. MONOPROGRAMMINGThree configurationsa) is rarely used anymoreb) is in use by MP3 players, Palm devicesc) was the model of early PCs, eg: DOS
  • 8. MULTIPROGRAMMING WITH FIXED PARTITIONSIt support multiprogrammingThis scheme based on contiguousallocation.memory is divided in to fixednumber of partitionsOperatingsystemp1number of partitionsEach partition is of fixed size.each partition can accommodateone program for execution.p2p3p4Fixed-size partitioning
  • 9. PLACEMENT ALGORITHMThe most common strategies to allocate free partition toprogramFirst-fit.Best-fit.Worst-fit.Worst-fit.First-fit:- allocates the first free partition large enough toaccommodate the process.Best-fit:- allocates the smallest free partition free partitionWorst-fit:-allocates the largest free partition free partition .
  • 10. MULTIPROGRAMMING WITH DYNAMIC PARTITIONIn dynamic partition, neither the size nor the number ofdynamically allocated partitions can be fixed.The memory manager continues creating partitions untilall physical memory exhausted.The main problem with fixed partition is wastage ofThe main problem with fixed partition is wastage ofmemory by programs are smaller than their partitions,this is also known as internal fragmentation.With dynamic partitioning the number and size ofpartition is vary. This improves utilization of memory
  • 11. SWAPPINGSwapping a process consists of bringing the process into memory,from disk, in its entirety. The process is executed for some timeand then placed back on the disk
  • 12. VIRTUAL MEMORYTechniques that automatically move program and datablokes in to the physical main memory when they arerequired for execution are called virtual memorytechnique.technique.
  • 13. WHAT IS PAGINGPaging is a memory management technique whichbasically makes use of part of your hard drive as it itwere RAM, thus creating "virtual memory.“It is useful if your PC doesnt have enough RAM to storeeverything on, so it can temporarily transfer some of itto the hard drive and store it until you need it again.to the hard drive and store it until you need it again.. When paging is used, the processor divides the linearaddress space into fixed-size pages (of 4KBytes, 2MBytes, or 4 MBytes in length) that can be mapped intophysical memory and/or disk storage.A special hardware unit,called the memorymanagement unit (MMU) translates virtual address intophysical address.
  • 14. ROLL OF MMU
  • 15. PAGINGAddress generated by CPU divided into:Page number (p) - index to page tablepage table contains base address of each page in physical memory(frame)Page offset (d) - offset into page/frameCPU p dpage tableff dphysicalmemory
  • 16. PAGING EXAMPLEPage 0Page size 4 bytesMemory size 32 bytes (8 pages)10Page 0Page 20123Page 1Page 2Page 3437123Page TableLogicalMemoryPage 1Page 3PhysicalMemory4567
  • 17. PAGING EXAMPLEPage0Page101000000010100110000010100110 0 1 0 1 1PageOffsetFramePage2Page3110010011011Page TableLogicalMemory100101110111PhysicalMemory100101110111
  • 18. Physical AddressOffsetHOW TO IMPLEMENT PAGING?Page Table (One per process)OffsetVirtualPage #Virtual Address:AccessError>PageTableSizePageTablePtr page #0page #2page #3page #4page #5V,Rpage #1 V,RV,R,WV,R,WNV,R,Wpage #1 V,RCheck PermAccessErrorPhysicalPage #Page Table (One per process)Resides in physical memoryContains physical page and permission for each virtual pagePermissions include: Valid bits, Read, Write, etcVirtual address mappingOffset from Virtual address copied to Physical AddressExample: 10 bit offset ⇒ 1024-byte pagesVirtual page # is all remaining bitsExample for 32-bits: 32-10 = 22 bits, i.e. 4 million entriesPhysical page # copied from table into physical addressCheck Page Table bounds and permissions
  • 19. PAGING HARDWAREaddress space 2mpage offset 2npage number 2m-nphsicalmemory2m bytespage numberppage offsetdm-n n•note: not losing any bytes!
  • 20. PAGING EXAMPLEConsider:Physical memory = 128 bytesPhysical address space = 8 framesHow many bits in an address?How many bits for page number?How many bits for page number?How many bits for page offset?Can a logical address space have only 2 pages? How bigwould the page table be?
  • 21. ANOTHER PAGING EXAMPLEConsider:8 bits in an address3 bits for the frame/page numberHow many bytes (words) of physical memory?How many frames are there?How many frames are there?How many bytes is a page?How many bits for page offset?If a process’ page table is 12 bits, how manylogical pages does it have?
  • 22. PAGE TABLE EXAMPLEPage 0APage 0B0123Page 0Page 1Process B3701Page Tablepage number page offsetb=71401Page TablePage 0Page 1Process APage 1APage 1BPhysicalMemory4567ppage offsetdm-n=3 n=4
  • 23. PAGING TRADEOFFSAdvantagesno external fragmentation (no compaction)relocation (now pages, before were processes)Disadvantagesinternal fragmentationconsider: 2048 byte pages, 72,766 byte proc35 pages + 1086 bytes = 962 bytes35 pages + 1086 bytes = 962 bytesavg: 1/2 page per processsmall pages!overheadpage table / process (context switch + space)lookup (especially if page to disk)
  • 24. IMPLEMENTATION OF PAGE TABLEPage table kept in main memoryPage Table Base Register (PTBR)Page 0Page 11401Page 1Page 00121 4PTBRPage 1 41Page TableLogicalMemoryPhysicalMemoryPage 023PTBR• Page Table Length• Two memory accesses per data/inst access.– Solution? Associative Registers
  • 25. ASSOCIATIVE REGISTERSCPUp df dpagenumberframenumberhitlogicaladdress10-20% mem timepage tablefphysicalmemoryassociativeregistersmissphysicaladdress(Intel P3 has 32 entries)(Intel P4 has 128 entries)
  • 26. PAGE REPLACEMENT ALGORITHMWhen the page fault occurs i.e. required page is not inmemory during execution , the o.s has to choose a pageto move from memory to make room for the incomingpage.
  • 27. PROCEDURE OF PAGE REPLACEMENT1]Page to be removed is selected.2]If the page to be removed is modified it must2]If the page to be removed is modified it mustbe written to the disk to bring the disk copy up todate3]If the page to be removed has not beenmodified , it can be overwritten .
  • 28. TYPES OF PAGE REPLACEMENTALGORITHM1) FIFO( First in first out) page replacement algorithm.2) Second chance page replacement.3) Clock page replacement.4) Least Recently Used page replacement5) Working set page replacement6) Working Set Clock page replacement.
  • 29. Suppose we have 3 page frames, 4 virtual pages, andfollowing reference stream:A B C A B D A D B C BConsider FIFO Page replacement:EXAMPLE: FIFOCDABCBDADBACBA1Ref:Page:FIFO: 7 faults.When referencing D, replacing A is bad choice, since need Aagain right awayCBADCBA321
  • 30. 2.SECOND CHANCE PAGE REPLACEMENT1] It is modified version of FIFO algo.2] It avoids the problem of throwing out a heavily usedpage.3] An additional bit R (read) is attached to each page inmemory.memory.4] Whenever a page is accessed R bit is set to 1.5]If it is 0,page is old and unused and replacedimmediately6] If it is 1,it is cleared and inserted back in the FIFOqueue.
  • 31. SECOND CHANCE ILLUSTRATIONMax page table size 4Page B arrivesPage A arrivesAccess page APage D arrivesPage C arrivesB u:0first loaded pageA u:1 D u:0 C u:0last loaded page
  • 32. SECOND CHANCE ILLUSTRATIONMax page table size 4Page B arrivesPage A arrivesAccess page APage D arrivesPage C arrivesPage F arrivesB u:0first loaded pageA u:1 D u:0 C u:0last loaded page
  • 33. SECOND CHANCE ILLUSTRATIONMax page table size 4Page B arrivesPage A arrivesAccess page APage D arrivesPage C arrivesPage F arrivesA u:1first loaded pageD u:0 C u:0 F u:0last loaded page
  • 34. SECOND CHANCE ILLUSTRATIONMax page table size 4Page B arrivesPage A arrivesAccess page APage D arrivesPage C arrivesPage F arrivesAccess page DPage E arrivesA u:1first loaded pageD u:1 C u:0 F u:0last loaded page
  • 35. SECOND CHANCE ILLUSTRATIONMax page table size 4Page B arrivesPage A arrivesAccess page APage D arrivesPage C arrivesPage F arrivesAccess page DPage E arrivesD u:1first loaded pageC u:0 F u:0 A u:0last loaded page
  • 36. SECOND CHANCE ILLUSTRATIONMax page table size 4Page B arrivesPage A arrivesAccess page APage D arrivesPage C arrivesPage F arrivesAccess page DPage E arrivesC u:0first loaded pageF u:0 A u:0 D u:0last loaded pageE u:0
  • 37. DISADVANTAGE OF SECOND CHANCE PAGEREPLACEMENT ALGORITHMIt is unnecessarily inefficient because it isconstantly moving pages around on its listconstantly moving pages around on its list
  • 38. CLOCK REPLACEMENT ILLUSTRATIONMax page table size 4Invariant: point at oldest pagePage B arrivesB u:0
  • 39. CLOCK REPLACEMENT ILLUSTRATIONMax page table size 4Invariant: point at oldest pagePage B arrivesPage A arrivesAccess page AB u:0A u:0
  • 40. CLOCK REPLACEMENT ILLUSTRATIONMax page table size 4Invariant: point at oldest pagePage B arrivesPage A arrivesAccess page APage D arrivesB u:0A u:1D u:0
  • 41. CLOCK REPLACEMENT ILLUSTRATIONMax page table size 4Invariant: point at oldest pagePage B arrivesPage A arrivesAccess page APage D arrivesPage C arrivesB u:0A u:1D u:0C u:0
  • 42. CLOCK REPLACEMENT ILLUSTRATIONMax page table size 4Invariant: point at oldest pageB u:0Page B arrivesPage A arrivesAccess page APage D arrivesPage C arrivesPage F arrivesF u:0A u:1D u:0C u:0
  • 43. Max page table size 4Invariant: point at oldest pagePage B arrivesPage A arrivesCLOCK REPLACEMENT ILLUSTRATIONC u:0E u:0Page A arrivesAccess page APage D arrivesPage C arrivesPage F arrivesAccess page DPage E arrivesA u:1A u:0D u:1D u:0F u:0
  • 44. NTH CHANCE VERSION OF CLOCK ALGORITHMNth chance algorithm: Give page N chancesOS keeps counter per page: # sweepsOn page fault, OS checks use bit:1⇒clear use and also clear counter (used in last sweep)0⇒increment counter; if count=N, replace pageMeans that clock hand has to sweep by N times without pagebeing used before page is replacedHow do we pick N?Why pick large N? Better approx to LRUWhy pick large N? Better approx to LRUIf N ~ 1K, really good approximationWhy pick small N? More efficientOtherwise might have to look a long way to find free pageWhat about dirty pages?Takes extra overhead to replace a dirty page, so give dirty pagesan extra chance before replacing?Common approach:Clean pages, use N=1Dirty pages, use N=2 (and write back to disk when N=1)
  • 45. CLOCK ALGORITHMS: DETAILSWhich bits of a PTE entry are useful to us?Use: Set when page is referenced; cleared by clock algorithmModified: set when page is modified, cleared when page written todiskValid: ok for program to reference this pageRead-only: ok for program to read page, but not modifyFor example for catching modifications to code pages!For example for catching modifications to code pages!Do we really need hardware-supported “modified” bit?No. Can emulate it (BSD Unix) using read-only bitInitially, mark all pages as read-only, even data pagesOn write, trap to OS. OS sets software “modified” bit, and marks page asread-write.Whenever page comes back in from disk, mark read-only
  • 46. CLOCK ALGORITHMS DETAILS (CONTINUED)Do we really need a hardware-supported “use” bit?No. Can emulate it using “invalid” bit:Mark all pages as invalid, even if in memoryOn read to invalid page, trap to OSOn read to invalid page, trap to OSOS sets use bit, and marks page read-onlyWhen clock hand passes by, reset use bit and mark page asinvalid again
  • 47. LEAST RECENTLY USED PAGE REPLACEMENT ALGORITHMLRU replaces the page in memory that has not beenused for the longest period of time.This algo. goes well with the principle of locality.A page that has not been used for a long time is leastlikely to be referenced in the near future.likely to be referenced in the near future.This algo. can be implemented by maintaining thebackward distance of each page .When a page is referenced its backward distance is setto 0.Backward distance of other pages is incremented by 1.
  • 48. LEAST RECENTLY USED PAGE REPLACEMENT ALGORITHMLRU replaces the page in memory that has not beenused for the longest period of time.This algo. goes well with the principle of locality.A page that has not been used for a long time is leastlikely to be referenced in the near future.likely to be referenced in the near future.This algo. can be implemented by maintaining thebackward distance of each page .When a page is referenced its backward distance is setto 0.Backward distance of other pages is incremented by 1.
  • 49. Consider the following: A B C D A B C D A B C DLRU Performs as follows (same as FIFO here):DWHEN WILL LRU PERFORM BADLY?CBADCBADCBACBADCBADCBA D321Ref:Page:Every reference is a page fault!MIN Does much better:DABC3BCDCBACBADCBADCBA D321Ref:Page:
  • 50. For exampleConsider the reference string2 3 2 1 5 2 4 5 3 2 5 2No. of page frames =3These are the page frame::
  • 51. REFERENCE STRINGStep 1:2 3 2 1 5 2 4 5 3 2 5 22
  • 52. Step 2:It causes page faults and brought into emptyframes.At this point backward dist. of page 2=1Backward dist of 3=0Backward dist of 3=02 3 2 1 5 2 4 5 3 2 5 22 23
  • 53. step 3:2 3 2 1 5 2 4 5 3 2 5 22 2323231251251254254253253253253
  • 54. WORKING SET PAGE REPLACEMENTALGORITHMWorking set: The set of process that a process is currently using.The working set algorithm is based on determining a working setand evicting any page that is not present in the working set.Locality of reference : A process reference only a small fraction ofits pages during any particular page of its execution.Thrashing : Result when a program causes page fault every fewinstruction, causing the page to pulled up from the memory. Noinstruction, causing the page to pulled up from the memory. No“real” useful work is being accomplished.Prepaging: Working set loaded into the memory before it beginsexecution.
  • 55. Information about onepage2084200311Time of last usePage table Reference bitPage reference duringThis tickPage not reference duringThis tick2003198012132014202020321620111110Time of last use
  • 56. Scan all pages examining R bit:if (R==1) settime of last use to current virtualtimeIf (R==0and age>stc)If (R==0and age>stc)remove this pageIf (R==0and age>stc)remember the smallest time
  • 57. WORKING SET CLOCK PAGE REPLACEMENTALGORITHMIt is based on the clock algorithm but also uses theworking set information.Due to its simplicity of implementation and goodperformance ,it is widely used.
  • 58. 162020322084R=0220420322084R=1New page20202014121319802003 20202014121319802003
  • 59. 16202032208416202032208420202014121319802003 20202014121319802003R=1
  • 60. ALGORITHM COMMENTFIFO Might throw out importantpagesSecond chance Big improvement over FIFOClock RealisticClock RealisticLRU Excellent but difficult toimplement exactlyWorking set Somewhat expensive toimplementWS clock Good efficient algorithm
  • 61. DESIGN ISSUES FOR PAGING SYSTEMSTHE WORKING SET:The reason that page faults decrease (and then stabilise) isbecause processes normally exhibit a locality of referenceAt a particular execution phase of the process it only uses asmall fraction of the pages available to the entire processsmall fraction of the pages available to the entire processThe set of pages that is currently being used is called itsworking setIf the entire working set is in memory then no page faults willoccurOnly when the process moves onto its next phase will pagefaults begin to occur again
  • 62. oIf the memory of the computer is not large enough to hold theentire working set, then pages will constantly be copied out to discand subsequently retrievedoThis drastically slows a process down and the process is said to bethrashingoThe paging system has to keep track of a processes’ working set sooThe paging system has to keep track of a processes’ working set sothat it can be loaded into memory before it is restarted.oThe approach is called the working set model (or prepaging). Its aim,as we have stated, is to avoid page faults being raised.oInformation about working set can be used by the operating systemto reduce page fault rate.
  • 63. LOCAL VERSUS GLOBAL ALLOCATION POLICIESA local alloction assigns a fixed number offrames to every running process.A global allocation dynamically allocates pageframe among runnable processes.frame among runnable processes.Global policies tend to work better.
  • 64. If you use a local policy and the working set grows, you can getthrashingSimilarly, if the working set shrinks, you wastememory.memory.With a global policy, though, you need to decidehow much memory to allocate to each process.
  • 65. PAGE SIZEWith large pages, we waste memory: on average, half of the lastpage isn’t usedWith small pages, we use a lot of memory for page tablesCall the average process size s and the page size p.Assume that each page table entry (and associated dataAssume that each page table entry (and associated datastructures) takes e bytesThe overhead o is o = (s/p)e + p/2To optimize for memory use, differentiate and set to 0:do/dp = −se/p2 + 1/2 = 0Best size: p = √2se
  • 66. OPERATING SYSTEM INVOLVEMENT WITH PAGINGFour times when OS involved with paging1. Process creation− determine program size− create page table2. Process execution2. Process execution− MMU reset for new process3. Page fault time− determine virtual address causing fault− swap target page out, needed page in4. Process termination time− release page table, pages
  • 67. PAGE FAULT HANDLING1. Hardware traps to kernel2. General registers saved3. OS determines which virtual page needed4. OS checks validity of address, seeks page frame5. If selected frame is modified, write it to disk5. If selected frame is modified, write it to disk6. OS schedules disk operation to bring new page in fromdisk7. Page tables updated8. Faulting process re scheduled for execution9. Registers restored10. Program continues
  • 68. SEGMENTATIONWHY USE SEGMENTS?Segmentation allows the programmer to viewmemory consisting of multiple address spacesor segments.or segments.A program can be divided into codesegment,stack segment and data segment.As segments are logically independent, aprogram segment can be changed orrecomplied at any time without affecting othersegments.
  • 69. oSegmentation can be used to protect aprogram from unauthorized access.program from unauthorized access.It is easy to share segments by differentprograms.Each process has its own segment table.
  • 70. SEGMENTATION ALLOWS EACH TABLE TOGROW OR SHRINK INDEPENDENTLY
  • 71. PROBLEMS WITH SEGMENTS?Maximum contiguous address space is limitedby segment size.For example, on the Intel 286, segments werelimited to 64K; that meant that no array couldlimited to 64K; that meant that no array couldbe larger than 64K bytes.
  • 72. COMPARISON OF PAGING AND SEGMENTATION