Operating System
Operating System
Operating System
The purpose of the swapping in operating system is to access the data present in the
hard disk and bring it to RAM so that the application programs can use it. The thing to
remember is that swapping is used only when data is not present in RAM.
Although the process of swapping affects the performance of the system, it helps to run
larger and more than one process. This is the reason why swapping is also referred to as
memory compaction.
The concept of swapping has divided into two more concepts: Swap-in and Swap-out.
o Swap-out is a method of removing a process from RAM and adding it to the hard
disk.
o Swap-in is a method of removing a program from a hard disk and putting it back
into the main memory or RAM.
Example: Suppose the user process's size is 2048KB and is a standard hard disk where
swapping has a data transfer rate of 1Mbps. Now we will calculate how long it will take
to transfer from main memory to secondary memory.
Advantages of Swapping
1. It helps the CPU to manage multiple processes within a single main memory.
2. It helps to create and use virtual memory.
3. Swapping allows the CPU to perform multiple tasks simultaneously. Therefore,
processes do not have to wait very long before they are executed.
4. It improves the main memory utilization.
Disadvantages of Swapping
1. If the computer system loses power, the user may lose all information related to
the program in case of substantial swapping activity.
2. If the swapping algorithm is not good, the composite method can increase the
number of Page Fault and decrease the overall processing performance.
Note:
o In a single tasking operating system, only one process occupies the user program
area of memory and stays in memory until the process is complete.
o In a multitasking operating system, a situation arises when all the active
processes cannot coordinate in the main memory, then a process is swap out
from the main memory so that other processes can enter it.
Contiguous Allocation
If the blocks are allocated to the file in such a way that all the logical blocks of the file
get the contiguous physical block in the hard disk then such allocation scheme is known
as contiguous allocation.
In the image shown below, there are three files in the directory. The starting block and
the length of each file are mentioned in the table. We can check in the table that the
contiguous blocks are assigned to each file as per its need.
Advantages
1. It is simple to implement.
2. We will get Excellent read performance.
3. Supports Random Access into files.
Disadvantages
1. The disk will become fragmented.
2. It may be difficult to have a file grow.
Paging in OS (Operating System)
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.
Advertisement
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.
1. Page Number
2. Offset
Memory management unit of OS needs to convert the page number to the frame
number.
Example
Considering the above image, let's say that the CPU demands 10th word of 4th page of
process P3. Since the page number 4 of process P1 gets stored at frame number 9
therefore the 10th word of 9th frame will be returned as the physical address.
Segmentation in OS (Operating
System)
In Operating Systems, Segmentation is a memory management technique in which the
memory is divided into the variable size parts. Each part is known as a segment which
can be allocated to a process.
The details about each segment are stored in a table called a segment table. Segment
table is stored in one (or many) of the segments.
It is better to have segmentation which divides the process into the segments. Each
segment contains the same type of functions such as the main function can be included
in one segment and the library functions can be included in the other segment.
1. Segment Number
2. Offset
For Example:
Suppose a 16 bit address is used with 4 bits for the segment number and 12 bits for the
segment offset so the maximum segment size is 4096 and the maximum number of
segments that can be refereed is 16.
When a program is loaded into memory, the segmentation system tries to locate space
that is large enough to hold the first segment of the process, space information is
obtained from the free list maintained by memory manager. Then it tries to locate space
for other segments. Once adequate space is located for all the segments, it loads them
into their respective areas.
The operating system also generates a segment map table for each program.
With the help of segment map tables and hardware assistance, the operating system
can easily translate a logical address into physical address on execution of a program.
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 addresses, the base address of the segment is added to the offset to
get the physical address of the actual word in the main memory.
The above figure shows how address translation is done in case of segmentation.
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 compared 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
Segmented Paging
Pure segmentation is not very popular and not being used in many of the operating
systems. However, Segmentation can be combined with Paging to get the best features
out of both the techniques.
In Segmented Paging, the main memory is divided into variable size segments which are
further divided into fixed size pages.
Each Page table contains the various information about every page of the segment. The
Segment Table contains the information about every segment. Each segment table entry
points to a page table entry and every page table entry is mapped to one of the page
within a segment.
However, deciding, which pages need to be kept in the main memory and which need
to be kept in the secondary memory, is going to be difficult because we cannot say in
advance that a process will require a particular page at particular time.
Whenever any page is referred for the first time in the main memory, then that page will
be found in the secondary memory.
Advertisement
After that, it may or may not be present in the main memory depending upon the page
replacement algorithm which will be covered later in this tutorial.
What is Thrashing?
If the number of page faults is equal to the number of referred pages or the number of
page faults are so high so that the CPU remains busy in just reading the pages from the
secondary memory then the effective access time will be the time taken by the CPU to
read one word from the secondary memory and it will be so high. The concept is called
thrashing.
If the page fault rate is PF %, the time taken in getting a page from the secondary
memory and again restarting is S (service time) and the memory access time is ma then
the effective access time can be given as;
In this scheme, User can load the bigger size processes than the available main memory
by having the illusion that the memory is available to load the process.
Instead of loading one big process in the main memory, the Operating System loads the
different parts of more than one process in the main memory.
By doing this, the degree of multiprogramming will be increased and therefore, the CPU
utilization will also be increased.
Advertisement
Since all this procedure happens automatically, therefore it makes the computer feel like
it is having the unlimited RAM.
Demand Paging
Demand Paging is a popular method of virtual memory management. In demand
paging, the pages of a process which are least used, get stored in the secondary
memory.
A page is copied to the main memory when its demand is made or page fault occurs.
There are various page replacement algorithms which are used to determine the pages
which will be replaced. We will discuss each one of them later in detail.
The page tables of both the pages are 1 KB size each and therefore they can be fit in
one frame each. The page tables of both the processes contain various information that
is also shown in the image.
The CPU contains a register which contains the base address of page table that is 5 in
the case of P1 and 7 in the case of P2. This page table base address will be added to the
page number of the Logical address when it comes to accessing the actual
corresponding entry.
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.
After filling up of the frames, the next page in the waiting queue tries to enter the frame.
If the frame is present then, no problem is occurred. Because of the page which is to be
searched is already present in the allocated frames.
If the page to be searched is found among the frames then, this process is known as
Page Hit.
If the page to be searched is not found among the frames then, this process is known as
Page Fault.
When Page Fault occurs this problem arises, then the First In First Out Page
Replacement Algorithm comes into picture.
The First In First Out (FIFO) Page Replacement Algorithm removes the Page in the frame
which is allotted long back. This means the useless page which is in the frame for a
longer time is removed and the new page which is in the ready queue and is ready to
occupy the frame is allowed by the First In First Out Page Replacement.
Let us understand this First In First Out Page Replacement Algorithm working with the
help of an example.
Example:
Points to Remember
Page Not Found - - - > Page Fault
Reference String:
The Ratio of Page Hit to the Page Fault = 8 : 12 - - - > 2 : 3 - - - > 0.66
The Page Fault Percentage = 100 - Page Hit Percentage = 100 - 40 = 60%
Explanation
First, fill the frames with the initial pages. Then, after the frames are filled we need to
create a space in the frames for the new page to occupy. So, with the help of First in
First Out Page Replacement Algorithm we remove the frame which contains the page is
older among the pages. By removing the older page we give access for the new frame
to occupy the empty space created by the First in First out Page Replacement Algorithm.
After filling up of the frames, the next page in the waiting queue tries to enter the frame.
If the frame is present then, no problem is occurred. Because of the page which is to be
searched is already present in the allocated frames.
If the page to be searched is found among the frames then, this process is known as
Page Hit.
If the page to be searched is not found among the frames then, this process is known as
Page Fault.
When Page Fault occurs this problem arises, then the OPTIMAL Page Replacement
Algorithm comes into picture.
The OPTIMAL Page Replacement Algorithms works on a certain principle. The principle
is:
Replace the Page which is not used in the Longest Dimension of time in future
This principle means that after all the frames are filled then, see the future pages which
are to occupy the frames. Go on checking for the pages which are already available in
the frames. Choose the page which is at last.
Example:
0, 3, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3, 2, 1, 2, 0
Now we need to enter 0 into the frame by removing one page from the page
From the sub sequence 0, 3, 4, 6, 0, 2, 1 we can say that 1 is the last occurring page
number. So we can say that 0 can be placed in the frame body by removing 1 from the
frame.
Let us understand this OPTIMAL Page Replacement Algorithm working with the help of
an example.
Example:
Points to Remember
Reference String:
The Ratio of Page Hit to the Page Fault = 8 : 12 - - - > 2 : 3 - - - > 0.66
The Page Fault Percentage = 100 - Page Hit Percentage = 100 - 40 = 60%
Explanation
First, fill the frames with the initial pages. Then, after the frames are filled we need to
create a space in the frames for the new page to occupy.
Here, we would fill the empty spaces with the pages we and the empty frames we have.
The problem occurs when there is no space for occupying of pages. We have already
known that we would replace the Page which is not used in the Longest Dimension of
time in future.
There comes a question what if there is absence of page which is in the frame.
0, 2, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3, 2, 1, 2, 0
Here, we can see that page number 5 is not present in the Reference String. But the
number 5 is present in the Frame. So, as the page number 5 is absent we remove it
when required and other page can occupy that position.
After filling up of the frames, the next page in the waiting queue tries to enter the frame.
If the frame is present then, no problem is occurred. Because of the page which is to be
searched is already present in the allocated frames.
If the page to be searched is found among the frames then, this process is known as
Page Hit.
If the page to be searched is not found among the frames then, this process is known as
Page Fault.
When Page Fault occurs this problem arises, then the Least Recently Used (LRU) Page
Replacement Algorithm comes into picture.
The Least Recently Used (LRU) Page Replacement Algorithms works on a certain
principle. The principle is:
Replace the page with the page which is less dimension of time recently used page in
the past.
Example:
6, 1, 1, 2, 0, 3, 4, 6, 0
The pages with page numbers 6, 1, 2 are in the frames occupying the frames.
Now, we need to travel back into the past to check which page can be replaced.
Let us understand this Least Recently Used (LRU) Page Replacement Algorithm working
with the help of an example.
Example:
Points to Remember
Reference String:
Number of Page Hits = 7
The Page Fault Percentage = 100 - Page Hit Percentage = 100 - 35 = 65%
Explanation
First, fill the frames with the initial pages. Then, after the frames are filled we need to
create a space in the frames for the new page to occupy.
Here, we would fill the empty spaces with the pages we and the empty frames we have.
The problem occurs when there is no space for occupying of pages. We have already
known that we would replace the Page which is not used in the Longest Dimension of
time in past or can be said as the Page which is very far away in the past.