0% found this document useful (0 votes)
150 views11 pages

Understanding Paging in Operating Systems

Paging is a storage mechanism used in operating systems to retrieve processes from secondary storage into main memory in fixed-size pages. The main memory and processes are divided into pages and frames of equal size. When a process needs a page, it is loaded from secondary storage into an available frame. If no frames are available, a page replacement algorithm must select a page to remove from memory and write back to secondary storage to make room for the new page. Common page replacement algorithms are first-in, first-out (FIFO) and least recently used (LRU).

Uploaded by

Bijal patel
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)
150 views11 pages

Understanding Paging in Operating Systems

Paging is a storage mechanism used in operating systems to retrieve processes from secondary storage into main memory in fixed-size pages. The main memory and processes are divided into pages and frames of equal size. When a process needs a page, it is loaded from secondary storage into an available frame. If no frames are available, a page replacement algorithm must select a page to remove from memory and write back to secondary storage to make room for the new page. Common page replacement algorithms are first-in, first-out (FIFO) and least recently used (LRU).

Uploaded by

Bijal patel
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/ 11

Paging

In Operating Systems, Paging is a storage mechanism used to retrieve processes from the secondary storage into the
main memory in the form of pages.

The main idea behind the paging is to divide each process in the form of pages. The main memory will also be divided
in the form of frames.

One page of the process is to be stored in one of the frames of the memory. The pages can be stored at the different
locations of the memory but the priority is always to find the contiguous frames or holes.

Pages of the process are brought into the main memory only when they are required otherwise they reside in the
secondary storage.

Different operating system defines different frame sizes. The sizes of each frame must be equal. Considering the fact
that the pages are mapped to the frames in Paging, page size needs to be as same as frame size.

Example

Let us consider the main memory size 16 Kb and Frame size is 1 KB therefore the main memory will be divided into
the collection of 16 frames of 1 KB each.
There are 4 processes in the system that is P1, P2, P3 and P4 of 4 KB each. Each process is divided into pages of 1 KB
each so that one page can be stored in one frame.

Initially, all the frames are empty therefore pages of the processes will get stored in the contiguous way.

Frames, pages and the mapping between the two is shown in the image below.

Let us consider that, P2 and P4 are moved to waiting state after some time. Now, 8 frames become empty and therefore
other pages can be loaded in that empty place. The process P5 of size 8 KB (8 pages) is waiting inside the ready queue.

Given the fact that, we have 8 non contiguous frames available in the memory and paging provides the flexibility of
storing the process at the different places. Therefore, we can load the pages of process P5 in the place of P2 and P4.
Memory Management Unit: The purpose of Memory Management Unit (MMU) is to convert the logical address into
the physical address. The logical address is the address generated by the CPU for every page while the physical address
is the actual address of the frame where each page will be stored.

When a page is to be accessed by the CPU by using the logical address, the operating system needs to obtain the
physical address to access that page physically.

The logical address has two parts.Page Number

1. Offset

Memory management unit of OS needs to convert the page number to the frame number.
Address generated by CPU is divided into
 Page number(p): Number of bits required to represent the pages in Logical Address Space or Page number
 Page offset(d): Number of bits required to represent particular word in a page or page size of Logical Address
Space or word number of a page or page offset.

Eg. Imagine that you're trying to locate a house in a city. Imagine that each house Was given a unique number - you
can imagine that the number of houses would Soon get very large and confusing. Now imagine that you introduce the
concept Of streets - the house numbers now become a bit more manageable as you've Grouped them into chunks. So:
Streets = Page number, house number = Offset address

To be technical,

Displacement within a particular segment.

Physical Address is divided into


 Frame number(f): Number of bits required to represent the frame of Physical Address Space or Frame number.
 Frame offset(d): Number of bits required to represent particular word in a frame or frame size of Physical
Address Space or word number of a frame or frame offset.

Paging and Segmentation


Demand Paging Segmentation

Demand paging is identical to the paging system Segmentation is the arrangement of memory
with swapping. management
In demand paging, a page is delivered into the According to the segmentation the logical
memory on demand i.e., only when a reference is address space is a collection of segments.
made to a location on that page.
Each Page has its P bit, M bit, page ID and Page Each segment has a name and length
Offset
Demand paging combines the feature of simple Each logical address have two quantities segment
paging and implement virtual memory as it has a name and the segment offset, for simplicity we
large virtual memory. Lazy swapper concept is use the segment number in place of segment
implemented in demand paging in which a page name.
is not swapped into the memory unless it is
required.
Page Replacement Algorithms
A computer system has a limited amount of memory. Adding more memory physically is very costly.
Therefore most modern computers use a combination of both hardware and software to allow the computer to
address more memory than the amount physically present on the system. This extra memory is actually
called Virtual Memory.

Virtual Memory is a storage allocation scheme used by the Memory Management Unit(MMU) to compensate
for the shortage of physical memory by transferring data from RAM to disk storage. It addresses secondary
memory as though it is a part of the main memory. Virtual Memory makes the memory appear larger than
actually present which helps in the execution of programs that are larger than the physical memory.

Virtual Memory can be implemented using two methods:


 Paging
 Segmentation

Paging
 Paging is a process of reading data from, and writing data to, the secondary storage. It is a memory
management scheme that is used to retrieve processes from the secondary memory in the form of
pages and store them in the primary memory. The main objective of paging is to divide each process
in the form of pages of fixed size. These pages are stored in the main memory in frames. Pages of a
process are only brought from the secondary memory to the main memory when they are needed.

 When an executing process refers to a page, it is first searched in the main memory. If it is not present
in the main memory, a page fault occurs.

 ** Page Fault is the condition in which a running process refers to a page that is not loaded in the
main memory.

 In such a case, the OS has to bring the page from the secondary storage into the main memory. This
may cause some pages in the main memory to be replaced due to limited storage. A Page
Replacement Algorithm is required to decide which page needs to be replaced.

Page Replacement Algorithm


 Page Replacement Algorithm decides which page to remove, also called swap out when a new page
needs to be loaded into the main memory. Page Replacement happens when a requested page is not
present in the main memory and the available space is not sufficient for allocation to the requested
page.
 When the page that was selected for replacement was paged out, and referenced again, it has to read in
from disk, and this requires for I/O completion. This process determines the quality of the page
replacement algorithm: the lesser the time waiting for page-ins, the better is the algorithm.
 A page replacement algorithm tries to select which pages should be replaced so as to minimize the
total number of page misses. There are many different page replacement algorithms. These algorithms
are evaluated by running them on a particular string of memory reference and computing the number
of page faults. The fewer is the page faults the better is the algorithm for that situation.
 ** If a process requests for page and that page is found in the main memory then it is called page hit,
otherwise page miss or page fault.

Some Page Replacement Algorithms :

 First In First Out (FIFO)


 Least Recently Used (LRU)
 Optimal Page Replacement
1. First In First Out (FIFO):

This is the simplest page replacement algorithm. In this algorithm, the operating system keeps track of
all pages in the memory in a queue, the oldest page is in the front of the queue. When a page needs to
be replaced page in the front of the queue is selected for removal.
Example 1: Consider page reference string 1, 3, 0, 3, 5, 6, 3 with 3 page frames. Find the number of
page faults.

 Initially, all slots are empty, so when 1, 3, 0 came they are allocated to the empty slots —> 3 Page
Faults.
when 3 comes, it is already in memory so —> 0 Page Faults. Then 5 comes, it is not available in
memory so it replaces the oldest page slot i.e 1. —>1 Page Fault. 6 comes, it is also not available in
memory so it replaces the oldest page slot i.e 3 —>1 Page Fault. Finally, when 3 come it is not
available so it replaces 0 1 page fault.

 Belady’s anomaly proves that it is possible to have more page faults when increasing the number of
page frames while using the First in First Out (FIFO) page replacement algorithm. For example, if we
consider reference strings 3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4, and 3 slots, we get 9 total page faults, but if
we increase slots to 4, we get 10-page faults.

Advantages

 Simple and easy to implement.


 Low overhead.
Disadvantages

 Poor performance.
 Doesn’t consider the frequency of use or last used time, simply replaces the oldest page.
 Suffers from Belady’s Anomaly(i.e. more page faults when we increase the number of page frames).

2. Optimal Page replacement:


In this algorithm, pages are replaced which would not be used for the longest duration of time in the
future.

Example-2: Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, with 4 page frame. Find


number of page fault.
 Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults
0 is already there so —> 0 Page fault. when 3 came it will take the place of 7 because it is not used
for the longest duration of time in the future.—>1 Page fault. 0 is already there so —> 0 Page
fault. 4 will takes place of 1 —> 1 Page Fault.

 Now for the further page reference string —> 0 Page fault because they are already available in the
memory.
Optimal page replacement is perfect, but not possible in practice as the operating system cannot know
future requests. The use of Optimal Page replacement is to set up a benchmark so that other
replacement algorithms can be analyzed against it.

Advantages

 Easy to Implement.

 Simple data structures are used.

 Highly efficient.

Disadvantages

 Requires future knowledge of the program.

 Time-consuming.

3. Least Recently Used:


In this algorithm, page will be replaced which is least recently used.

Example-3: Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2 with 4 page frames.


Find number of page faults.
 Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults
0 is already their so —> 0 Page fault. when 3 came it will take the place of 7 because it is least
recently used —>1 Page fault
0 is already in memory so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault
Now for the further page reference string —> 0 Page fault because they are already available in the
memory.

Advantages

 Efficient.
 Doesn't suffer from Belady’s Anomaly.
Disadvantages

 Complex Implementation.
 Expensive.
 Requires hardware support.

Segmentation
A process is divided into Segments. The chunks that a program is divided into which are not necessarily all of
the same sizes are called segments.
There are types of segmentation:
1. Virtual memory segmentation –
Each process is divided into a number of segments, not all of which are resident at any one point in time.
2. Simple segmentation –
Each process is divided into a number of segments, all of which are loaded into memory at run time,
though not necessarily contiguously.

There is no simple relationship between logical addresses and physical addresses in segmentation. A table
stores the information about all such segments and is called Segment Table.
Segment Table – It maps two-dimensional Logical address into one-dimensional Physical address. It’s each
table entry has:
 Base Address: It contains the starting physical address where the segments reside in memory.
 Limit: It specifies the length of the segment.

Address generated by the CPU is divided into:


 Segment number (s): Number of bits required to represent the segment.
 Segment offset (d): Number of bits required to represent the size of the segment.
Translation of Two dimensional Logical Address to one dimensional Physical Address.
Advantages of Segmentation –
 No Internal fragmentation.
 Segment Table consumes less space in comparison to Page table in paging.

Disadvantage of Segmentation –
 As processes are loaded and removed from the memory, the free memory space is broken into little pieces,
causing External fragmentation.
 Segments of unequal size are not suited for swapping.
 There is an overhead of maintaining a segment table for each process.

You might also like