Understanding Demand Paging and Segmentation in Operating Systems
Table of contents
Introduction
In modern operating systems, memory management is an essential aspect of providing efficient and reliable computing. Memory management involves allocating and deallocating memory to processes and managing the use of virtual memory. Two important techniques used in memory management are demand paging and segmentation.
Demand Paging
Demand paging is a memory management technique that enables the operating system to transfer pages of memory from the hard disk to the main memory only when they are needed by a process. This technique is used to reduce the amount of physical memory required by a process and to increase the overall system performance.
In a demand-paged system, when a process requests a page that is not currently in the main memory, the operating system loads the required page from the hard disk into the main memory. This process is called page fault, and it is a trap that is handled by the operating system. Once the required page is loaded, the process can continue execution as if the page was always present in the main memory.
Demand paging is an important technique because it allows the system to run more processes simultaneously than the available physical memory would otherwise allow. It also allows the system to swap pages in and out of the main memory as needed, which can help optimize the use of available memory.
Example
Suppose a process needs to execute and requires access to a large dataset, which is too big to fit in the available physical memory. In a demand-paged system, the operating system can load the necessary pages of the dataset from the hard disk to the main memory as needed. As the process continues executing, additional pages can be loaded from the hard disk as necessary. This way, the process can run without running out of memory, and other processes can continue to execute concurrently.
Segmentation
Segmentation is a memory management technique that enables the operating system to divide the logical address space of a process into several segments, which can be independently managed. Each segment represents a portion of the process's address space and can have its own size, protection, and sharing attributes.
In a segmented memory system, the operating system maintains a segment table that contains information about each segment of a process's address space. This table includes the starting address of the segment, the length of the segment, and the access control attributes for the segment.
Example
Suppose a process has a code segment, a data segment, and a stack segment. In a segmented memory system, each segment can be independently managed. The code segment can be marked read-only, while the data segment can be marked read-write. The stack segment can be marked with a special attribute that allows it to grow dynamically.
When the process needs to access a particular segment, the operating system uses the segment table to locate the starting address of the segment in the main memory. This way, the process can access the required segment without having to manage the entire address space of the process at once.
Conclusion
In conclusion, demand paging and segmentation are two important memory management techniques used in modern operating systems. Demand paging enables the system to optimize the use of physical memory by loading pages from the hard disk as needed. Segmentation enables the system to divide a process's address space into independent segments, each with its own attributes. Both techniques are critical for providing efficient and reliable memory management in modern operating systems.
Subscribe to my newsletter
Read articles from Harsh Mange directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Harsh Mange
Harsh Mange
This is Harsh Mange, working as a Software Engineer - Backend at Argoid. I love building apps. Working on contributing to open-source projects and the dev community.