Unit 3 (Paging To Thrashing)
Unit 3 (Paging To Thrashing)
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.
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.
Paging
Page 1 of 11
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.
Paging
Page 2 of 11
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.
Memory management unit of OS needs to convert the page number to the frame number.
The hardware support for paging is illustrated in above figure Every address generated the CPU is
divided into two parts: a page no.(p) and a page offset (d). The page number is used as an index into a
page table contains the base address of each page in physical memory. This base address is combined
with the page offset to define the physical memory address that is sent to the memory unit.
Segmentation:
What is Segmentation?
Segmentation method works almost similarly to paging, only difference between the two is that
segments are of variable-length whereas, in the paging method, pages are always of fixed size.
The details about each segment are stored in a table called as segment table. Segment table is stored in
one (or many) of the segments.
Segment table contains mainly two information about segment:
Till now, we were using Paging as our main memory management technique. Paging is more
close to Operating system rather than the User. It divides all the process into the form of pages
regardless of the fact that a process can have some relative parts of functions which needs to be
loaded in the same page.
Operating system doesn't care about the User's view of the process. It may divide the same
function into different pages and those pages may or may not be loaded at the same time into the
memory. It decreases the efficiency of the system.
It is better to have segmentation which divides the process into the segments. Each segment
contain same type of functions such as main function can be included in one segment and the
library functions can be included in the other segment,
The Segment number is mapped to the segment table. The limit of the respective segment is
compared with the offset. If the offset is less than the limit then the address is valid otherwise it
throws an error as the address is invalid.
In the case of valid address, the base address of the segment is added to the offset to get the
physical address of actual word in the main memory.
Advantages of Segmentation
1. No internal fragmentation
2. Average Segment Size is larger than the actual page size.
3. Less overhead
4. It is easier to relocate segments than entire address space.
5. The segment table is of lesser size as compare to the page table in paging.
Disadvantages
1. It can have external fragmentation.
2. it is difficult to allocate contiguous memory to variable sized partition.
3. Costly memory management algorithms.
Paging VS Segmentation
Virtual memory is a storage mechanism which offers user an illusion of having a very big
main memory. It is done by treating a part of secondary memory as the main memory. In
Virtual memory, the user can store processes with a bigger size than the available main
memory.
Therefore, instead of loading one long process in the main memory, the OS loads the
various parts of more than one process in the main memory. Virtual memory is mostly
implemented with demand paging and demand segmentation.
Demand paging
A demand paging mechanism is very much similar to a paging system with swapping
where processes stored in the secondary memory and pages are loaded only on demand,
not in advance.
So, when a context switch occurs, the OS never copy any of the old program’s pages from
the disk or any of the new program’s pages into the main memory. Instead, it will start
executing the new program after loading the first page and fetches the program’s pages,
which are referenced.
During the program execution, if the program references a page that may not be available
in the main memory because it was swapped, then the processor considers it as an invalid
memory reference. That’s because the page fault and transfers send control back from the
program to the OS, which demands to store page back into the memory.
The process of loading the page into memory on demand (whenever page fault occurs) is known
as demand paging.
The process includes the following steps :
1. If the CPU tries to refer to a page that is currently not available in the main memory, it
generates an interrupt indicating a memory access fault.
2. The OS puts the interrupted process in a blocking state. For the execution to proceed the OS
must bring the required page into the memory.
3. The OS will search for the required page in the logical address space.
4. The required page will be brought from logical address space to physical address space. The
page replacement algorithms are used for the decision-making of replacing the page in
physical address space.
5. The page table will be updated accordingly.
6. The signal will be sent to the CPU to continue the program execution and it will place the
process back into the ready state.
Page Replacement
The page replacement algorithm decides which memory page is to be replaced. The process of
replacement is sometimes called swap out or write to disk. Page replacement is done when the
requested page is not found in the main memory (page fault).
As studied in Demand Paging, only certain pages of a process are loaded initially into the memory.
This allows us to get more number of processes into memory at the same time but what happens
when a process requests for more pages and no free memory is available to bring them in.
Following steps can be taken to deal with this problem:
1. Put the process in the wait queue, until any other process finishes its execution thereby
freeing frames.
2. Or, remove some other process completely from the memory to free frames.
3. Or, find some pages that are not being used right now, move them to the disk to get free
frames. This technique is called Page replacement and is most commonly used. We have
some great algorithms to carry on page replacement efficiently.
FIFO
Optimal Algorithm
LRU Page Replacement
Features:
Whenever a new page loaded, the page recently comes in the memory is removed. So, it is
easy to decide which page requires to be removed as its identification number is always at
the FIFO stack.
The oldest page in the main memory is one that should be selected for replacement first.
Optimal Algorithm
The optimal page replacement method selects that page for a replacement for which the time to the
next reference is the longest.
Features:
Optimal algorithm results in the fewest number of page faults. This algorithm is difficult to
implement.
An optimal page-replacement algorithm method has the lowest page-fault rate of all
algorithms. This algorithm exists and which should be called MIN or OPT.
Replace the page which unlike to use for a longer period of time. It only uses the time
when a page needs to be used.
LRU Page Replacement
The full form of LRU is the Least Recently Used page. This method helps OS to find page usage
over a short period of time. This algorithm should be implemented by associating a counter with
an even- page.
Page, which has not been used for the longest time in the main memory, is the one that will
be selected for replacement.
Easy to implement, keep a list, replace pages by looking back into time.
Features:
The LRU replacement method has the highest count. This counter is also called aging
registers, which specify their age and how much their associated pages should also be
referenced.
The page which hasn’t been used for the longest time in the main memory is the one that
should be selected for replacement.
It also keeps a list and replaces pages by looking back into time.
Thrashing
A process that is spending more time paging than executing is said to be thrashing. In other words,
it means, that the process doesn't have enough frames to hold all the pages for its execution, so it is
swapping pages in and out very frequently to keep executing. Sometimes, the pages which will be
required in the near future have to be swapped out.
Initially, when the CPU utilization is low, the process scheduling mechanism, to increase the level
of multiprogramming loads multiple processes into the memory at the same time, allocating a
limited amount of frames to each process. As the memory fills up, the process starts to spend a lot
of time for the required pages to be swapped in, again leading to low CPU utilization because most
of the processes are waiting for pages. Hence the scheduler loads more processes to increase CPU
utilization, as this continues at a point of time the complete system comes to a stop.
Advantages of Virtual Memory
1. The degree of Multiprogramming will be increased.
2. User can run large application with less real RAM.
3. There is no need to buy more memory RAMs.