Virtual memory is a key concept in operating systems. It is a memory management technique that allows the computer to use more memory than is physically available. In virtual memory, the operating system divides the memory into equal-sized blocks called pages. These pages are then mapped to corresponding blocks in the computer's storage device, such as a hard disk.
When a program is running, only the pages that are actively being used are loaded into the physical memory. The rest of the pages reside in the storage device. This allows the computer to effectively utilize its available memory resources.
Virtual memory provides several benefits. It allows for efficient multitasking, as multiple programs can be loaded into memory simultaneously. It also enables programs to use more memory than is physically available, which is especially useful for large applications and data sets.
However, the use of virtual memory also introduces some overhead. When a page that is not currently in physical memory is accessed, a page fault occurs, and the operating system must retrieve the required page from the storage device. This can result in slower performance compared to accessing data directly from physical memory.
There are two ways in which virtual memory can be implemented:
Physical memory(RAM) is being split into fixed-size blocks called frames. The logical memory(from perspective of Process) is being split into fixed-size blocks called pages.
While the process is executing, the pages are mounted into appropriate frames if needed. Therefore, the process is called demand paging or paging on-demand.
A validation bit is a hardware support mechanism for verifying the page’s state. The validation bit is set for each record in the table of pages. The bit can take only two possible values: 0 or 1.
Initially, all bits are set to 0. It means that the page isn’t present in the random access memory or the reference is invalid. On the other hand, when the bit is set to 1, the reference is valid and the page is present. If the bit is set to 0 during the address translation, the situation it’s called a page fault.
When the page fault occurs the processor transfers the control to the operating system. Firstly, the system locates the required data on the disk. Secondly, it looks for a free frame in the random access memory. Then, it loads the page into that frame. After that, it updates the table of pages. Finally, the process continues to execute.
It’s possible that there aren’t any free frames in the RAM. Therefore there is a need to free up some. Page Replacement Strategies
Segmentation is the process of splitting the physical memory into continuous blocks called segments. The segments can be of different sizes. Therefore logical address is represented by two values: a segment number and an offset.
The segment number is an index in the table of segments. Each record in that table has a base address which refers to the beginning of the segment in the physical memory. Moreover, the segment has a limit value that points to the end of the segment in the physical memory. Subsequently, the offset is a value between 0 and the limit.