Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Virtual Memory Chapter 9. 2 Characteristics of Paging and Segmentation n Memory references are dynamically translated into physical addresses at run.

Similar presentations

Presentation on theme: "1 Virtual Memory Chapter 9. 2 Characteristics of Paging and Segmentation n Memory references are dynamically translated into physical addresses at run."— Presentation transcript:

1 1 Virtual Memory Chapter 9

2 2 Characteristics of Paging and Segmentation n Memory references are dynamically translated into physical addresses at run time u a process may be swapped in and out of main memory such that it occupies different regions n A process may be broken up into pieces (pages or segments) that do not need to be located contiguously in main memory n Hence: all pieces of a process do not need to be loaded in main memory during execution u computation may proceed for some time if the next instruction to be fetch (or the next data to be accessed) is in a piece located in main memory

3 3 Demand Paging n The OS brings into main memory only a few pieces of the program (including its starting point) n Each page/segment table entry has a present bit that is set only if the corresponding piece is in main memory n The resident set is the portion of the process that is in main memory n An interrupt (memory or page fault) is generated when the memory reference is on a piece not present in main memory

4 4 Process Execution (cont.) n OS places the process in a Blocking state n OS issues a disk I/O Read request to bring into main memory the piece referenced to n Another process is dispatched to run while the disk I/O takes place n An interrupt is issued when the disk I/O completes u This causes the OS to place the affected process in the Ready state

5 5 Advantages of Partial Loading n More processes can be maintained in main memory u Only load in some of the pieces of each process u With more processes in main memory, it is more likely that a process will be in the Ready state at any given time n A process can now execute even if its size is larger than the (available) main memory u It is even possible to use more bits for logical addresses than the bits needed for addressing the physical memory

6 6 Virtual Memory n Assume that 16 bits are needed to address a physical memory of 64KB n We can use a page size of 1KB so that 10 bits are needed for offsets within a page u For the page number part of a logical address we may use a number of bits larger than 6, say 22 (a modest value!!) n The memory referenced by a logical address is called virtual memory u maintained on secondary memory (ex: disk) u pieces are brought into main memory only when needed (demand paging)

7 7 Virtual Memory n For better performance, the file system is often bypassed and virtual memory is stored in a special area of the disk called the swap space u Larger blocks are used and file lookups and indirect allocation methods are not used n The translation from logical address to physical address is done by indexing the appropriate page/segment table with the help of memory management unit (MMU) n MMU is hardware typically implemented through TLBs (translation lookaside buffers)

8 8 Operating System Software: paging n Memory management software depends on whether the hardware supports paging or segmentation or both n Pure segmentation systems are rare. Segments are usually paged -- memory management issues are then those of paging n We shall thus concentrate on issues associated with paging

9 9 Thrashing n To accommodate as many processes as possible, only a few pieces of each process reside in main memory n When the main memory is full and the OS needs to bring a new piece in, it must swap one piece out n The OS must not swap out a piece of a process that becomes needed immediately after it is swapped out u Doing so very often leads to trashing: F The processor spends most of its time swapping pieces rather than executing user instructions

10 10 Thrashing (English) n thrash (thrsh) v. thrashed, thrash·ing, thrash·es. n v. tr 1.To beat with or as if with a flail, especially as a punishment. 2.To swing or strike in a manner suggesting the action of a flail: The alligator thrashed its tail. 3.To defeat utterly; vanquish. 4.To thresh. 5.Nautical. To sail (a boat) against opposing winds or tides. n v. intr. 1.To move wildly or violently: thrashed about all night. 2.To strike or flail. 3.To thresh. 4.Nautical. To sail against opposing tides or winds. n n. u The act or an instance of thrashing.

11 11 Locality and Virtual Memory n Principle of locality of references: u Memory references within a process tend to cluster n Hence, only a few pieces of a process will be needed over a short period of time n Possible to make intelligent guesses about which pieces will be needed in the future n This suggests that virtual memory may work efficiently (i.e.: trashing should not occur too often)

12 12 The Page Size Issue n Page size is defined by hardware; always a power of 2 for more efficient logical to physical address translation. But exactly which size to use is a difficult question: u Large page size is good since for a small page size, more pages are required per process F More pages per process means larger page tables. Hence, a large portion of page tables in virtual memory u Small page size is good to minimize internal fragmentation u Large page size is good since disks are designed to efficiently transfer large blocks of data u Larger page sizes means less pages in main memory; this increases the TLB hit ratio

13 13 The Page Size Issue n With a very small page size, each page matches the code that is actually used  page faults are low n Increased page size causes each page to contain code that is not used  Page faults rise n Pagesize = process size  Page faults are low n Small pages  large page tables  costly translation

14 14 The Page Size Issue n Page fault rate is also determined by the number of frames allocated per process n Page faults drops to a reasonable value when W frames are allocated n Drops to 0 when the number (N) of frames is such that a process is entirely in memory

15 15 The Page Size Issue n Page sizes from 1KB to 4KB are most commonly used n But the issue is non trivial. Hence some processors are now supporting multiple page sizes. Ex: u Pentium supports 2 sizes: 4KB or 4MB u R4000, R10000 support 7 sizes: 4KB to 16MB n Multiple page sizes are NOT easy to implement from an OS point-of-view. u Look at “General Purpose Operating System Support for Multiple Page Sizes” available from papers/ganapathy/ganapathy_html/ganapathy.html papers/ganapathy/ganapathy_html/ganapathy.html

16 16 Fetch Policy n Two policies are commonly used to determine when a page should be brought into main memory: u Demand paging: only brings pages into main memory when a reference is made to a location on the page (i.e.: paging on demand only) F many page faults when process first started but should decrease as more pages are brought in u Prepaging: brings in more pages than needed F locality of references suggest that it is more efficient to bring in pages that reside contiguously on the disk F efficiency not definitely established, as the extra pages brought in are “often” not referenced

17 17 Placement Policy n Determines where in real memory a process piece resides n For pure segmentation systems: u First-fit, next fit... are possible choices (a real issue) n For paging (and paged segmentation): u The hardware decides where to place the page u The chosen frame location is irrelevant since F all memory frames are equivalent (same size and access is same for all accesses)

18 18 Replacement Policy n Deals with the selection of a page in main memory to be replaced when a new page is brought in n This occurs whenever main memory is full u No free frames are available n Occurs often, since the OS tries to bring into main memory as many processes as it can to increase the multiprogramming level

19 19 Replacement Policy n Not all pages in main memory can be selected for replacement n Some frames are locked (cannot be paged out): u Much of the kernel space is held on locked frames as well as key control structures and I/O buffers n The OS might decide that the set of pages considered for replacement should be: u Limited to those of the process that has suffered the page fault u The set of all pages in unlocked frames

20 20 Replacement Policy n The decision for the set of pages to be considered for replacement is related to the resident set management strategy: u How many page frames are to be allocated to each process? F To be discussed later n No matter what set of pages is considered for replacement, the replacement policy deals with algorithms that choose the page within that set

21 21 Basic algorithms for the replacement policy n The Optimal policy selects for replacement the page for which the time to the next reference is the longest u Produces the fewest number of page faults u Impossible to implement (need to know the future) but serves as a standard for comparison n More practical algorithms inlcude: u Least recently used (LRU) u First-in, first-out (FIFO) u Clock

22 22 The LRU Policy n Replaces the page that has not been referenced for the longest time u By the principle of locality, this should be the page least likely to be referenced in the near future u Performs nearly as well as the optimal policy

23 23 The LRU Policy n Example: A process of 5 pages with an OS that fixes the resident set size to 3

24 24 Counting Page Faults n When the main memory is empty, each new page brought in is a result of a page fault n For the purpose of comparing the different algorithms, initial page faults are not counted u This is because the number of these page faults is the same for all algorithms n But, although NOT shown in the figures, these initial references are REALLY causing page faults

25 25 Implementation of the LRU Policy n Each page could be tagged (in the page table entry) with the time at each memory reference. n The LRU page is the one with the smallest time value (needs to be searched at each page fault) n This would require expensive hardware and a great deal of overhead. n Consequently very few computer systems provide sufficient hardware support for true LRU replacement policy n Other algorithms are used instead

26 26 The FIFO Policy n Treats page frames allocated to a process as a circular buffer u When the buffer is full, the oldest page is replaced: first-in-first-out F This is not necessarily the same as the LRU page F A frequently used page is often the oldest, so it will be repeatedly paged out by FIFO u Simple to implement F Requires only a pointer that circles through the page frames of the process

27 27 Comparison of FIFO with LRU n LRU recognizes that pages 2 and 5 are referenced more frequently than others but FIFO does not n FIFO is simple to implement, but performs relatively poorly

28 28 The Clock Policy n The set of frames candidate for replacement is considered as a circular buffer n A use bit for each frame is set to 1 whenever: u A page is first loaded into the frame u The corresponding page is referenced n When it is time to replace a page, the first frame encountered with the use bit set to 0 is replaced. u During the search for replacement, each use bit set to 1 is changed to 0 n When a page is replaced, a pointer is set to point to the next frame in buffer

29 29 The Clock Policy: An Example

30 30 Comparison of Clock with FIFO and LRU n Asterisk indicates that the corresponding use bit is set to 1 n Clock protects frequently referenced pages by setting the use bit to 1 at each reference

31 31 Comparison of Clock with FIFO and LRU n Numerical experiments tend to show that performance of Clock is close to that of LRU n Experiments have been performed when the number of frames allocated to each process is fixed and when pages local to the page-fault process are considered for replacement u When few (6 to 8) frames are allocated per process, there is almost a factor of 2 of page faults between LRU and FIFO u This factor reduces to close to 1 when several (more than 12) frames are allocated. F But then more main memory is needed to support the same level of multiprogramming

32 32 Page Buffering n Pages to be replaced are kept in main memory for a while to guard against poorly performing replacement algorithms such as FIFO n Two lists of pointers are maintained: each entry points to a frame selected for replacement u A free page list for frames that have not been modified since brought in (no need to swap out) u A modified page list for frames that have been modified (need to write them out) n A frame to be replaced has a pointer added to the tail of one of the lists and the present bit is cleared in the corresponding page table entry u The page remains in the same memory frame

33 33 Page Buffering n At each page fault the two lists are first examined to see if the needed page is still in main memory u If it is, the present bit in the corresponding page table entry is set and the matching entry in the relevant page list is removed u If it is not, then the needed page is brought in, it is placed in the frame pointed by the head of the free frame list (overwriting the page that was there) F The head of the free frame list is moved to the next entry u The frame number in the page table entry could be used to scan the two lists, or each list entry could contain the process id and page number of the occupied frame n The modified list also serves to write out modified pages in cluster (rather than individually)

34 34 Cleaning Policy n When should a modified page be written out to disk? u Demand cleaning: A page is written out only when its frame has been selected for replacement F A process that suffers a page fault may have to wait for 2 page transfers u Pre-cleaning: Modified pages are written before their frames are needed so that they can be written out in batches F Makes little sense to write out a batch of pages if the majority of these pages will be modified again before they are replaced

35 35 Cleaning Policy n A good compromise can be achieved with page buffering u Recall that pages chosen for replacement are maintained either on a free (unmodified) list or on a modified list u Pages on the modified list can be periodically written out in batches and moved to the free list u An efficient compromise because: F Not all dirty pages are written out but only those chosen for replacement F Writing is done in batches

Download ppt "1 Virtual Memory Chapter 9. 2 Characteristics of Paging and Segmentation n Memory references are dynamically translated into physical addresses at run."

Similar presentations

Ads by Google