Presentation is loading. Please wait.

Presentation is loading. Please wait.

Virtual Memory CSSE 332 Operating Systems

Similar presentations


Presentation on theme: "Virtual Memory CSSE 332 Operating Systems"— Presentation transcript:

1 Virtual Memory CSSE 332 Operating Systems
Rose-Hulman Institute of Technology 1

2 Announcements Milestone 5 & presentation are due on Wed

3 Background Virtual memory – separation of user logical memory from physical memory. Only part of the program needs to be in memory for execution Logical address space can therefore be much larger than physical address space Allows address spaces to be shared by several processes Allows for more efficient process creation Virtual memory can be implemented via: Demand paging Demand segmentation So virtual memory != real memory (physical memory) Virtual memory resides both on disk and in main memory. It is the user’s perception of the amount of memory they have. 3

4 Virtual memory larger than physical memory
Virtual Memory That is Larger Than Physical Memory The MMU maps logical pages to physical page frames in memory. The memory map refers to page tables, which store the mapping between a certain process's virtual memory layout and how that space relates to physical memory addresses. 4

5 Virtual-address space
This is a logical view of how a process is stored in memory. According to this view, a process has a logical address from 0 to Max and exists in contiguous memory. But if memory is organized in frames, then the frames can be located anywhere in memory. The large blank space between the stack and the heap is part of the virtual address space and will require physical pages ONLY if the heap or stack grows. 5

6 Shared library using virtual memory
System libraries can be shared by several processes through mapping of the shared object into a virtual address space. Although each process considers the shared libraries to be part of its virtual address space, the actual frames where the shared libraries reside in memory are shared by all the processes. 6

7 Virtual memory support
Hardware must support paging and/or segmentation Operating system must be able to manage the movement of pages and/or segments between secondary memory and main memory

8 Demand Paging Bring a page into memory only when it is needed
Less I/O needed Less memory needed Faster response More users or processes Page is needed  reference to it invalid reference  abort not-in-memory  bring to memory Lazy swapper – never swaps a page into memory unless page will be needed Swapper that deals with pages is a pager What are the advantages of using demand paging? They are listed under the first large bullet. 8

9 Transfer paged memory to contiguous disk space
A demand paging system is similar to a paging system with swapping where processes reside in secondary storage. When we want to execute a process we swap it into memory. But with demand paging, rather than swapping the entire process, we use a lazy swapper. 9

10 Valid-invalid bit With each page table entry a valid–invalid bit is associated (v  in-memory, i  not-in-memory) Initially valid–invalid bit is set to i on all entries Example of a page table snapshot: During address translation, if valid–invalid bit in page table entry is I  page fault Frame # valid-invalid bit v v v v i Since we are using demand paging, in each process page table entry, there is a valid-invalid bit that tells us whether the associated page is in memory or not. …. i i page table 10

11 Some pages not in main memory
Page Table When Some Pages Are Not in Main Memory 11

12 Page fault If there is a reference to a page, first reference to that page will trap to operating system: page fault Operating system looks at page table to decide: Invalid reference  abort Just not in memory Get empty frame Swap page into frame Reset tables Set validation bit = v Restart the instruction that caused the page fault A page fault occurs when the CPU tries to access a page that is not resident in main memory. Step 4 involves updating the page table to note the frame number for the swapped-in page. 12

13 Steps in handling a page fault
13

14 Performance of demand paging
Page fault rate 0  p  1.0 if p = 0 no page faults if p = 1, every reference is a fault Effective access time (EAT) EAT = (1 – p) x memory access time + p (page fault overhead + swap page out time + swap page in time + restart overhead) Page fault rate is the probability that a page fault will happen. Effective access time is the average time it takes for the CPU to access data/instructions in main memory. Memory access time ranges from 10 to 200 nanoseconds for most systems. 14

15 Demand paging example Memory access time = 200 nanoseconds
Average page-fault service time = 8 milliseconds EAT = (1 – p) x p (8 milliseconds) = (1 – p) x p x 8,000,000 = p x 7,999,800 If one access out of 1,000 causes a page fault, then EAT = 8.2 microseconds. This is a slowdown by a factor of 40!! If no page fault the EAT = 200 nanoseconds. 15

16 Process creation Virtual memory allows other benefits during process creation: Copy-on-Write Memory-Mapped Files (in text) 16

17 Copy-on-write Copy-on-Write (COW) allows both parent and child processes to initially share the same pages in memory If either process modifies a shared page, only then is the page copied COW allows more efficient process creation as only modified pages are copied Free pages are allocated from a pool of zeroed-out frames 17

18 Before process1 modifies page C
18

19 After process1 modifies page C
19

20 What happens if there is no free frame?
Page replacement – find some page in memory, but not really in use, swap it out algorithm performance – want an algorithm that will result in minimum number of page faults Same page may be brought into memory several times 20

21 Page replacement Prevent over-allocation of memory by modifying page-fault service routine to include page replacement Use modify (dirty) bit to reduce overhead of page transfers – only modified pages are written to disk Page replacement completes separation between logical memory and physical memory – large virtual memory can be provided on a smaller physical memory All pages of a process are in virtual memory. However, only a subset of these pages needs to be resident in physical memory. 21


Download ppt "Virtual Memory CSSE 332 Operating Systems"

Similar presentations


Ads by Google