Memory ManagementWalking towards the futureProf. Hemang Kothari
Memory Management (why)• Memory is cheap today, and getting cheaperBut applications are demanding more and more memory, th...
Memory needs to be allocated to ensure areasonable supply of ready processes to consumeavailable processor time.Prof. Hema...
Memory Management Requirement• Relocation• Protection• Sharing• Logical organisation• Physical organisationProf. Hemang Ko...
Relocation• Programmer does not know where the program will be placed inmemory when it is executed• While the program is e...
Memory Management TermsProf. Hemang Kothari
Addressing RequirementProf. Hemang Kothari
Protection - Security• Processes should not be able to reference memory locations inanother process without permission• Im...
Sharing• Allow several processes to access the same portion of memory• Better to allow each process access to the same cop...
Logical Organization• Programs are written in modules• Modules can be written and compiled independently• Different degree...
Physical Organization• Memory available for a program plus its data may be insufficient• Overlaying allows various modules...
Picture we want to paintProf. Hemang Kothari
Essence of memory managementThe task of moving information between the two levels ofmemoryProf. Hemang Kothari
Basic Memory ManagementMonoprogramming without Swapping or PagingThree simple ways of organizing memory- an operating syst...
Fixed Partitioning• Any process whose size is less than or equal to the partition size canbe loaded into an available part...
Prof. Hemang Kothari
Placement Algorithm With Partition• Equal-size partitions• Because all partitions are of equal size, it does not matter wh...
Prof. Hemang Kothari
Dynamic Partition• Partitions are of variable length and number• Process is allocated exactly as much memory as required• ...
Prof. Hemang Kothari
Dynamic Storage Allocation Problem• First-fit: Allocate the first hole that is big enough.• Best-fit: Allocate the smalles...
Dynamic Partitioning• Next-fit• Scans memory from the location of the last placement• More often allocate a block of memor...
Best Fit Vs. First Fit• Memory sizes 1300 and 1200• Requests: 1000, 1100, 250• Request First-Fit Best-Fit• 1300, 1200 1300...
Best Fit Vs. First Fit• Memory sizes 1300 and 1200• Requests: 1100, 1050, 250• Request First-Fit Best-Fit• 1300, 1200 1300...
Placement Algorithm• Used to decide which freeblock to allocate to aprocess of 16MB.• Goal: reduce usage ofcompaction proc...
Final Comments• First-fit favors allocation near the beginning: tends to create lessfragmentation then Next-fit.• Next-fit...
Final Comments - Fragmentation• There are really two types of fragmentation:1. Internal Fragmentation –allocated memory ma...
HOME WORK• Consider a swapping system in which memory consists of thefollowing hole sizes in memory order: 10 KB, 4 KB, 20...
GTU -2011Given memory partition of 100K, 500K, 200K, 300K, and 600K inorder, How would each of the First-fit, Best-fit and...
Assume that the list of holes in a variable partitions memory system contains thefollowing entries (in the given order): 1...
Prof. Hemang Kothari
Prof. Hemang Kothari
Answer :Total internal fragmentation for first-fit =640KBTotal internal fragmentation for best fit = 275KBTotal internal f...
MMUProf. Hemang Kothari
Virtual or Logical Memory• The basic idea behind virtual memory is that the combined size of theprogram, data, and stack m...
Case Study• We have a computer that can generate 16-bit addresses, from 0 up to64K. These are the virtual addresses.• This...
Paging• Divide logical memory into blocks ofsame size called pages• Divide physical memory into fixed-sizedblocks called f...
Case Study – Continue• In this example pages are 4 KB, but page sizes from 512 bytes to 64 KBhave been used in real system...
• When the program tries to access address0, for example, using the instruction• MOV REG,0• virtual address 0 is sent to t...
Address TranslationProf. Hemang Kothari
Prof. Hemang Kothari
Page Fault• What happens if the program tries to use an unmapped page, forexample, by using the instruction• MOV REG,32780...
Processes & Frames & Page FaultA.0A.1A.2A.3B.0B.1B.2C.0C.1C.2C.3D.0D.1D.2D.3D.4Prof. Hemang Kothari
Page Table of ProcessProf. Hemang Kothari
Summary of Mapping• The virtual address is split into a virtual page number (high-order bits)and an offset (low-order bits...
Review• What is the difference between a physical address and a virtualaddress?Answer:Real memory uses physical addresses....
ReviewConversion Code - ChartDECIMAL0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15HEX 0 1 2 3 4 5 6 7 8 9 A B C D E FBINARY0000 000...
Review• Give the physical addresscorresponding to each of thefollowing virtual addresses:(a) 20(b) 4100(c) 8300Answer:(a) ...
Review Continue• A machine has 48-bit virtual addresses and 32-bit physicaladdresses. Pages are 8 KB. How many entries are...
Page TablePurpose : map virtual pages onto page frames•Major issues to be faced1.The page table can be extremely large2.Th...
1. Page Table is extremely large• Modern computers use virtual addresses of at least 32 bits.With, say, a 4-KB page size, ...
2. Mapping must be fast• The second point is a consequence of the fact that the virtual-to-physical mapping must be done o...
Solution - Multilevel Page Table• Multilevel page tables avoid keeping one huge page table in memory all thetime: this wor...
Prof. Hemang Kothari
Review of Multilevel Page Table• Assume a 32 bit system, with 2-level page table (page size is 4KB,|p1|=|p2|=10bits, |offs...
Review• We use the following scheme:• The 12 least significant digits in this address, allow access for 212 bytes – 4Kb.• ...
Two-level Page Tables (cont.)Ex. Given 32 bit virtual address 00403004 (hex) = 4,206,596 (dec)converting to binary we have...
Top-level page table: 102310...1Corresponds to all possiblevirtual addresses with 32bitaddresses: 0 -4,294,967,295(dec)Cor...
Reviewpage numberpageoffsetp1 p2 d10 10 12Top-levelpage table012341023 409532 bit virtual address, 4K pages, lookup of0x00...
Structure of Page Table Entry•If a referenced page is not in memory, the present/absent bit will be zero, and apage fault ...
Implementation of Page Table• Page table is kept in main memory• Page-table base register (PTBR) points to the page table•...
Translation Lookaside Buffer (TLB)• It is a hardware. It’s a cache actually.• When a virtual address is generated by the C...
Paging Hardware With TLBProf. Hemang Kothari
Segmentation• Segmentation is a technique for breaking memory up into logicalpieces• Each “piece” is a grouping of related...
Segmentationlogical address spaceOS Code OS data OS stackP1 dataP1 codeprintfunctionP2 codeP2 dataProf. Hemang Kothari
Addressing Segments• User generates logical addresses• These addresses consist of a segment number and an offset into thes...
Addressing SegmentsS oo < limitlimit base+Physical Addresserroryesnosegment tablelogical addressProf. Hemang Kothari
Segmentation Hardware• Sounds very similar to paging• Big difference – segments can be variable in size• Most systems prov...
Logical AddressesProf. Hemang Kothari
PagingProf. Hemang Kothari
SegmentationProf. Hemang Kothari
Mix IT – Paging & Segmentation• A virtual address becomes a segment number, a page within thatsegment, and an offset withi...
offsetdescriptorlimit basesegment table+virtual address from userlinear addressPT directory page tablepage frameDir (PT-1)...
Motivation for Page Replacement• When a page fault occurs, the operating system must choosea page to remove from memory to...
FIFO•Treats page frames allocated to a process as a circularbuffer:• When the buffer is full, the oldest page is replaced....
Example - FIFOProf. Hemang Kothari
Belady’s (or FIFO) AnomalyCertain page reference patterns actually cause more page faults when number of page framesalloca...
FIFO page replacement algorithmDisadvantage:• The oldest page may be needed again soon• Some page may be important through...
Review - FIFO• If FIFO page replacement is used with four page frames and eightpages, how many page faults will occur with...
Optimal Page Replacement• The Optimal policy selects for replacement the page that will not beused for longest period of t...
ExampleProf. Hemang Kothari
One more example• Reference string : 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5• 4 frames example• How do you know future use? You...
How can we do better?• Need an approximation of how likely each frame is to beaccessed in the future• If we base this on p...
Page table: referenced and dirty bits• Each page table entry (and TLB entry!) has a• Referenced bit - set by TLB when page...
Not recently used page replacement alg.• Uses the Referenced Bit and the Dirty Bit• Initially, all pages have• Referenced ...
Not recently used page replacement alg.• When a page fault occurs...• Categorize each page...• Class 1: Referenced = 0 Dir...
Second chance page replacement alg.• An implementation of NRU based on FIFO• Pages kept in a linked list• Oldest is at the...
ExampleProf. Hemang Kothari
Clock algorithm (an implementation of NRU)• Maintain a circular list of pages in memory• Set a bit for the page when a pag...
Prof. Hemang Kothari
Least Recently Used (LRU)• Replaces the page that has not been referenced for thelongest time Or Evict the page that was u...
Example - LRUProf. Hemang Kothari
One more example• Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 58 page faults52431234125412531243Implementation● Eve...
Review - LRU• If LRU page replacement is used with four page frames and eightpages, how many page faults will occur with t...
Not frequently used algorithm (NFU)• Bases decision of frequency of use rather than recency• Associate a counter with each...
Not frequently used algorithm (NFU)• Problem:• Some page may be heavily used• ---> Its counter is large• The program’s beh...
Modified NFU with aging• Associate a counter with each page• On every clock tick, the OS looks at each page.• Shift the co...
Working set page replacement• Demand paging• Pages are only loaded when accessed• When process begins, all pages marked IN...
Working set page replacement• Thrashing• If you can’t get working set into memory, page faultsoccur every few instructions...
Working set page replacement• Based on prepaging (prefetching)• Load pages before they are needed• Main idea:• Try to iden...
Working set page replacement• The size of the working set:k (the time interval)Prof. Hemang Kothari
Working set page replacement• Idea:• Look back over the last T msec of time• Which pages were referenced?• This is the wor...
Working set page replacementProf. Hemang Kothari
WSClock page replacement algorithm-Imple.• All pages are kept in a circular list (ring)• As pages are added, they go into ...
WSClock page replacement algorithm• If Reference Bit = 0• If the age of the page is less than T...• This page is in the wo...
Thank YouProf. Hemang Kothari
Upcoming SlideShare
Loading in …5
×

Memory Management Unit

3,214 views

Published on

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,214
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
108
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • Animated slideSystem with a number of frames allocatedProcess A, stored on disk, consists of four pages. When it comes time to load this process, the operating system finds four free frames and loads the four pages of process A into the four frames.Process B, consisting of three pages, and process C, consisting of four pages, are subsequently loaded.Then process B is suspended and is swapped out of main memory. Later, all of the processes in main memory are blocked, and the operating system needs to bring in a new process, process D, which consists of five pages. The Operating System loads the pages into the available frames and updates the page table
  • Memory Management Unit

    1. 1. Memory ManagementWalking towards the futureProf. Hemang Kothari
    2. 2. Memory Management (why)• Memory is cheap today, and getting cheaperBut applications are demanding more and more memory, there isnever enough!• Memory Management, involves swapping blocks of data fromsecondary storage.• Memory I/O is slow compared to a CPU• The OS must cleverly time the swapping to maximize the CPU’sefficiencyProf. Hemang Kothari
    3. 3. Memory needs to be allocated to ensure areasonable supply of ready processes to consumeavailable processor time.Prof. Hemang Kothari
    4. 4. Memory Management Requirement• Relocation• Protection• Sharing• Logical organisation• Physical organisationProf. Hemang Kothari
    5. 5. Relocation• Programmer does not know where the program will be placed inmemory when it is executed• While the program is executing, it may be swapped to disk andreturned to main memory at a different location (relocated)• Memory references must be translated in the code to actual physicalmemory addressProf. Hemang Kothari
    6. 6. Memory Management TermsProf. Hemang Kothari
    7. 7. Addressing RequirementProf. Hemang Kothari
    8. 8. Protection - Security• Processes should not be able to reference memory locations inanother process without permission• Impossible to check absolute addresses at compile time• Must be checked at rum time• Memory protection requirement must be satisfied by the processor(hardware) rather than the operating system (software)• Operating system cannot anticipate all of the memory references aprogram will makeProf. Hemang Kothari
    9. 9. Sharing• Allow several processes to access the same portion of memory• Better to allow each process access to the same copy of the programrather than have their own separate copyProf. Hemang Kothari
    10. 10. Logical Organization• Programs are written in modules• Modules can be written and compiled independently• Different degrees of protection given to modules (read-only, execute-only)• Share modules among processesProf. Hemang Kothari
    11. 11. Physical Organization• Memory available for a program plus its data may be insufficient• Overlaying allows various modules to be assigned the same region of memory• Programmer does not know how much space will be availableProf. Hemang Kothari
    12. 12. Picture we want to paintProf. Hemang Kothari
    13. 13. Essence of memory managementThe task of moving information between the two levels ofmemoryProf. Hemang Kothari
    14. 14. Basic Memory ManagementMonoprogramming without Swapping or PagingThree simple ways of organizing memory- an operating system with one user processProf. Hemang Kothari
    15. 15. Fixed Partitioning• Any process whose size is less than or equal to the partition size canbe loaded into an available partition• If all partitions are full, the operating system can swap a process outof a partition• A program may not fit in a partition. The programmer must designthe program with overlays.• Main memory use is inefficient. Any program, no matter how small,occupies an entire partition. This is called internal fragmentation.Prof. Hemang Kothari
    16. 16. Prof. Hemang Kothari
    17. 17. Placement Algorithm With Partition• Equal-size partitions• Because all partitions are of equal size, it does not matter which partition isused• Unequal-size partitions• Can assign each process to the smallest partition within which it will fit• Queue for each partition• Processes are assigned in such a way as to minimize wasted memory within apartitionProf. Hemang Kothari
    18. 18. Prof. Hemang Kothari
    19. 19. Dynamic Partition• Partitions are of variable length and number• Process is allocated exactly as much memory as required• Eventually get holes in the memory. This is called externalfragmentation• Must use compaction to shift processes so they are contiguous and allfree memory is in one blockProf. Hemang Kothari
    20. 20. Prof. Hemang Kothari
    21. 21. Dynamic Storage Allocation Problem• First-fit: Allocate the first hole that is big enough.• Best-fit: Allocate the smallest hole that is big enough; must search entirelist, unless ordered by size. Produces the smallest leftover hole.• Worst-fit: Allocate the largest hole; must also search entire list. Producesthe largest leftover hole.How to satisfy a request of size n from a list of free holes.First-fit and best-fit better than worst-fit in terms of speed and storage utilization.Prof. Hemang Kothari
    22. 22. Dynamic Partitioning• Next-fit• Scans memory from the location of the last placement• More often allocate a block of memory at the end of memory where thelargest block is found• The largest block of memory is broken up into smaller blocks• Compaction is required to obtain a large block at the end of memoryProf. Hemang Kothari
    23. 23. Best Fit Vs. First Fit• Memory sizes 1300 and 1200• Requests: 1000, 1100, 250• Request First-Fit Best-Fit• 1300, 1200 1300, 1200• 1000 300, 1200 1300, 200• 1100 300, 100 200, 200• 250 50, 100 stuckProf. Hemang Kothari
    24. 24. Best Fit Vs. First Fit• Memory sizes 1300 and 1200• Requests: 1100, 1050, 250• Request First-Fit Best-Fit• 1300, 1200 1300, 1200• 1100 200, 1200 1300, 100• 1050 200, 150 250, 200• 250 stuck 0, 200Prof. Hemang Kothari
    25. 25. Placement Algorithm• Used to decide which freeblock to allocate to aprocess of 16MB.• Goal: reduce usage ofcompaction procedure(its time consuming).• Example algorithms:• First-fit• worst-fit• Best-fitProf. Hemang Kothari
    26. 26. Final Comments• First-fit favors allocation near the beginning: tends to create lessfragmentation then Next-fit.• Next-fit often leads to allocation of the largest block at the end ofmemory.• Best-fit searches for smallest block: the fragment left behind is smallas possible –• main memory quickly forms holes too small to hold any process: compactiongenerally needs to be done more often.• First/Next-fit and Best-fit better than Worst-fit (name is fitting) interms of speed and storage utilization.Prof. Hemang Kothari
    27. 27. Final Comments - Fragmentation• There are really two types of fragmentation:1. Internal Fragmentation –allocated memory may be slightly larger than requested memory;this size difference is memory internal to a partition, but not beingused.2. External Fragmentation –total memory space exists to satisfy a size n request, but thatmemory is not contiguous.Prof. Hemang Kothari
    28. 28. HOME WORK• Consider a swapping system in which memory consists of thefollowing hole sizes in memory order: 10 KB, 4 KB, 20 KB, 18KB, 7 KB. 9 KB, 12 KB, and 15 KB. Which hole is taken forsuccessive segment requests of1. 12 KB2. 10 KB3. 9 KBfor first fit? Now repeat the question for best fit, worst fit, andnext fit.Prof. Hemang Kothari
    29. 29. GTU -2011Given memory partition of 100K, 500K, 200K, 300K, and 600K inorder, How would each of the First-fit, Best-fit and Worst-fitalgorithms place the processes of 212K, 417K, 112K and 426K inOrder ?Which algorithm makes the most efficient use of memory?Show the diagram of memory status in each cases.Answer:First fit takes 20 KB, 10 KB, 18 KB.Best fit takes 12 KB, 10 KB, and 9 KB.Worst fit takes 20 KB, 18 KB, and 15 KB.Next fit takes 20 KB, 18 KB, and 9 KB.Prof. Hemang Kothari
    30. 30. Assume that the list of holes in a variable partitions memory system contains thefollowing entries (in the given order): 190 KB, 550 KB, 220 KB, 420KB, 650KB, and110KB. Consider the following sequence of requests (in the given order): A= 210KB,B=430 KB, C=100KB, D=420KB, E=515KB.Determine which holes would be allocatedto which request by each of the following schemes and compute total internalfragmentation and external fragmentation for each algorithm.a. First fitb. Best fitc. Worst fitProf. Hemang Kothari
    31. 31. Prof. Hemang Kothari
    32. 32. Prof. Hemang Kothari
    33. 33. Answer :Total internal fragmentation for first-fit =640KBTotal internal fragmentation for best fit = 275KBTotal internal fragmentation for worst fit = 880KBProf. Hemang Kothari
    34. 34. MMUProf. Hemang Kothari
    35. 35. Virtual or Logical Memory• The basic idea behind virtual memory is that the combined size of theprogram, data, and stack may exceed the amount of physical memoryavailable for it.• The operating system keeps those parts of the program currently inuse in main memory, and the rest on the disk.• These program-generated addresses are called virtual addresses andform the virtual address space.• MMU (Memory Management Unit) that maps the virtual addressesonto the physical memory addressesProf. Hemang Kothari
    36. 36. Case Study• We have a computer that can generate 16-bit addresses, from 0 up to64K. These are the virtual addresses.• This computer, however, has only 32 KB of physical memory, soalthough 64-KB programs can be written, they cannot be loaded intomemory in their entirety and run.• A complete copy of a program’s core image, up to 64 KB, must bepresent on the disk, however, so that pieces can be brought in asneeded.Prof. Hemang Kothari
    37. 37. Paging• Divide logical memory into blocks ofsame size called pages• Divide physical memory into fixed-sizedblocks called frames. Keep track of allfree frames• To run a program of size n pages, needto find n free frames.• Set up a page table to translate logicalto physical addresses• The pages and frames are always thesame size.Prof. Hemang Kothari
    38. 38. Case Study – Continue• In this example pages are 4 KB, but page sizes from 512 bytes to 64 KBhave been used in real systems.• With 64 KB of virtual address space and 32 KB of physical memory, weget 16 virtual pages and 8 page frames.• Transfers between RAM and disk are always in units of a page.Prof. Hemang Kothari
    39. 39. • When the program tries to access address0, for example, using the instruction• MOV REG,0• virtual address 0 is sent to the MMU. TheMMU sees that this virtual address falls inpage 0 (0 to 4095), which according to itsmapping is page frame 2 (8192 to 12287).• It thus transforms the address to 8192 andoutputs address 8192 onto the bus.• Example, virtual address 20500 is 20 bytesfrom the start of virtual page 5 (virtualaddresses 20480 to 24575) and maps ontophysical address 12288 + 20 = 12308.Prof. Hemang Kothari
    40. 40. Address TranslationProf. Hemang Kothari
    41. 41. Prof. Hemang Kothari
    42. 42. Page Fault• What happens if the program tries to use an unmapped page, forexample, by using the instruction• MOV REG,32780• Which is byte 12 within virtual page 8 (starting at 32768)?• The MMU notices that the page is unmapped (indicated by a cross inthe figure) and causes the CPU to trap to the operating system. Thistrap is called a page fault.• The operating system picks a little-used page frame and writes itscontents back to the disk. It then fetches the page just referenced intothe page frame just freed, changes the map, and restarts the trappedinstruction.Prof. Hemang Kothari
    43. 43. Processes & Frames & Page FaultA.0A.1A.2A.3B.0B.1B.2C.0C.1C.2C.3D.0D.1D.2D.3D.4Prof. Hemang Kothari
    44. 44. Page Table of ProcessProf. Hemang Kothari
    45. 45. Summary of Mapping• The virtual address is split into a virtual page number (high-order bits)and an offset (low-order bits).• For example, with a 16-bit address and a 4-KB page size, the upper 4bits could specify one of the 16 virtual pages and the lower 12 bitswould then specify the byte offset (0 to 4095) within the selectedpage.• However a split with 3 or 5 or some other number of bits for the pageis also possible. Different splits imply different page sizes.Prof. Hemang Kothari
    46. 46. Review• What is the difference between a physical address and a virtualaddress?Answer:Real memory uses physical addresses. These are the numbers that thememory chips react to on the bus. Virtual addresses are the logicaladdresses that refer to a process’ address space. Thus a machine with a16-bit word can generate virtual addresses up to 64K, regardless ofwhether the machine has more or less memory than 64 KB.Prof. Hemang Kothari
    47. 47. ReviewConversion Code - ChartDECIMAL0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15HEX 0 1 2 3 4 5 6 7 8 9 A B C D E FBINARY0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 11111.For each of the following decimal virtual addresses,compute the virtual page number and offset for a 4-KBpage and for an 8 KB page: 20000, 32768, 60000.Answer: For a 4-KB page size the (page, offset) pairs are (4, 3616), (8, 0),and (14, 2656). For an 8-KB page size they are (2, 3616), (4, 0), (7, 2656).Prof. Hemang Kothari
    48. 48. Review• Give the physical addresscorresponding to each of thefollowing virtual addresses:(a) 20(b) 4100(c) 8300Answer:(a) 8212(b) 4100(c) 24684Prof. Hemang Kothari
    49. 49. Review Continue• A machine has 48-bit virtual addresses and 32-bit physicaladdresses. Pages are 8 KB. How many entries are needed forthe page table?Prof. Hemang Kothari
    50. 50. Page TablePurpose : map virtual pages onto page frames•Major issues to be faced1.The page table can be extremely large2.The mapping must be fast.Prof. Hemang Kothari
    51. 51. 1. Page Table is extremely large• Modern computers use virtual addresses of at least 32 bits.With, say, a 4-KB page size, a 32-bit address space has 1million pages, and a 64-bit address space has more than youwant to contemplate.• With 1 million pages in the virtual address space, the pagetable must have 1 million entries. And remember that eachprocess needs its own page table (because it has its ownvirtual address space).Prof. Hemang Kothari
    52. 52. 2. Mapping must be fast• The second point is a consequence of the fact that the virtual-to-physical mapping must be done on every memory reference.• A typical instruction has an instruction word, and often a memoryoperand as well. Consequently, it is necessary to make 1, 2, orsometimes more page table references per instruction, If aninstruction takes, say, 4 nsec.• The page table lookup must be done in under 1 nsec to avoidbecoming a major bottleneck.Prof. Hemang Kothari
    53. 53. Solution - Multilevel Page Table• Multilevel page tables avoid keeping one huge page table in memory all thetime: this works because most processes use only a few of its pages frequentlyand the rest, seldom if at all.Scheme: the page table itself is paged.• EX. Using 32 bit addressing:• The top-level table contains 1,024 pages (indices). The entry at each indexcontains the page frame number of a 2nd-level page table. This index (or pagenumber) is found in the 10 highest (leftmost) bits in the virtual addressgenerated by the CPU.• The next 10 bits in the address hold the index into the 2nd-level page table. Thislocation holds the page frame number of the page itself.• The lowest 12 bits of the address is the offset, as usual.Prof. Hemang Kothari
    54. 54. Prof. Hemang Kothari
    55. 55. Review of Multilevel Page Table• Assume a 32 bit system, with 2-level page table (page size is 4KB,|p1|=|p2|=10bits, |offset|=12bits).• Program “A” on this system requires 12 MB of memory. The bottom4MB of memory are used by the program text segment, followed by4MB for data and lastly, the top 4MB for stack.• Question:1. How many page table pages are actually required for this process.2. Describe the lookup within the page tables of address 0x00403004.Prof. Hemang Kothari
    56. 56. Review• We use the following scheme:• The 12 least significant digits in this address, allow access for 212 bytes – 4Kb.• These are pointed to by any of the 210 entries of p2. In total, a second levelpage table can point to 222 bytes – 4 MB.• Each such page table is pointed to by a first level table entry.• In our case – we require 4 page table pages: a single first level page table(also known as the “directory”), which points to 3 second level page tables.pageoffsetp1 p2 d10 10 12Prof. Hemang Kothari
    57. 57. Two-level Page Tables (cont.)Ex. Given 32 bit virtual address 00403004 (hex) = 4,206,596 (dec)converting to binary we have:0000 0000 0100 0000 0011 0000 0000 0100regrouping 10 highest bits, next 10 bits, remaining 12 bits:0000 0000 01 00 0000 0011 0000 0000 0100PT1 = 1 PT2 = 3 offset = 4PT1 = 1 => go to index 1 in top-level page table. Entry here is the page frame number of the 2nd-levelpage table. (entry =1 in this ex.)PT2 = 3 => go to index 3 of 2nd-level table 1. Entry here is the no. of the page frame that actuallycontains the address in physical memory. (entry=3 in this ex.) The address is found using the offsetfrom the beginning of this page frame. (Remember each page frame corresponds to 4096 addresses ofbytes of memory.)Prof. Hemang Kothari
    58. 58. Top-level page table: 102310...1Corresponds to all possiblevirtual addresses with 32bitaddresses: 0 -4,294,967,295(dec)Correspondsto addresses0 - 4, 194,303Correspondsto addresses4, 194, 304 -8,388,6082nd-level page table:. . ..........012310230 1 1023Each page ~ 4Keach chunk ~ 4M(~4000*1000)Corresponds to bytes12,288 -16,384 from beginning of pagetable 1312,292Offset 4 + 12,288 = 12,292(corresponds to absolute address 4, 206, 596)Prof. Hemang Kothari
    59. 59. Reviewpage numberpageoffsetp1 p2 d10 10 12Top-levelpage table012341023 409532 bit virtual address, 4K pages, lookup of0x00403004 (4,206,596(dec)) in binary0000 0000 01 00 0000 0011 0000 0000 0100Binary:0000000001 = 1(dec)0000000011 = 3(dec)000000000100 = 4(dec)102301234102301234012344 – 8 MB12288 – 16383 Byte4 MB Entry4 KB Entry1 Byte EntryBLOCK OF 4 MBPAGE OF 4 KBProf. Hemang Kothari
    60. 60. Structure of Page Table Entry•If a referenced page is not in memory, the present/absent bit will be zero, and apage fault occurs and the operating system will signal the process.•Memory protection in a paged environment is accomplished by protections foreach frame, also kept in the page table. One bit can define a page as read-only.•The “dirty bit” is set when a page has been written to. In that case it has beenmodified. When the operating system decides to replace that page frame, if thisbit (also called the modified or referenced bit) is set, the contents must bewritten back to disk or page is in use (reference bit). If not, that step is notneeded: the disk already contains a copy of the page frame.Prof. Hemang Kothari
    61. 61. Implementation of Page Table• Page table is kept in main memory• Page-table base register (PTBR) points to the page table• Page-table length register (PRLR) indicates size of the page table• In this scheme every data/instruction access requires two memory accesses.One for the page table and one for the data/instruction.• The two memory access problem can be solved by the use of a specialfast-lookup hardware cache called associative memory or translationlook-aside buffers (TLBs)Prof. Hemang Kothari
    62. 62. Translation Lookaside Buffer (TLB)• It is a hardware. It’s a cache actually.• When a virtual address is generated by the CPU, its pagenumber is presented to the TLB. If the page number isfound, its frame is immediately available and used to accessmemory.• If the page number is not in the TLB ( a miss) a memoryreference to the page table must be made.Prof. Hemang Kothari
    63. 63. Paging Hardware With TLBProf. Hemang Kothari
    64. 64. Segmentation• Segmentation is a technique for breaking memory up into logicalpieces• Each “piece” is a grouping of related information• data segments for each process• code segments for each process• data segments for the OS• etc.• Like paging, use virtual addresses and use disk to make memory lookbigger than it really is• Segmentation can be implemented with or without pagingProf. Hemang Kothari
    65. 65. Segmentationlogical address spaceOS Code OS data OS stackP1 dataP1 codeprintfunctionP2 codeP2 dataProf. Hemang Kothari
    66. 66. Addressing Segments• User generates logical addresses• These addresses consist of a segment number and an offset into thesegment• Use segment number to index into a table• Table contains the physical address of the start of the segment• often called the base address• Add the offset to the base and generate the physical address• before doing this, check the offset against a limit• the limit is the size of the segmentProf. Hemang Kothari
    67. 67. Addressing SegmentsS oo < limitlimit base+Physical Addresserroryesnosegment tablelogical addressProf. Hemang Kothari
    68. 68. Segmentation Hardware• Sounds very similar to paging• Big difference – segments can be variable in size• Most systems provide segment registers• If a reference isn’t found in one of the segment registers• trap to operating system• OS does lookup in segment table and loads new segment descriptor into theregister• return control to the user and resume• Again, similar to pagingProf. Hemang Kothari
    69. 69. Logical AddressesProf. Hemang Kothari
    70. 70. PagingProf. Hemang Kothari
    71. 71. SegmentationProf. Hemang Kothari
    72. 72. Mix IT – Paging & Segmentation• A virtual address becomes a segment number, a page within thatsegment, and an offset within the page.• The segment number indexes into the segment table which yields thebase address of the page table for that segment.• Check the remainder of the address (page number and offset) againstthe limit of the segment.• Use the page number to index the page table. The entry is the frame.(The rest of this is just like paging.)• Add the frame and the offset to get the physical address.Prof. Hemang Kothari
    73. 73. offsetdescriptorlimit basesegment table+virtual address from userlinear addressPT directory page tablepage frameDir (PT-1) page offsetdirectory baseBenefits:1. faster process start times2. faster process growth3. memory sharing between processes.Costs:1. somewhat slower context switches2. slower address translation.Prof. Hemang Kothari
    74. 74. Motivation for Page Replacement• When a page fault occurs, the operating system must choosea page to remove from memory to make room for the pagethat has to be brought in.• A page-replacement strategy is characterized by• Heuristic it uses to select a page for replacement• The overhead it incursProf. Hemang Kothari
    75. 75. FIFO•Treats page frames allocated to a process as a circularbuffer:• When the buffer is full, the oldest page is replaced. Hencefirst-in, first-out:• A frequently used page is often the oldest, so it will berepeatedly paged out by FIFO.• Simple to implement:• requires only a pointer that circles through the page frames ofthe process.Prof. Hemang Kothari
    76. 76. Example - FIFOProf. Hemang Kothari
    77. 77. Belady’s (or FIFO) AnomalyCertain page reference patterns actually cause more page faults when number of page framesallocated to a process is increased Prof. Hemang Kothari
    78. 78. FIFO page replacement algorithmDisadvantage:• The oldest page may be needed again soon• Some page may be important throughout execution• It will get old, but replacing it will cause an immediatepage faultProf. Hemang Kothari
    79. 79. Review - FIFO• If FIFO page replacement is used with four page frames and eightpages, how many page faults will occur with the reference string0 1 7 2 3 2 7 1 0 3• if the four frames are initially empty?Answer: FIFO yields 6 page faultsProf. Hemang Kothari
    80. 80. Optimal Page Replacement• The Optimal policy selects for replacement the page that will not beused for longest period of time.• Impossible to implement (need to know the future) but serves as astandard to compare with the other algorithms we shall study.• On the second run of a program, if the operating system kept track ofall page references, the “Optimal Page Replacement Algorithm” couldbe used:Prof. Hemang Kothari
    81. 81. ExampleProf. Hemang Kothari
    82. 82. One more example• Reference string : 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5• 4 frames example• How do you know future use? You don’t!• Used for measuring how well your algorithm performs.6 page faultsProf. Hemang Kothari
    83. 83. How can we do better?• Need an approximation of how likely each frame is to beaccessed in the future• If we base this on past behavior we need a way to track pastbehavior• Tracking memory accesses requires hardware support to beefficientProf. Hemang Kothari
    84. 84. Page table: referenced and dirty bits• Each page table entry (and TLB entry!) has a• Referenced bit - set by TLB when page read / written• Dirty / modified bit - set when page is written• Idea: use the information contained in these bits to drive thepage replacement algorithmProf. Hemang Kothari
    85. 85. Not recently used page replacement alg.• Uses the Referenced Bit and the Dirty Bit• Initially, all pages have• Referenced Bit = 0• Dirty Bit = 0• Periodically... (e.g. whenever a timer interrupt occurs)• Clear the Referenced Bit• Referenced bit now indicates “recent” accessProf. Hemang Kothari
    86. 86. Not recently used page replacement alg.• When a page fault occurs...• Categorize each page...• Class 1: Referenced = 0 Dirty = 0• Class 2: Referenced = 0 Dirty = 1• Class 3: Referenced = 1 Dirty = 0• Class 4: Referenced = 1 Dirty = 1• Choose a victim page from class 1 … why?• If none, choose a page from class 2 … why?• If none, choose a page from class 3 … why?• If none, choose a page from class 4 … why?Although class 2pages seem, at firstglance, impossible,they occur when aclass 4 page has its Rbit cleared by a clockinterrupt.Prof. Hemang Kothari
    87. 87. Second chance page replacement alg.• An implementation of NRU based on FIFO• Pages kept in a linked list• Oldest is at the front of the list• Look at the oldest page• If its “referenced bit” is 0...• Select it for replacement• Else• It was used recently; don’t want to replace it• Clear its “referenced bit”• Move it to the end of the list• RepeatProf. Hemang Kothari
    88. 88. ExampleProf. Hemang Kothari
    89. 89. Clock algorithm (an implementation of NRU)• Maintain a circular list of pages in memory• Set a bit for the page when a page is referenced• Clock sweeps over memory looking for a victim page thatdoes not have the referenced bit set• If the bit is set, clear it and move on to the next page• Replaces pages that haven’t been referenced for onecomplete clock revolution – essentially an implementationof NRUProf. Hemang Kothari
    90. 90. Prof. Hemang Kothari
    91. 91. Least Recently Used (LRU)• Replaces the page that has not been referenced for thelongest time Or Evict the page that was used the longesttime ago• By the principle of locality, this should be the page least likelyto be referenced in the near future.• Temporal locality: Memory accessed recently tends to beaccessed again soon• Spatial locality: Memory locations near recently-accessedmemory is likely to be referenced soonProf. Hemang Kothari
    92. 92. Example - LRUProf. Hemang Kothari
    93. 93. One more example• Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 58 page faults52431234125412531243Implementation● Every time a page is accessed, record a timestamp of the access time● When choosing a page to evict, scan over all pages and throw out page witholdest timestampProf. Hemang Kothari
    94. 94. Review - LRU• If LRU page replacement is used with four page frames and eightpages, how many page faults will occur with the reference string0 1 7 2 3 2 7 1 0 3• if the four frames are initially empty?Answer: LRU yields 7 page faultsProf. Hemang Kothari
    95. 95. Not frequently used algorithm (NFU)• Bases decision of frequency of use rather than recency• Associate a counter with each page• On every clock interrupt, the OS looks at each page.• If the Reference Bit is set...• Increment that page’s counter & clear the bit.• The counter approximates how often the page is used.• For replacement, choose the page with lowest counter.Prof. Hemang Kothari
    96. 96. Not frequently used algorithm (NFU)• Problem:• Some page may be heavily used• ---> Its counter is large• The program’s behavior changes• Now, this page is not used ever again (or only rarely)• This algorithm never forgets!• This page will never be chosen for replacement!Prof. Hemang Kothari
    97. 97. Modified NFU with aging• Associate a counter with each page• On every clock tick, the OS looks at each page.• Shift the counter right 1 bit (divide its value by 2)• If the Reference Bit is set...• Set the most-significant bit• Clear the Referenced BitT1100000 = 32T2010000 = 16T3001000 = 8T4000100 = 4T5100010 = 34Prof. Hemang Kothari
    98. 98. Working set page replacement• Demand paging• Pages are only loaded when accessed• When process begins, all pages marked INVALID• Locality of Reference• Processes tend to use only a small fraction of their pages• Working Set• The set of pages a process needs• If working set is in memory, no page faults• What if you can’t get working set into memory?Prof. Hemang Kothari
    99. 99. Working set page replacement• Thrashing• If you can’t get working set into memory, page faultsoccur every few instructions• Little work gets done• Most of the CPU’s time is going on overheadProf. Hemang Kothari
    100. 100. Working set page replacement• Based on prepaging (prefetching)• Load pages before they are needed• Main idea:• Try to identify the process’s “working set”• How big is the working set?• Look at the last K memory references• As K gets bigger, more pages needed.• In the limit, all pages are needed.Prof. Hemang Kothari
    101. 101. Working set page replacement• The size of the working set:k (the time interval)Prof. Hemang Kothari
    102. 102. Working set page replacement• Idea:• Look back over the last T msec of time• Which pages were referenced?• This is the working set.• Current Virtual Time• Only consider how much CPU time this process has seen.• Implementation• On each clock tick, look at each page• Was it referenced?• Yes: Make a note of Current Virtual Time• If a page has not been used in the last T msec,• It is not in the working set!• Evict it; write it out if it is dirty.Prof. Hemang Kothari
    103. 103. Working set page replacementProf. Hemang Kothari
    104. 104. WSClock page replacement algorithm-Imple.• All pages are kept in a circular list (ring)• As pages are added, they go into the ring• The “clock hand” advances around the ring• Each entry contains “time of last use”• Upon a page fault...• If Reference Bit = 1...• Page is in use now. Do not evict.• Clear the Referenced Bit.• Update the “time of last use” field.Prof. Hemang Kothari
    105. 105. WSClock page replacement algorithm• If Reference Bit = 0• If the age of the page is less than T...• This page is in the working set.• Advance the hand and keep looking• If the age of the page is greater than T...• If page is clean• Reclaim the frame and we are done!• If page is dirty• Schedule a write for the page• Advance the hand and keep lookingProf. Hemang Kothari
    106. 106. Thank YouProf. Hemang Kothari

    ×