Paging is a memory management strategy in operating systems.
Demand Paging is a virtual memory management strategy.
The basic method of implementing paging involves breaking physical memory into fixed-sized blocks called frames and breaking logical memory into blocks of same size called pages. When a process is to be executed, its pages are loaded into any available memory frames, and a contiguous memory space is not needed hence. Paging is carried out by the Memory Management Unit.
Advantages of paging
- Eliminates the need of a contiguous memory for a process
- Avoids external fragmentation ( occurs when a larger contiguous memory is allocated to a process than needed)
Disadvantages of paging
- Internal fragmentation still exists.
- Memory reference overhead: 2 references into the memory ( 1 for the page table and 1 for the frame)
How is paging done?
Every address generated by the CPU is divided into page number and page offset. The page number is used as an index into the page table. The page table for a process consists of all pages of the process and also shows in which frame every page exists.
A demand-paging system is similar to a paging system with swapping. Swapping is the process of rolling in and rolling out of memory from the main memory. Swapping is essentially having only the needed information in the memory and putting the rest in a secondary storage.
Imagine a process as a collection of a large number of pages. All pages together form the virtual memory. Now, not all pages are needed at once at any point for the process. For example, login authentication thread does not need the “save” thread. Hence, demand paging suggests the operating system to put only relevant pages into the physical memory and give the user an illusion of a larger non-existent virtual memory.
When the need for a new page arises, existent ‘not-much-needed’ pages can be swapped out.
How is demand paging carried out?
- Few pages are loaded into the memory.
- Needed pages are in main memory. Other pages are in secondary storage.
- When a required page is not in memory, the page search in the main memory fails and a page fault occurs.
- Only when a page fault occurs, is the requested page taken from the secondary memory. This new page is swapped with another page( preferably a page that wont be needed in the near future).
Advantages of demand paging
- More space in physical memory. Hence more processes can be run in the same memory
- Less loading latency occurs at program start-up, as less information is accessed from secondary storage and less information is brought into main memory.
Disadvantages of demand paging
- A page fault results in look up into the secondary storage. The secondary storage is slower, hence, the process is slower. This is one trade-off for memory.
- Needs to be secure from timed attacks.