Lets Discuss Virtual Memory Over here ,All the memory management policies we have discussed so far, try to keep a number of processes in the memory simultaneously to allow multi-programming. But they require an entire process to be loaded in the memory before it can execute.
With the virtual memory technique, we can execute a process which is only partially loaded in memory. Thus, the logical address space may be larger than physical memory, and we can have more processes executing in memory at a time, hence a greater degree of multi-programming.
1 Demand Paging
Demand paging is the most common virtual memory management system. It is based on the locality model of program execution. As a program executes, it moves from locality to locality.
Locality is defined as a set of pages actively used together. A program is composed of several different localities which may overlap. For example, a small procedure when called, defines a new locality. A while-do loop when being executed defines a new locality. As a program executes, it moves from locality to locality. Procedural languages with while-do, repeat-until, for-do structures (i.e. Pascal, Algol, C, etc.) have less frequently changing localities than other high-level languages with go-to structure (i.e. Basic, Fortran).
In demand paging, programs reside on a swapping device commonly known as the backing store. The backing store, for most of today’s operating systems is a disk.
When the operating system decides to start a new process, it swaps only a small part of this new process (a few pages) into memory. The page table of this new process is prepared and loaded into memory, and the valid/invalid bits of all pages that are brought into memory are set to “valid”. All the other valid/invalid bits are set to “invalid” showing that those pages are not brought into memory.
If the process currently executing tries to access a page which is not in the memory, a page fault occurs, and the OS brings that page into the memory. If a process causes page fault then the following procedure is applied:
- Trap the OS.
- Save registers and process state for the current process.
- Check if the trap was caused because of a page fault and whether the page reference is legal.
- If yes, determine the location of the required page on the backing store
- Find a free frame.
- Read (swap in) the required page from the backing store into the free frame. (During this I/O, the processor may be scheduled to some other process)
- When I/O is completed, restore registers and process state for the process which caused the page fault and save state of the currently executing process.
- Modify the corresponding page table entry to show that the recently copied page is now in memory.
- Resume execution with the instruction that caused the page fault.
While executing a process, in the case of a page fault, the OS finds the desired page on the backing store and if there is no free frames, the OS must choose a page in the memory (which is not the one being used) as a victim, and must swap it out (slow) to the backing store.
Then, the OS changes the valid/invalid bit for the victim page in the page table to indicate that it is no longer in memory. It swaps the desired page into newly freed frame, modifies the frame table, and sets the valid/invalid bit of the new page to valid. The executing process may now go on.
This operations can be summarized as:
- Checking the address and finding a free frame or victim page (fast)
- Swap out the victim page to secondary storage (slow)
- Swap in the page from secondary storage (slow)
- Context switch for the process and resume its execution (fast)
In servicing a page fault, the time is spent mainly for swap-out and swap-in. The time required for other operations are negligible.
Virtual memory can be implemented in:
- paging systems
- paged segmentation systems
- segmentation systems (However, segment replacement is much more sophisticated than page replacement since segments are of variable size.)