Learning outcomes
- Discuss the concept of virtual memory and how demand paging works.
These notes explain three closely related memory-management concepts: paged segmentation, virtual memory, and demand paging. These techniques help an operating system manage large programs efficiently, protect memory, and allow processes to run even when the complete program is not present in main memory.
Paged segmentation is a hybrid memory-management technique that combines the logical advantages of segmentation with the physical-memory efficiency of paging.
In segmentation, a program is divided into meaningful logical parts such as code, data, stack, heap, and procedures. However, pure segmentation suffers from external fragmentation because segments are variable in size.
In paging, memory is divided into fixed-size pages and frames. Paging removes external fragmentation, but it does not represent the programmer’s logical view very naturally.
Paged segmentation is used to achieve both:
This means the operating system can protect and share logical program units, while still avoiding the major external-fragmentation problem of pure segmentation.
In paged segmentation, the logical address usually contains three parts:
| Address Part | Meaning |
|---|---|
| Segment Number | Identifies the logical segment, such as code segment, data segment, or stack segment. |
| Page Number | Identifies the page inside the selected segment. |
| Offset | Identifies the exact byte or word inside the selected page. |
Protection can be applied at two levels:
This makes paged segmentation powerful for systems where different parts of a program require different levels of protection.
Suppose a logical address is:
Segment table entry for Segment 2 points to Page Table PT2. In PT2, Page 3 maps to Frame 8.
If frame size is 1024 bytes:
Virtual memory is a memory-management technique that allows a process to execute even when the entire process is not present in main memory.
The process sees a large continuous logical address space, but physically, only some required parts of the process are kept in RAM. The remaining parts are stored on secondary storage, usually disk.
Without virtual memory, a complete program must be loaded into RAM before execution. This creates problems when:
Virtual memory solves this by loading only required parts of the program into memory.
| Memory Type | Meaning |
|---|---|
| Logical / Virtual Memory | The memory space seen by the process. It may be much larger than actual RAM. |
| Physical Memory | The actual RAM available in the computer system. |
| Backing Store | Disk area used to store pages that are not currently in RAM. |
Virtual memory works efficiently because most programs follow the principle of locality of reference.
This means that at a given time, a program tends to use only a small part of its code and data repeatedly. For example, inside a loop, the same few instructions and variables may be accessed again and again.
| Type of Locality | Meaning | Example |
|---|---|---|
| Temporal Locality | Recently used items are likely to be used again soon. | A loop variable accessed repeatedly. |
| Spatial Locality | Nearby memory locations are likely to be accessed soon. | Sequential access of array elements. |
Demand paging is a virtual-memory technique in which a page is loaded into main memory only when it is actually needed.
Instead of loading the complete process into RAM, the OS loads only those pages that are demanded during execution.
At the beginning, only a few pages of a process may be present in RAM. The remaining pages stay on disk. If the process tries to access a page that is not in RAM, the hardware generates a page fault. The OS then brings that page into memory.
Demand paging uses a valid-invalid bit in the page table.
| Bit Value | Meaning |
|---|---|
| Valid | The page is legal and currently present in main memory. |
| Invalid | The page is either not part of the process address space or is currently not in memory. |
A page fault occurs when a process refers to a page that is not currently present in RAM. In demand paging, page faults are normal events, not always errors.
However, the OS must check whether the reference is legal:
In pure demand paging, no page is loaded until it is actually referenced. A process may start execution with very few pages in memory.
This saves memory, but it may cause many page faults at the beginning of execution. Therefore, some systems may use limited prepaging, where a few likely-needed pages are loaded in advance.
Demand paging is efficient only when the page-fault rate is low. A normal memory access is very fast, but a page fault may require disk I/O, which is much slower.
Here:
Suppose a process has pages 0, 1, 2, 3, and 4 in its logical address space. Pages 0 and 3 are currently in RAM. Pages 1, 2, and 4 are on disk.
| Page | Status |
|---|---|
| Page 0 | In RAM |
| Page 1 | On Disk |
| Page 2 | On Disk |
| Page 3 | In RAM |
| Page 4 | On Disk |
If the CPU references Page 3, no page fault occurs because it is already in RAM.
If the CPU references Page 2, a page fault occurs. Since Page 2 is a legal page, the OS loads it from disk into a free frame.
If the CPU references Page 8, this is an illegal reference because Page 8 is not part of the process address space.
| Feature | Paged Segmentation | Virtual Memory | Demand Paging |
|---|---|---|---|
| Main idea | Segments are divided into pages. | Process can run without being fully loaded into RAM. | Pages are loaded only when referenced. |
| Purpose | Combine segmentation and paging benefits. | Create illusion of large memory. | Implement virtual memory efficiently. |
| Address format | Segment number, page number, offset. | Virtual address translated to physical address. | Virtual page mapped when demanded. |
| Main benefit | Logical structure with reduced fragmentation. | Runs larger programs and improves memory use. | Avoids loading unused pages. |
| Main drawback | Complex address translation. | Page faults can slow the system. | High page-fault rate causes poor performance. |
Paged segmentation is a hybrid memory-management technique in which a program is divided into logical segments, and each segment is further divided into pages. It combines the logical organization of segmentation with the efficient allocation of paging.
Virtual memory allows a process to execute even when the entire process is not present in main memory. It separates the logical address space from physical memory and gives the user the illusion of a large memory.
Demand paging is a virtual-memory technique where pages are loaded into memory only when they are actually needed. If a required page is not in RAM, a page fault occurs, and the operating system brings the page from disk into memory.
These techniques are central to modern operating systems because they improve memory utilization, support large programs, allow better multiprogramming, and provide memory protection.