Presentation is loading. Please wait.

Presentation is loading. Please wait.

Virtual Memory.

Similar presentations


Presentation on theme: "Virtual Memory."— Presentation transcript:

1 Virtual Memory

2 Virtual Address Space, Physical Address Space, Swap Space
Virtual memory is the separation of user logical memory from physical memory. The programmer no longer needs to worry about the amount of physical memory available. The Virtual Address Space of a process refers to the logical (or virtual) view of how a process is stored in memory. physical memory may be organized in page frames physical page frames assigned to a process may not be contiguous.

3 Virtual Memory, Sharing
Virtual memory allows files and memory to be shared by several different processes through page sharing.

4 How works the Virtual Memory? Large program example
Fetch Policy (Page load policy to the physical memory) When the page will be brought to the main memory ? Demand paging Prepaging

5 Demand Paging pages are only loaded when they are demanded during program execution pages that are never accessed are thus never loaded into physical memory. Lazy Swapper More pager than swapper Brings the pages only by demand A demand-paging system is similar to a paging system with swapping

6 Valid Invalid bit, hardware support
With demand paging, we need some form of hardware support to distinguish between those pages that are in memory and those pages that are on the disk. page table @B 8 9 @D @H @E @G The valid-invalid bit scheme can be used for this purpose. When this bit is set to "valid," this value indicates that the associated page is both legal and in memory. If the bit is set to "invalid," the page either is not valid (that is, not in the logical address space of the process), or is valid but is currently on the disk (then the entry points to page address on the disk).

7 Page Fault Service the page-fault interrupt. Read in the page.
1. Page request 2. If V=0 (not valid) then Page Fault Occurs (the page is not in RAM) page table @B 8 9 @D @H @E @F We are faced with three major components of the page-fault service time: Service the page-fault interrupt. Read in the page. Restart the process.

8

9 Page Fault handling

10 Page Fault handling need to be able to restart any instruction after a page fault. The restart would require fetching the instruction again decoding it again fetching the two operands again and then adding again. However, there is not much repeated work (less than one complete instruction), and the repetition is necessary only when a page fault occurs.

11 Copy on Write In the extreme case, we could start executing a process with no pages in memory. On the first instruction the process immediately faults for the page. After this page is brought into memory, the process continues to execute, faulting as necessary until every page that it needs is in memory. Process creation using the fork() system call may initially bypass the need for demand paging by using a technique similar to page sharing. Copy the page only if the process writes on it This technique provides for rapid process creation and minimizes the number of new pages that must be allocated to the newly created process.

12 Why to do Page Replacement ?
What happens if there is no free frame to allocate ? Do page replacement. Find some page in memory that is not in use now and swap it out. Now you have a free frame. Bring new page here.

13 How is done the Page Replacement ?
Page fault service algorithm rewritten for Page Replacement Find the location of the desired page on the disk. Find a free frame: a. If there is a free frame, use it. b. If there is no free frame, use a page-replacement algorithm to select a victim frame. c. Write the victim frame to the disk; change the page and frame tables accordingly (change to invalid). Modify to f, valid Read the desired page into the newly freed frame; change the page and frame tables (change to valid). Restart the user process.

14 Reduce the Page Replacement time
Notice that, if no frames are free, two page transfers (one out and one in) are required. This situation effectively doubles the page-fault service time and increases the effective access time accordingly. We can reduce this overhead by using a modify bit (or dirty bit). each page or frame has a modify bit associated with it in the hardware If the page has not been modified since it was read into memory we need not write the memory page to the disk: it is already there. This scheme can significantly reduce the time required to service a page fault, since it reduces I/O time by one-half if the page has not been modified.

15 Replacement Policies Random Replacement
Memory manager must decide which page to swap out of primary memory in order to load the requested page Random Replacement F I O

16 Replacement Policies

17 Replacement Policies

18 Thrashing, Locality Theoretically, some programs may access several new pages of memory with each instruction execution (one page for the instruction and many for data), possibly causing multiple page faults per instruction. This situation would result in unacceptable system performance. Fortunately, analysis of running processes shows that this behavior is exceedingly unlikely. Programs tend to have locality of reference which results in reasonable performance from demand paging. A process is thrashing if it is spending more time paging than executing.

19 Frame Allocation to prevent Thrashing
Setup page-fault “normal” upper and lower limits for “your” OS. Allocate some amount of frames for each process Monitor the page fault rate If it’s high add more frames for processes. If page fault rate is low you can decrease the number of frames per process

20 Prepaging In a system using the working-set model, for example, we keep with each process a list of the pages in its working set If we must suspend a process (due to an I/O wait or a lack of free frames), we remember the working set for that process. When the process is to be resumed (I/O completion or enough free frames), we automatically bring back into memory its entire working set before restarting the process.

21 Virtual Memory Example
The process logical space has 4 pages. The following logical pages of the process 0,2 are placed in the physical memory following frames 2,5 Show the above situation on the below tables filling all columns Page Table Frame Valid 2 v 1 i 5 3 4 Physical Memory Frame N: Con tent Usage frequency 10 1 20 2 A 30 3 40 4 50 5 C 60 Virtual Memory Page N: Content A 1 B 2 C 3 D The next instruction of the process performs the following operation z = x + y The logical addresses of x, y, z are in the following pages of logical space: z – 0 x – 2 y – 3

22 Virtual Memory Example
The next instruction of the process performs the following operation z = x + y The logical addresses of x, y, z are in the following pages of logical space: z – 0 x – 2 y – 3 Show the situation of memory resources on below tables after the operation. If needed use the page replacement policy “Least Frequently Used”. After replacing set the “Usage Frequency” value to 0. Page Table Frame Valid 2 v 1 i 5 3  0 4 Physical Memory Frame N: Con tent Usage frequency  D 1 20 2 A 30 3 40 4 50 5 C 60 Virtual Memory Page N: Content A 1 B 2 C 3 D


Download ppt "Virtual Memory."

Similar presentations


Ads by Google