0% found this document useful (0 votes)
10 views18 pages

Paging

The document discusses non-contiguous memory allocation in operating systems, focusing on paging as a fixed-size partitioning scheme that divides both secondary and main memory into pages and frames. It explains the process of translating logical addresses to physical addresses using a page table and outlines the advantages and disadvantages of paging. Additionally, it covers demand paging, its operational steps, page replacement algorithms, and numerical examples for FIFO and LRU page replacement methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views18 pages

Paging

The document discusses non-contiguous memory allocation in operating systems, focusing on paging as a fixed-size partitioning scheme that divides both secondary and main memory into pages and frames. It explains the process of translating logical addresses to physical addresses using a page table and outlines the advantages and disadvantages of paging. Additionally, it covers demand paging, its operational steps, page replacement algorithms, and numerical examples for FIFO and LRU page replacement methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Paging in operating system

5 February, 2025
Non-Contiguous Memory Allocation
Non-contiguous memory allocation is a memory allocation technique.
It allows to store parts of a single process in a non-contiguous fashion.
Thus, different parts of the same process can be stored at different places in
the main memory.

There are two popular techniques used for non-contiguous memory allocation :
1. Paging
2. Segmentation
Paging
Paging is a fixed size partitioning scheme.
In paging, secondary memory and main memory are divided into equal fixed
size partitions.
The partitions of secondary memory are called as pages.
The partitions of main memory are called as frames.
Each process is divided into parts where size of each part is same as page size.
The size of the last part may be less than the page size.
The pages of process are stored in the frames of main memory depending
upon their availability.
Translating Logical Address into Physical Address
CPU always generates a logical address.
A physical address is needed to access the main memory.

Following steps are followed to translate logical address into physical address-

Step-01:

CPU generates a logical address consisting of two parts-


1. Page Number
2. Page Offset

Page Number specifies the specific page of the process from which CPU wants to read the data.
Page Offset specifies the specific word on the page that CPU wants to read.
Step-02:

For the page number generated by the CPU,


Page Table provides the corresponding frame number (base address of the
frame) where that page is stored in the main memory.

Here, Page table is a data structure.


It maps the page number referenced by the CPU to the frame number where
that page is stored.
Page table is stored in the main memory.
Number of entries in a page table = Number of pages in which the process is
divided.
Page Table Base Register (PTBR) contains the base address of page table.
Each process has its own independent page table.
Page Table Base Register (PTBR) provides the base address of the page table.
The base address of the page table is added with the page number referenced by
the CPU.
It gives the entry of the page table containing the frame number where the
referenced page is stored.

Step-03:

The frame number combined with the page offset forms the required physical
address.

Frame number specifies the specific frame where the required page is stored.
Page Offset specifies the specific word that has to be read from that page.
The advantages of paging are-
It allows to store parts of a single process in a non-contiguous fashion.
It solves the problem of external fragmentation.

The disadvantages of paging are-


It suffers from internal fragmentation.
There is an overhead of maintaining a page table for each process.
The time taken to fetch the instruction increases since now two memory accesses
are required.
Demand Paging
Demand paging is a technique used in virtual memory systems where pages enter
main memory only when requested or needed by the CPU. In demand paging, the
operating system loads only the necessary pages of a program into memory at
runtime, instead of loading the entire program into memory at the start. A page
fault occurred when the program needed to access a page that is not currently in
memory.
The operating system then loads the required pages from the disk into memory
and updates the page tables accordingly.
Working of Demand Paging
The operating system‘s demand paging mechanism follows a few steps in its operation.
Program Execution: Upon launching a program, the operating system allocates a
certain amount of memory to the program and establishes a process for it.
Creating Page Tables: To keep track of which program pages are currently in memory
and which are on disk, the operating system makes page tables for each process.
Handling Page Fault: When a program tries to access a page that isn’t in memory at the
moment, a page fault happens. In order to determine whether the necessary page is on
disk, the operating system pauses the application and consults the page tables.
Page Fetch: The operating system loads the necessary page into memory by retrieving
it from the disk if it is there.
The page’s new location in memory is then reflected in the page table.
Resuming The Program: The operating system picks up where it left off when the
necessary pages are loaded into memory.
Page Replacement: If there is not enough free memory to hold all the pages a
program needs, the operating system may need to replace one or more pages
currently in memory with pages currently in memory. on the disk. The page
replacement algorithm used by the operating system determines which pages
are selected for replacement.

Page Cleanup: When a process terminates, the operating system frees the
memory allocated to the process and cleans up the corresponding entries in
the page tables.
A page replacement algorithm is used only when memory is full and a new page needs to be
loaded. The OS must replace an existing page using algorithms like:

FIFO (First-In-First-Out): Replaces the oldest page in memory with a new one. It’s simple but
can cause issues if pages are frequently swapped in and out, leading to thrashing.

LRU (Least Recently Used): Replaces the page that hasn’t been used for the longest time. It
reduces thrashing more effectively than FIFO but is more complex to implement.

Optimal Page Replacement is one of the Algorithms of Page Replacement. In this algorithm,
pages are replaced which would not be used for the longest duration of time in the future.

The idea is simple, for every reference we do following:


If referred page is already present, increment hit count.
If not present, find if a page that is never referenced in future. If such a page exists, replace
this page with new page. If no such page exists, find a page that is referenced farthest in
future. Replace this page with new page.
Numerical of FIFO (First-In-First-Out)
Consider the reference string 6, 1, 1, 2, 0, 3, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3, 2, 1, 2, 0 for a
memory with three frames and calculate number of page faults by using FIFO (First In
First Out) Page replacement algorithms. Also calculate Hit ratio and miss ratio
Number of Page Hits = 8
Number of Page Faults = 12

Hit Ratio = Number of page hits / Total number of pages = 8/20 = 0.4

Miss Ratio = Number of page miss / Total number of pages = 12/20 = 0.6
Numerical of LRU
Consider the reference string 1, 2, 3, 4, 5, 1, 3, 1, 6, 3, 2, 3, for a memory with four
frames and calculate number of page faults by using LRU Page replacement
algorithms. Also calculate Hit ratio and miss ratio
Total Page Fault = 8
Total Page Hit = 4

Hit Ratio = Number of page hits / Total number of pages = 8/12 = 0.67

Miss Ratio = Number of page miss / Total number of pages = 4/12 = 0.33
Numerical of Optimal

You might also like