UNIT4 Operating System
UNIT4 Operating System
Memory is the important part of the computer that is used to store the data. Its
management is critical to the computer system because the amount of main memory
available in a computer system is very limited. At any time, many processes are competing
for it. Moreover, to increase performance, several processes are executed simultaneously.
For this, we must keep several processes in the main memory, so it is even more important
to manage them effectively.
o Simple to implement.
o Easy to manage and design.
o In a Single contiguous memory management scheme, once a process is
loaded, it is given full processor's time, and no other processor will interrupt it.
Disadvantages of Single contiguous memory management schemes:
Multiple Partitioning:
The single Contiguous memory management scheme is inefficient as it limits computers
to execute only one program at a time resulting in wastage in memory space and CPU
time. The problem of inefficient CPU use can be overcome using multiprogramming that
allows more than one program to run concurrently. To switch between two processes, the
operating systems need to load both processes into the main memory. The operating
system needs to divide the available main memory into multiple parts to load multiple
processes into the main memory. Thus multiple processes can reside in the main memory
simultaneously.
o Fixed Partitioning
o Dynamic Partitioning
Fixed Partitioning
The main memory is divided into several fixed-sized partitions in a fixed partition memory
management scheme or static partitioning. These partitions can be of the same size or
different sizes. Each partition can hold a single process. The number of partitions
determines the degree of multiprogramming, i.e., the maximum number of processes in
memory. These partitions are made at the time of system generation and remain fixed
after that.
o Simple to implement.
o Easy to manage and design.
Disadvantages of Fixed Partitioning memory management schemes:
Dynamic Partitioning
The dynamic partitioning was designed to overcome the problems of a fixed partitioning
scheme. In a dynamic partitioning scheme, each process occupies only as much memory
as they require when loaded for processing. Requested processes are allocated memory
until the entire physical memory is exhausted or the remaining space is insufficient to hold
the requesting process. In this scheme the partitions used are of variable size, and the
number of partitions is not defined at the system generation time.
What is paging?
Paging is a technique that eliminates the requirements of contiguous allocation of main
memory. In this, the main memory is divided into fixed-size blocks of physical memory
called frames. The size of a frame should be kept the same as that of a page to maximize
the main memory and avoid external fragmentation.
Advantages of paging:
What is Segmentation?
Segmentation is a technique that eliminates the requirements of contiguous allocation of
main memory. In this, the main memory is divided into variable-size blocks of physical
memory called segments. It is based on the way the programmer follows to structure their
programs. With segmented memory allocation, each job is divided into several segments
of different sizes, one for each module. Functions, subroutines, stack, array, etc., are
examples of such modules.
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.
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.
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.
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.
The CPU has to access the missed page from the secondary memory. If the number of
page fault is very high then the effective access time of the system will become very high.
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;
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.
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 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.
Translation of Logical address into physical address by segment
table
CPU generates a logical address which contains two parts:
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.
Translation of logical address to physical address
The CPU generates a logical address which is divided into two parts: Segment Number
and Segment Offset. The Segment Offset must be less than the segment limit. Offset is
further divided into Page number and Page Offset. To map the exact page number in the
page table, the page number is added into the page table base.
The actual frame number with the page offset is mapped to the main memory to get the
desired word in the page of the certain segment of the process.
Advantages of Segmented Paging
1. It reduces memory usage.
2. Page table size is limited by the segment size.
3. Segment table has only one entry corresponding to one actual segment.
4. External Fragmentation is not there.
5. It simplifies memory allocation.
Non-Contiguous memory
1 Non-contiguous memory allocation
allocation
Paging is closer to
5 Segmentation is closer to User
Operating System
There is no external
7 There is no external fragmentation
fragmentation
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.
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.
Only after understanding the concept of Paging we will understand about Page
Replacement Algorithms.
The main memory is divided into small blocks called pages. Now, each of the pages
contains the process which is retrieved into main memory and it is stored in one frame of
memory.
It is very important to have pages and frames which are of equal sizes which are very
useful for mapping and complete utilization of memory.
By giving the user the impression that there is memory available to load the process, this
approach allows them to load larger size programs than the primary memory that is
accessible.
The Operating System loads the many components of several processes in the main
memory as opposed to loading a single large process there.
By doing this, the level of multiprogramming will be enhanced, which will increase CPU
consumption.
Demand Paging
The Demand Paging is a condition which is occurred in the Virtual Memory. We know that
the pages of the process are stored in secondary memory. The page is brought to the
main memory when required. We do not know when this requirement is going to occur.
So, the pages are brought to the main memory when required by the Page Replacement
Algorithms.
So, the process of calling the pages to main memory to secondary memory upon demand
is known as Demand Paging.
The important jobs of virtual memory in Operating Systems are two. They are:
o Frame Allocation
o Page Replacement.
A Physical Address is required by the Central Processing Unit (CPU) for the frame creation
and the physical Addressing provides the actual address to the frame created. For each
page a frame must be created.
Here, in this Frame Allocation Algorithm we take number of frames and number of
processes at once. We divide the number of frames by number of processes. We get the
number of frames we must provide for each process.
This means if we have 36 frames and 6 processes. For each process 6 frames are
allocated.
It is not very logical to assign equal frames to all processes in systems with processes of
different sizes. A lot of allocated but unused frames will eventually be wasted if a lot of
frames are given to a little operation.
Here, in this Frame Allocation Algorithms we take number of frames based on the process
size. For big process more number of frames is allocated. For small processes less
number of frames is allocated by the operating system.
The problem in the Proportionate Frame Allocation Algorithm is number of frames are
wasted in some rare cases.
The advantage in Proportionate Frame Allocation Algorithm is that instead of equally, each
operation divides the available frames according to its demands.
According to the quantity of frame allocations and the processes, priority frame allocation
distributes frames. Let's say a process has a high priority and needs more frames; in such
case, additional frames will be given to the process. Processes with lower priorities are
then later executed in future and first only high priority processes are executed first.
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
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.
Suppose the Reference String is:
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:
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.
Belady'sAnomaly
In the case of LRU and optimal page replacement algorithms, it is seen that the number
of page faults will be reduced if we increase the number of frames. However, Balady found
that, In FIFO page replacement algorithm, the number of page faults will get increased
with the increment in number of frames.
This is the strange behavior shown by FIFO algorithm in some of the cases. This is an
Anomaly called as Belady'sAnomaly.
Request 0 1 5 3 0 1 4 0 1 5 3 4
Frame 3 5 5 5 1 1 1 1 1 3 3
Frame 2 1 1 1 0 0 0 0 0 5 5 5
Frame 1 0 0 0 3 3 3 4 4 4 4 4 4
Miss/Hit Miss Miss Miss Miss Miss Miss Miss Hit Hit Miss Miss Hit
Request 0 1 5 3 0 1 4 0 1 5 3 4
Frame 4 3 3 3 3 3 3 5 5 5
Frame 3 5 5 5 5 5 5 1 1 1 1
Frame 2 1 1 1 1 1 1 0 0 0 0 4
Frame 1 0 0 0 0 0 0 4 4 4 4 3 3
Miss/Hit Miss Miss Miss Miss Hit Hit Miss Miss Miss Miss Miss Miss
Therefore, in this example, the number of page faults is increasing by increasing the
number of frames hence this suffers from Belady'sAnomaly.
Cache Memory
The data or contents of the main memory that are used frequently by CPU are stored in
the cache memory so that the processor can easily access that data in a shorter time.
Whenever the CPU needs to access memory, it first checks the cache memory. If the data
is not found in cache memory, then the CPU moves into the main memory.
Cache memory is placed between the CPU and the main memory. The block diagram for
a cache memory can be represented as:
The cache is the fastest component in the memory hierarchy and approaches the speed
of CPU components.
Cache memory is organised as distinct set of blocks where each set contains a small fixed
number of blocks.
As shown in the above sets are represented by the rows. The example contains N sets
and each set contains four blocks. Whenever an access is made to cache, the cache
controller does not search the entire cache in order to look for a match. Rather, the
controller maps the address to a particular set of the cache and therefore searches only
the set for a match.
If a required block is not found in that set, the block is not present in the cache and cache
controller does not search it further. This kind of cache organisation is called set
associative because the cache is divided into distinct sets of blocks. As each set contains
four blocks the cache is said to be four way set associative.
o When the CPU needs to access memory, the cache is examined. If the word is
found in the cache, it is read from the fast memory.
o If the word addressed by the CPU is not found in the cache, the main memory
is accessed to read the word.
o A block of words one just accessed is then transferred from main memory to
cache memory. The block size may vary from one word (the one just accessed)
to about 16 words adjacent to the one just accessed.
o The performance of the cache memory is frequently measured in terms of a
quantity called hit ratio.
o When the CPU refers to memory and finds the word in cache, it is said to
produce a hit.
o If the word is not found in the cache, it is in main memory and it counts as
a miss.
o The ratio of the number of hits divided by the total CPU references to memory
(hits plus misses) is the hit ratio.
Levels of memory:
Level 1
It is a type of memory in which data is stored and accepted that are immediately stored in
CPU. Most commonly used register is accumulator, Program counter, address register
etc.
Level 2
It is the fastest memory which has faster access time where data is temporarily stored for
faster access.
Level 3
It is memory on which computer works currently. It is small in size and once power is off
data no longer stays in this memory.
Level 4
It is external memory which is not as fast as main memory but data stays permanently in
this memory.
What is Thrash?
In computer science, thrash is the poor performance of a virtual memory (or paging) system when
the same pages are being loaded repeatedly due to a lack of main memory to keep them in memory.
Depending on the configuration and algorithm, the actual throughput of a system can degrade by
multiple orders of magnitude.
In computer science, thrashing occurs when a computer's virtual memory resources are overused,
leading to a constant state of paging and page faults, inhibiting most application-level processing.
It causes the performance of the computer to degrade or collapse. The situation can continue
indefinitely until the user closes some running applications or the active processes free up
additional virtual memory resources.
To know more clearly about thrashing, first, we need to know about page fault and swapping.
o Page fault: We know every program is divided into some pages. A page fault occurs
when a program attempts to access data or code in its address space but is not currently
located in the system RAM.
o Swapping: Whenever a page fault happens, the operating system will try to fetch that
page from secondary memory and try to swap it with one of the pages in RAM. This
process is called swapping.
Thrashing is when the page fault and swapping happens very frequently at a higher rate, and then
the operating system has to spend more time swapping these pages. This state in the operating
system is known as thrashing. Because of thrashing, the CPU utilization is going to be reduced or
negligible.
The basic concept involved is that if a process is allocated too few frames, then there will be too
many and too frequent page faults. As a result, no valuable work would be done by the CPU, and
the CPU utilization would fall drastically.
The long-term scheduler would then try to improve the CPU utilization by loading some more
processes into the memory, thereby increasing the degree of multiprogramming. Unfortunately,
this would result in a further decrease in the CPU utilization, triggering a chained reaction of higher
page faults followed by an increase in the degree of multiprogramming, called thrashing.
Since global page replacement can bring any page, it tries to bring more pages whenever thrashing
is found. But what actually will happen is that no process gets enough frames, and as a result, the
thrashing will increase more and more. Therefore, the global page replacement algorithm is not
suitable when thrashing happens.
2. Local Page Replacement
Unlike the global page replacement algorithm, local page replacement will select pages which only
belong to that process. So there is a chance to reduce the thrashing. But it is proven that there are
many disadvantages if we use local page replacement. Therefore, local page replacement is just an
alternative to global page replacement in a thrashing scenario.
Causes of Thrashing
Programs or workloads may cause thrashing, and it results in severe performance problems, such
as:
o Adjust the swap file size:If the system swap file is not configured correctly, disk
thrashing can also happen to you.
o Increase the amount of RAM: As insufficient memory can cause disk thrashing, one
solution is to add more RAM to the laptop. With more memory, your computer can
handle tasks easily and don't have to work excessively. Generally, it is the best long-
term solution.
o Decrease the number of applications running on the computer: If there are too
many applications running in the background, your system resource will consume a lot.
And the remaining system resource is slow that can result in thrashing. So while
closing, some applications will release some resources so that you can avoid thrashing
to some extent.
o Replace programs: Replace those programs that are heavy memory occupied with
equivalents that use less memory.
A locality is a set of pages that are actively used together. The locality model states that as a process
executes, it moves from one locality to another. Thus, a program is generally composed of several
different localities which may overlap.
For example, when a function is called, it defines a new locality where memory references are
made to the function call instructions, local and global variables, etc. Similarly, when the function
is exited, the process leaves this locality.
2. Working-Set Model
The basic principle states that if we allocate enough frames to a process to accommodate its current
locality, it will only fault whenever it moves to some new locality. But if the allocated frames are
lesser than the size of the current locality, the process is bound to thrash.
According to this model, based on parameter A, the working set is defined as the set of pages in
the most recent 'A' page references. Hence, all the actively used pages would always end up being
a part of the working set.
The accuracy of the working set is dependent on the value of parameter A. If A is too large, then
working sets may overlap. On the other hand, for smaller values of A, the locality might not be
covered entirely.
If D is the total demand for frames and WSSi is the working set size for process i,
D = ⅀ WSSi
Now, if 'm' is the number of frames available in the memory, there are two possibilities:
o D>m, i.e., total demand exceeds the number of frames, then thrashing will occur as
some processes would not get enough frames.
o D<=m, then there would be no thrashing.
If there are enough extra frames, then some more processes can be loaded into the memory. On the
other hand, if the summation of working set sizes exceeds the frames' availability, some of the
processes have to be suspended (swapped out of memory).
This technique prevents thrashing along with ensuring the highest degree of multiprogramming
possible. Thus, it optimizes CPU utilization.
A more direct approach to handle thrashing is the one that uses the Page-Fault Frequency concept.
The problem associated with thrashing is the high page fault rate, and thus, the concept here is to
control the page fault rate.
If the page fault rate is too high, it indicates that the process has too few frames allocated to it. On
the contrary, a low page fault rate indicates that the process has too many frames.
Upper and lower limits can be established on the desired page fault rate, as shown in the diagram.
If the page fault rate falls below the lower limit, frames can be removed from the process. Similarly,
if the page faults rate exceeds the upper limit, more frames can be allocated to the process.
In other words, the graphical state of the system should be kept limited to the rectangular region
formed in the given diagram.
If the page fault rate is high with no free frames, some of the processes can be suspended
and allocated to them can be reallocated to other processes. The suspended processes
can restart later.