1 / 80

Operating Systems

Operating Systems. Certificate Program in Software Development CSE-TC and CSIM, AIT September -- November, 2003. Objectives describe virtual memory , a technique that allows the execution of processes that may not be completely in memory (RAM). 9. Virtual Memory (Ch.9, S&G).

Download Presentation

Operating Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Operating Systems Certificate Program in Software DevelopmentCSE-TC and CSIM, AITSeptember -- November, 2003 • Objectives • describe virtual memory, a technique that allows the execution of processes that may not be completely in memory (RAM) 9. Virtual Memory(Ch.9, S&G) ch 10 in the 6th ed.

  2. Contents 1. Motivation 2. Virtual Memory 3. Demand Paging 4. Demand Paging Performance 5. Page Replacement continued

  3. 6. Page Replacement Algorithms 7. Allocation of Frames 8. Thrashing 9. Other Considerations 10. Demand Segmentation

  4. 1. Motivation • Loading all of a program/process into memory is frequently unnecessary because: • many routines/functions are rarely used • programmers make data structures too large • e.g. char line[500]; continued

  5. Advantages of being able to run a program that is only partially loaded into memory: • programs are no longer limited by physical memory size • more programs can be loaded and run • increased CPU utilization, throughput • the OS can pretend to have a larger virtual address space

  6. 2. Virtual Memory • Virtual memory separates the user’s logical view of memory from the physical memory. • Virtual memory appears to be much larger, and more uniform, than physical memory • a better abstraction • requires the hardware to support swapping in/out from secondary storage

  7. Virtual Memory Diagram Fig. 9.1, p.291 virtual memory page 0 page 1 page 2 page 3 secondarystorage memorymap physicalmemory page n

  8. 3. Demand Paging • Virtual memory is commonly implemented by demand paging • similar to a page system with swapping • The OS only swaps a page into memory when it is required by a process • called lazy paging (or lazy swapping)

  9. Swapping in/out Fig. 9.2, p.292 programA swap out programB swap in mainmemory

  10. Implementation • One approach is to add a valid-invalid bit to each page in the page table • valid means that the page is in memory and can be legally referenced by the process • invalid means that the page is not in memory or that it cannot be legally referenced by the process

  11. Valid-Invalid Diagram Fig. 9.3, p.293 0 0 A 1 0 4 v 1 B 2 1 i 2 C 3 2 6 v 4 D A 3 3 i A B 5 4 E 4 i C D E 6 C 5 5 F 9 v 7 F 6 i 6 G 8 7 i 7 H 9 F page table virtual memory physicalmemory

  12. Page Faults • If a process tries to use a page which is marked invalid then a page-fault trap occurs.

  13. i Page Fault Steps Fig. 9.4, p.294 page is onbacking store 3 OS 1 2 reference loadM trap 6 restartinst. page table free frame reset page table 4 bring inmissing page 5 physical memory

  14. Notes • Must distinguish between the two meanings of ‘invalid’: • an illegal reference  process termination • page not in memory  swap it in • Free frames in physical memory are registered in a free-frame list

  15. Resuming a Process • Before the page fault occurs, the state of the process must be saved • e.g. record its register values, PC • The saved state allows the process to be resumed from the line where it was interrupted.

  16. Resuming an Instruction • A single instruction may involve several pages: • e.g. ADD A B Result • The need to swap in several pages may require the interruption and resumption of an instruction several times • not always easy

  17. 4. Demand Page Performance • Let p = probability of a page fault ma = memory access time (typically 10 - 200 nsecs;use 100 nsecs) • Effective Access Time (EAT) = ((1-p) * ma) + (p * page-fault-rate)

  18. Page Fault Time • Main components: (1) service the page fault interrupt (2) read the page into memory (3) restart the process • (1) and (3) take ~1-100 microsecs each continued

  19. (2) Reading in the page ~= • hard disk latency (~8 msecs) +seek time (~15 msecs) +transfer time (~1 msec) 24 msecs • Page fault time = (1) + (2) + (3) = about 25 msecs continued

  20. Effective Access Time (in nsecs) = ((1-p) * 100) + p * 25,000,000 = 100 + p*24,999,900 nsecs

  21. Examples • Assume 1 access out of 1000 causes a page fault. • EAT = 100 + 0.001 * 24,999,900= about 25,000 nsecs • Slowdown is about250 times! continued

  22. What is the page fault rate to make the slowdown less than 10%? • 110 > 100 + p*24,999,900so 10 > p*24,999,900so p < ~ 1/2,500,000 • A page fault must occur less than once in 2,500,000 memory accesses.

  23. Observations • The page fault rate must be very low in a demand paging system or the effective access time increases dramatically. • Transfer time is a small part of the total time for reading in a page.

  24. 5. Page Replacement • Assume an OS with 4 frames. • A process p0 which would fill 4 pages if loaded into memory all at once may only require at most 2 pages at any one time. continued

  25. Knowing this, the OS can allocate 2 pages to p0 and give the other 2 pages to another process p1 • increases CPU utilization and throughput • Called over-allocation of memory

  26. Problem • Process usage varies with data, and so in the future the p0 process might need 3,4, etc pages • but no longer any free frames • What to do?

  27. Possible Solutions • Terminate the process • restart it later • Swap out the process • swap it back when there are free frames • Page replacement • find a page that is not required and swap it out • this frees the frame for the process’ page

  28. Page Replacement Diagram Fig. 9.6, p.302 frame swap outvictim page change toinvalid 1 f victim 2 3 f v 4 swapdesiredpage in reset page table for new page page table physicalmemory

  29. Implementation • There must be a page replacement algorithm which selects a victim page to swap out. • Also requires a frame allocation algorithm • how many frames to allocate to each process? • Two page transfers are required: • doubles the page fault rate • makes the EAT worse

  30. Reducing the Overhead • Each page can have a modify (dirty) bit • indicates if the page has been modified • No need to swap out an unmodified victim page.

  31. 6. Page Replacement Algorithms • Aim: to reduce the page fault rate by as much as possible. • Different algorithms are evaluated by calculating the number of page faults they cause on a reference string. • A reference string is a sequence of addresses referenced by a program.

  32. Example • An address reference string: 0100 0432 0101 0612 0102 0103 0104 01010611 0103 0104 0101 0610 0102 0103 01040101 0609 0102 0105 • Assuming 100 bytes/page, then its corresponding page reference string is: 1 4 1 6 1 6 1 6 1 6 1 11 pages continued

  33. How many page faults occur? • depends on the number of frames available • If there are 3 or more frames: • 3 page faults occur • If 1 frame: • 11 page faults occur

  34. Page Fault/Frames Curve Fig. 9.7, p.304

  35. Comparison of Algorithms • We will use the reference string: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 • Assume memory contains 3 frames.

  36. Algorithms Examined 6.1. FIFO 6.2. Optimal 6.3. LRU 6.4. LRU Approximation 6.4.1. Addition Reference Bits 6.4.2. Second Chance • Enhanced Second Chance (X) • Counting (X) • Page Buffering (X)

  37. 6.1. FIFO Algorithm • FIFO = first-in, first-out • When a page must be replaced, the oldest one is chosen. • Easy to understand/program • but performance is not always good

  38. 4 7 7 0 0 7 7 0 2 4 2 4 2 0 3 1 0 0 3 0 2 3 1 2 1 2 1 0 2 1 2 0 3 3 0 3 1 1 2 FIFO Example Fig. 9.8, p.305 7 0 1 2 0 3 0 4 2 3 ... 7 7 0 15pagefaults … 0 3 2 1 2 0 1 7 0 1

  39. Belady’s Anomaly • It is not always true that increased memory (frames) reduces the number of page faults. • Consider the reference string: 1 2 3 4 1 2 5 1 2 3 4 5

  40. Graph Fig. 9.9, p.306

  41. 6.2. Optimal Algorithm • Replace the page that will not be used for the longest period of time in the future • guaranteed lowest page fault rate • Must know the future behaviour of the process • very difficult to implement • useful for comparison with other algorithms

  42. 2 7 2 2 2 7 2 0 0 0 0 0 4 0 3 1 1 3 1 1 3 Optimal Example Fig. 9.10, p.307 7 0 1 2 0 3 0 4 2 3 ... 7 7 0 9pagefaults … 0 3 2 1 2 0 1 7 0 1

  43. 6.3. LRU Algorithm • LRU = Least Recently Used • Replace the page that has not been used for the longest period of time. • Hard to implement efficiently: • use an extra time-of-use field in a page • replace the oldest page • store a stack of page numbers • replace the bottom page

  44. 1 0 1 4 7 4 2 4 2 1 0 0 0 3 0 0 3 3 0 0 3 3 2 2 2 2 1 7 1 2 LRU Example Fig. 9.11, p.307 7 0 1 2 0 3 0 4 2 3 ... 7 7 0 12pagefaults … 0 3 2 1 2 0 1 7 0 1

  45. 6.4. LRU Approximations • Few machines have sufficient hardware to support LRU • so approximate it with the available hardware • Many systems have a reference bit in each page • it is set when a page is read or written continued

  46. Over time, the reference bits of pages show which have been used, which not • can be used as the basis of an approximate LRU

  47. 6.4.1. Additional Reference Bits Algorithm • Use a byte to hold 8 reference bits for each page. • At regular intervals, shift the bits of the ref. byte to the right by 1 bit, discarding the low-order bit, and updating the high-order bit (set to 1 or 0). • The byte holds a history of the last 8 time periods.

  48. Example • The reference byte 00000000 means that the page has not been referenced. • 11111111 means its page has been referenced in all 8 time periods. • Convert the byte to an unsigned integer • the lowest number is the least used, and so should be replaced

  49. 6.4.2. Second Chance Algorithm • If a selected page has 0 in its reference bit then replace it. • If the reference bit is 1 (it has been accessed) then set the bit to 0 and move to the next page • i.e. give the page a second chance continued

  50. Can be implemented by treating the page table used by a process as a circular queue • this version is called the clock algorithm

More Related