SOCSAMS e-learning VIRTUAL MEMORY Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Introduction To execute a program, the entire process should be loaded into the main memory. Thus the size of the process is limited to the size of the physical memory. A memory management scheme called “Overlaying” can be used to overcome this situation. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Overlaying The programmer splits a program into smaller parts called overlays in such a way that no two overlays are required to be in main memory at the same time. An overlay is loaded into memory only when it is needed. One major disadvantage of this technique is that it requires major involvement of the programmer and time consuming. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Virtual Memory – overview Virtual memory is a technique that allows execution of a program that is bigger than the physical memory of the computer system. In this technique, the operating system loads only those parts of program in memory that are currently needed for the execution of the process. The rest part is kept in the disk and is loaded into memory only when needed. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Virtual Memory – overview Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Virtual Memory – overview Virtual memory gives the illusion that the system has a much larger memory than is actually available. Virtual memory frees programs from the constraints of physical memory limitation. The virtual memory can be implemented by: • Demand Paging. • Demand Segmentation. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Demand Paging Demand paging is a system which a page is loaded into the memory only when it is needed during program execution. Pages that are never accessed are never loaded into the memory. A demand paging system combines the features of paging with swapping. To facilitates swapping, the entire virtual address space is stored contiguously on the disk. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Demand Paging Whenever a process is to be executed, an area on the disk is allocated to it on which pages are copied, called swap space of the process. During the execution of a process, whenever a page is required, it is loaded into the main memory from the swap space. Similarly, when a process is to be removed from main memory, it is written back into the swap space if it has been modified. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Demand Paging Other than swap space, some form of hardware support is also needed to differentiate the pages that are in memory from the ones on disk. For this, only an additional bit valid is maintained in each page table entry to indicate whether the page is in memory. If a page is valid, that is it exists in the virtual address space of the process, the associated valid bit is set to 1, otherwise it is set to 0. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Demand Paging Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Demand Paging Whenever a process request for a page, the virtual address is sent to MMU. The MMU checks the valid bit in the page table entry of that page. If the valid bit is 1, it is access as in paging. Otherwise, the MMU raises an interrupt called page fault or a missing page interrupt and the control is passed to the page fault routine in the operating system. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Demand Paging Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Demand Paging - Handing Page Fault To handle the page fault, the page fault routine first of all checks whether the virtual address for the desired page is valid from its PCB stored in the process table. If it is invalid, it terminates the process giving error. Otherwise, it takes the following steps: Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Demand Paging - Handing Page Fault 1. Location for a free page frame in memory and allocation it to the process. 2. Swaps the desired page into this allocated page frame. 3. Update the process table and page table to indicate that the page is in memory. After performing these steps, the CPU restarts from the instruction that it left off due to the page fault. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Demand Paging – page-in In demand paging, the process of loading a page in memory is known as page-in operation instead of swap-in. It is because the whole process is not loaded; only some pages are loaded into the memory. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Demand Paging - Advantages • It reduces the swap time since only the required pages are swapped in instead of the whole process. • It increases the degree of multiprogramming by reducing the amount of physical memory required for a process. • It minimizes the initial disk overhead as not all pages are to be read initially. • It does not need extra hardware support. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning COPY-ON-WRITE A process may need to create several processes during its execution and the parent and child processes have their own distinct address spaces. If the newly created process is the duplicate of the parent process, it will contain the same pages in its address space as that of parent. In this case, copying the parent’s address space may be unnecessary. To avoid copying, copy-on-write – a virtual memory technique can be employed. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning COPY-ON-WRITE In this technique, initially, parent and child processes are allowed to share the same pages and these shared pages are marked as copy-on-write pages. Now, if either process attempts to write on a shared page, a copy of that page is created for that process. Only the pages that can be modified (containing data) are marked as copyon-write pages while the pages that cannot be modified (containing executable code) are shared between parent and child processes. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning COPY-ON-WRITE To implement this, a bit is associated in page table entry of each shared page to indicate that it is a copy-on-write page. Whenever either process say, child process tries to modify a page, the operating system creates a copy of that page, maps it to the address space of the child process and turns off the copy-on-write bit. Now the child process can modify the copied page without affecting the page of the parent process. Thus, it saves memory. Dept. of Computer Applications, MES College Marampally