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

Memory Management in Os

Learning Purpose

Uploaded by

ideascollect123
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 views37 pages

Memory Management in Os

Learning Purpose

Uploaded by

ideascollect123
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/ 37

Memory

Computer memory can be defined as a collection of some data represented in the


binary format. On the basis of various functions, memory can be classified into
various categories. We will discuss each one of them later in detail.
A computer device that is capable to store any information or data temporally or
permanently, is called storage device.
How Data is being stored in a computer system?
In order to understand memory management, we have to make everything clear
about how data is being stored in a computer system.
Machine understands only binary language that is 0 or 1. Computer converts every
data into binary language first and then stores it into the memory.
That means if we have a program line written as int = 10 then the computer converts
it into the binary language and then store it into the memory blocks.
The representation of inti = 10 is shown below.

The binary representation of 10 is 1010. Here, we are considering 32 bit system


therefore, the size of int is 2 bytes i.e. 16 bit. 1 memory block stores 1 bit. If we are
using signed integer then the most significant bit in the memory array is always a
signed bit.
Signed bit value 0 represents positive integer while 1 represents negative integer.
Here, the range of values that can be stored using the memory array is -32768 to
+32767.
well, we can enlarge this range by using unsigned int. in that case, the bit which is
now storing the sign will also store the bit value and therefore the range will be 0 to
65,535.
Need for Multi programming
However, The CPU can directly access the main memory, Registers and cache of
the system. The program always executes in main memory. The size of main
memory affects degree of Multi programming to most of the extant. If the size of the
main memory is larger than CPU can load more processes in the main memory at
the same time and therefore will increase degree of Multi programming as well as
CPU utilization.
1. Let's consider,
2. Process Size = 4 MB
3. Main memory size = 4 MB
4. The process can only reside in the main memory at any time.
5. If the time for which the process does IO is P,
6.
7. Then,
8.
9. CPU utilization = (1-P)
10. let's say,
11. P = 70%
12. CPU utilization = 30 %
13. Now, increase the memory size, Let's say it is 8 MB.
14. Process Size = 4 MB
15. Two processes can reside in the main memory at the same time.
16. Let's say the time for which, one process does its IO is P,
17.
18. Then
19.
20. CPU utilization = (1-P^2)
21. let's say P = 70 %
22. CPU utilization = (1-0.49) =0.51 = 51 %
Fixed Partitioning
The earliest and one of the simplest technique which can be used to load more than
one processes into the main memory is Fixed partitioning or Contiguous memory
allocation.
In this technique, the main memory is divided into partitions of equal or different
sizes. The operating system always resides in the first partition while the other
partitions can be used to store user processes. The memory is assigned to the
processes in contiguous way.
In fixed partitioning,
1. The partitions cannot overlap.
2. A process must be contiguously present in a partition for the execution.
There are various cons of using this technique.
1. Internal Fragmentation
If the size of the process is lesser then the total size of the partition then some size
of the partition get wasted and remain unused. This is wastage of the memory and
called internal fragmentation.
As shown in the image below, the 4 MB partition is used to load only 3 MB process
and the remaining 1 MB got wasted.
2. External Fragmentation
The total unused space of various partitions cannot be used to load the processes
even though there is space available but not in the contiguous form.
As shown in the image below, the remaining 1 MB space of each partition cannot be
used as a unit to store a 4 MB process. Despite of the fact that the sufficient space is
available to load the process, process will not be loaded.
3. Limitation on the size of the process
If the process size is larger than the size of maximum sized partition then that
process cannot be loaded into the memory. Therefore, a limitation can be imposed
on the process size that is it cannot be larger than the size of the largest partition.
4. Degree of multiprogramming is less
By Degree of multi programming, we simply mean the maximum number of
processes that can be loaded into the memory at the same time. In fixed partitioning,
the degree of multiprogramming is fixed and very less due to the fact that the size of
the partition cannot be varied according to the size of processes.

Dynamic Partitioning
Dynamic partitioning tries to overcome the problems caused by fixed partitioning. In
this technique, the partition size is not declared initially. It is declared at the time of
process loading.
The first partition is reserved for the operating system. The remaining space is
divided into parts. The size of each partition will be equal to the size of the process.
The partition size varies according to the need of the process so that the internal
fragmentation can be avoided.

Advantages of Dynamic Partitioning over fixed


partitioning
1. No Internal Fragmentation
Given the fact that the partitions in dynamic partitioning are created according to the
need of the process, It is clear that there will not be any internal fragmentation
because there will not be any unused remaining space in the partition.
2. No Limitation on the size of the process
In Fixed partitioning, the process with the size greater than the size of the largest
partition could not be executed due to the lack of sufficient contiguous memory.
Here, In Dynamic partitioning, the process size can't be restricted since the partition
size is decided according to the process size.
3. Degree of multiprogramming is dynamic
Due to the absence of internal fragmentation, there will not be any unused space in
the partition hence more processes can be loaded in the memory at the same time.
Disadvantages of dynamic partitioning
External Fragmentation
Absence of internal fragmentation doesn't mean that there will not be external
fragmentation.
Let's consider three processes P1 (1 MB) and P2 (3 MB) and P3 (1 MB) are being
loaded in the respective partitions of the main memory.
After some time P1 and P3 got completed and their assigned space is freed. Now
there are two unused partitions (1 MB and 1 MB) available in the main memory but
they cannot be used to load a 2 MB process in the memory since they are not
contiguously located.
The rule says that the process must be contiguously present in the main memory to
get executed. We need to change this rule to avoid external fragmentation.

Complex Memory Allocation


In Fixed partitioning, the list of partitions is made once and will never change but in
dynamic partitioning, the allocation and deallocation is very complex since the
partition size will be varied every time when it is assigned to a new process. OS has
to keep track of all the partitions.
Due to the fact that the allocation and deallocation are done very frequently in
dynamic memory allocation and the partition size will be changed at each time, it is
going to be very difficult for OS to manage everything.
Partitioning Algorithms
There are various algorithms which are implemented by the Operating System in
order to find out the holes in the linked list and allocate them to the processes.
The explanation about each of the algorithm is given below.
1. First Fit Algorithm

First Fit algorithm scans the linked list and whenever it finds the first big enough hole
to store a process, it stops scanning and load the process into that hole. This
procedure produces two partitions. Out of them, one partition will be a hole while the
other partition will store the process.
First Fit algorithm maintains the linked list according to the increasing order of
starting index. This is the simplest to implement among all the algorithms and
produces bigger holes as compare to the other algorithms.
2. Next Fit Algorithm

Next Fit algorithm is similar to First Fit algorithm except the fact that, Next fit scans
the linked list from the node where it previously allocated a hole.
Next fit doesn't scan the whole list, it starts scanning the list from the next node. The
idea behind the next fit is the fact that the list has been scanned once therefore the
probability of finding the hole is larger in the remaining part of the list.
Experiments over the algorithm have shown that the next fit is not better then the first
fit. So it is not being used these days in most of the cases.
3. Best Fit Algorithm

The Best Fit algorithm tries to find out the smallest hole possible in the list that can
accommodate the size requirement of the process.
Using Best Fit has some disadvantages.

1. 1. It is slower because it scans the entire list every time and tries to find out the
smallest hole which can satisfy the requirement the process.

2. Due to the fact that the difference between the whole size and the process size is
very small, the holes produced will be as small as it cannot be used to load any
process and therefore it remains useless.
Despite of the fact that the name of the algorithm is best fit, It is not the best algorithm
among all.

4. Worst Fit Algorithm

The worst fit algorithm scans the entire list every time and tries to find out the biggest
hole in the list which can fulfill the requirement of the process.
Despite of the fact that this algorithm produces the larger holes to load the other
processes, this is not the better approach due to the fact that it is slower because it
searches the entire list every time again and again.
5. Quick Fit Algorithm

The quick fit algorithm suggestsmaintaining the different lists of frequently used
sizes. Although, it is not practically suggestible because the procedure takes so
much time to create the different lists and then expending the holes to load a
process.
The first fit algorithm is the best algorithm among all because

1. It takes lesser time compare to the other algorithms.

2. It produces bigger holes that can be used to load other processes later on.

3. It is easiest to implement.

Compaction
We got to know that the dynamic partitioning suffers from external fragmentation.
However, this can cause some serious problems.
To avoid compaction, we need to change the rule which says that the process can't
be stored in the different places in the memory.
We can also use compaction to minimize the probability of external fragmentation. In
compaction, all the free partitions are made contiguous and all the loaded partitions
are brought together.
By applying this technique, we can store the bigger processes in the memory. The
free partitions are merged which can now be allocated according to the needs of new
processes. This technique is also called defragmentation.
As shown in the image above, the process P5, which could not be loaded into the
memory due to the lack of contiguous space, can be loaded now in the memory
since the free partitions are made contiguous.
Problem with Compaction
The efficiency of the system is decreased in the case of compaction due to the fact
that all the free spaces will be transferred from several places to a single place.
Huge amount of time is invested for this procedure and the CPU will remain idle for
all this time. Despite of the fact that the compaction avoids external fragmentation, it
makes system inefficient.
Let us consider that OS needs 6 NS to copy 1 byte from one place to another.
1. 1 B transfer needs 6 NS
2. 256 MB transfer needs 256 X 2^20 X 6 X 10 ^ -9 secs
hence, it is proved to some extent that the larger size memory transfer needs some
huge amount of time that is in seconds.
Need for Paging
Disadvantage of Dynamic Partitioning
The main disadvantage of Dynamic Partitioning is External fragmentation. Although,
this can be removed by Compaction but as we have discussed earlier, the
compaction makes the system inefficient.
We need to find out a mechanism which can load the processes in the partitions in a
more optimal way. Let us discuss a dynamic and flexible mechanism called paging.
Need for Paging
Lets consider a process P1 of size 2 MB and the main memory which is divided into
three partitions. Out of the three partitions, two partitions are holes of size 1 MB
each.
P1 needs 2 MB space in the main memory to be loaded. We have two holes of 1 MB
each but they are not contiguous.
Although, there is 2 MB space available in the main memory in the form of those
holes but that remains useless until it become contiguous. This is a serious problem
to address.
We need to have some kind of mechanism which can store one process at different
locations of the memory.
The Idea behind paging is to divide the process in pages so that, we can store them
in the memory at different holes. We will discuss paging with the examples in the
next sections.

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.
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.
Basics of Binary Addresses
Computer system assigns the binary addresses to the memory locations. However,
The system uses amount of bits to address a memory location.
Using 1 bit, we can address two memory locations. Using 2 bits we can address 4
and using 3 bits we can address 8 memory locations.
A pattern can be identified in the mapping between the number of bits in the address
and the range of the memory locations.
We know,
1. Using 1 Bit we can represent 2^1 i.e 2 memory locations.
2. Using 2 bits, we can represent 2^2 i.e. 4 memory locations.
3. Using 3 bits, we can represent 2^3 i.e. 8 memory locations.
4. Therefore, if we generalize this,
5. Using n bits, we can assign 2^n memory locations.
6.
7. n bits of address 2 ^ n memory locations

these n bits can be divided into two parts, that are, K bits and (n-k) bits.
Physical and Logical Address Space
Physical Address Space
Physical address space in a system can be defined as the size of the main memory.
It is really important to compare the process size with the physical address space.
The process size must be less than the physical address space.

Physical Address Space = Size of the Main Memory

If, physical address space = 64 KB = 2 ^ 6 KB = 2 ^ 6 X 2 ^ 10 Bytes = 2 ^ 16 bytes

Let us consider,
word size = 8 Bytes = 2 ^ 3 Bytes

Hence,
Physical address space (in words) = (2 ^ 16) / (2 ^ 3) = 2 ^ 13 Words

Therefore,
Physical Address = 13 bits

In General,
If, Physical Address Space = N Words

then, Physical Address = log2 N


Logical Address Space
Logical address space can be defined as the size of the process. The size of the
process should be less enough so that it can reside in the main memory.
Let's say,
Logical Address Space = 128 MB = (2 ^ 7 X 2 ^ 20) Bytes = 2 ^ 27 Bytes
Word size = 4 Bytes = 2 ^ 2 Bytes

Logical Address Space (in words) = (2 ^ 27) / (2 ^ 2) = 2 ^ 25 Words


Logical Address = 25 Bits

In general,
If, logical address space = L words
Then, Logical Address = Log2L bits
What is a Word?
The Word is the smallest unit of the memory. It is the collection of bytes. Every
operating system defines different word sizes after analyzing the n-bit address that is
inputted to the decoder and the 2 ^ n memory locations that are produced from the
decoder.
Page Table in OS
Page Table is a data structure used by the virtual memory system to store the
mapping between logical addresses and physical addresses.
Logical addresses are generated by the CPU for the pages of the processes
therefore they are generally used by the processes.
Physical addresses are the actual frame address of the memory. They are generally
used by the hardware or more specifically by RAM subsystems.
The image given below considers,
Physical Address Space = M words
Logical Address Space = L words
Page Size = P words

Physical Address = log 2 M = m bits


Logical Address = log 2 L = l bits
page offset = log 2 P = p bits

The CPU always accesses the processes through their logical addresses. However,
the main memory recognizes physical address only.
In this situation, a unit named as Memory Management Unit comes into the picture. It
converts the page number of the logical address to the frame number of the physical
address. The offset remains same in both the addresses.
To perform this task, Memory Management unit needs a special kind of mapping
which is done by page table. The page table stores all the Frame numbers
corresponding to the page numbers of the page table.
In other words, the page table maps the page number to its actual location (frame
number) in the memory.
In the image given below shows, how the required word of the frame is accessed
with the help of offset.
Mapping from page table to main memory
In operating systems, there is always a requirement of mapping from logical address
to the physical address. However, this process involves various steps which are
defined as follows.
1. Generation of logical address
CPU generates logical address for each page of the process. This contains two
parts: page number and offset.
2. Scaling
To determine the actual page number of the process, CPU stores the page table
base in a special register. Each time the address is generated, the value of the page
table base is added to the page number to get the actual location of the page entry in
the table. This process is called scaling.
3. Generation of physical Address
The frame number of the desired page is determined by its entry in the page table. A
physical address is generated which also contains two parts : frame number and
offset. The Offset will be similar to the offset of the logical address therefore it will be
copied from the logical address.
4. Getting Actual Frame Number
The frame number and the offset from the physical address is mapped to the main
memory in order to get the actual word address.
Page Table Entry
Along with page frame number, the page table also contains some of the bits
representing the extra information regarding the page.
Let's see what the each bit represents about the page.
1. Caching Disabled
Sometimes, there are differences between the information closest to the CPU and
the information closest to the user. Operating system always wants CPU to access
user's data as soon as possible. CPU accesses cache which can be inaccurate in
some of the cases, therefore, OS can disable the cache for the required pages. This
bit is set to 1 if the cache is disabled.
2. Referenced
There are variouspage replacement algorithms which will be covered later in this
tutorial. This bit is set to 1 if the page is referred in the last clock cycle otherwise it
remains 0.
3. Modified
This bit will be set if the page has been modified otherwise it remains 0.
4. Protection
The protection field represents the protection level which is applied on the page. It
can be read only or read & write or execute. We need to remember that it is not a bit
rather it is a field which contains many bits.
5. Present/Absent
In the concept of demand paging, all the pages doesn't need to be present in the
main memory Therefore, for all the pages that are present in the main memory, this
bit will be set to 1 and the bit will be 0 for all the pages which are absent.
If some page is not present in the main memory then it is called page fault.

Finding Optimal Page Size


We have seen that the bigger page table size cause an extra overhead because we
have to divide that table into the pages and then store that into the main memory.
Our concern must be about executing processes not on the execution of page table.
Page table provides a support for the execution of the process. The larger the page
Table, the higher the overhead.
We know that,
1. Page Table Size = number of page entries in page table X size of one page entry
2. Let's consider an example,
3. Virtual Address Space = 2 GB = 2 X 2 ^ 30 Bytes
4. Page Size = 2 KB = 2 X 2 ^ 10 Bytes
5. Number of Pages in Page Table = (2 X 2 ^ 30)/(2 X 2 ^ 10) = 1 M pages
There will be 1 million pages which is quite big number. However, try to make page
size larger, say 2 MB.
Then, Number of pages in page table = (2 X 2 ^ 30)/(2 X 2 ^ 20) = 1 K pages.
If we compare the two scenarios, we can find out that the page table size is anti
proportional to Page Size.
In Paging, there is always wastage on the last page. If the virtual address space is
not a multiple of page size, then there will be some bytes remaining and we have to
assign a full page to those many bytes. This is simply a overhead.
Let's consider,
1. Page Size = 2 KB
2. Virtual Address Space = 17 KB
3. Then number of pages = 17 KB / 2 KB
The number of pages will be 9 although the 9th page will only contain 1 byte and the
remaining page will be wasted.
In general,
1. If page size = p bytes
2. Entry size = e bytes
3. Virtual Address Space = S bytes
4. Then, overhead O = (S/p) X e + (p/2)
On an average, the wasted number of pages in a virtual space is p/2(the half of total
number of pages).
For, the minimal overhead,
1. O/p = 0
2. -S/(p^2) + ½ = 0
3. p = (2.S.e) bytes
Hence, if the page size (2.S.e) bytes then the overhead will be minimal.
What is Virtual Memory in OS (Operating
System)?
Virtual Memory is a storage scheme that provides user an illusion of having a very
big main memory. This is done by treating a part of secondary memory as the main
memory.
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.
How Virtual Memory Works?
In modern word, virtual memory has become quite common these days. In this
scheme, whenever some pages needs to be loaded in the main memory for the
execution and the memory is not available for those many pages, then in that case,
instead of stopping the pages from entering in the main memory, the OS search for
the RAM area that are least used in the recent times or that are not referenced and
copy that into the secondary memory to make the space for the new pages in the
main memory.
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.
Snapshot of a virtual memory management system
Let us assume 2 processes, P1 and P2, contains 4 pages each. Each page size is 1
KB. The main memory contains 8 frame of 1 KB each. The OS resides in the first two
partitions. In the third partition, 1st page of P1 is stored and the other frames are also
shown as filled with the different pages of processes in the main memory.
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.
Disadvantages of Virtual Memory
1. The system becomes slower since swapping takes time.
2. It takes more time in switching between applications.
3. The user will have the lesser hard disk space for its use.

Translation Look aside buffer


Drawbacks of Paging
1. Size of Page table can be very big and therefore it wastes main memory.
2. CPU will take more time to read a single word from the main memory.
How to decrease the page table size
1. The page table size can be decreased by increasing the page size but it will cause
internal fragmentation and there will also be page wastage.
2. Other way is to use multilevel paging but that increases the effective access time
therefore this is not a practical approach.
How to decrease the effective access time
1. CPU can use a register having the page table stored inside it so that the access time
to access page table can become quite less but the register are not cheaper and they
are very small in compare to the page table size therefore, this is also not a practical
approach.
2. To overcome these many drawbacks in paging, we have to look for a memory that is
cheaper than the register and faster than the main memory so that the time taken by
the CPU to access page table again and again can be reduced and it can only focus
to access the actual word.
Locality of reference
In operating systems, the concept of locality of reference states that, instead of
loading the entire process in the main memory, OS can load only those number of
pages in the main memory that are frequently accessed by the CPU and along with
that, the OS can also load only those page table entries which are corresponding to
those many pages.
Translation look aside buffer (TLB)
A Translation look aside buffer can be defined as a memory cache which can be
used to reduce the time taken to access the page table again and again.
It is a memory cache which is closer to the CPU and the time taken by CPU to
access TLB is lesser then that taken to access main memory.
In other words, we can say that TLB is faster and smaller than the main memory but
cheaper and bigger than the register.
TLB follows the concept of locality of reference which means that it contains only the
entries of those many pages that are frequently accessed by the CPU.
In translation look aside buffers, there are tags and keys with the help of which, the
mapping is done.
TLB hit is a condition where the desired entry is found in translation look aside
buffer. If this happens then the CPU simply access the actual location in the main
memory.
However, if the entry is not found in TLB (TLB miss) then CPU has to access page
table in the main memory and then access the actual frame in the main memory.
Therefore, in the case of TLB hit, the effective access time will be lesser as compare
to the case of TLB miss.
If the probability of TLB hit is P% (TLB hit rate) then the probability of TLB miss (TLB
miss rate) will be (1-P) %.
Therefore, the effective access time can be defined as;
1. EAT = P (t + m) + (1 - p) (t + k.m + m)
Where, p TLB hit rate, t time taken to access TLB, m time taken to access main
memory k = 1, if the single level paging has been implemented.
By the formula, we come to know that
1. Effective access time will be decreased if the TLB hit rate is increased.
2. Effective access time will be increased in the case of multilevel paging.

What is Demand Paging in OS (Operating


System)?
According to the concept of Virtual Memory, in order to execute some process, only
a part of the process needs to be present in the main memory which means that only
a few pages will only be present in the main memory at any time.
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.
Therefore, to overcome this problem, there is a concept called Demand Paging is
introduced. It suggests keeping all pages of the frames in the secondary memory
until they are required. In other words, it says that do not load any page in the main
memory until it is required.
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.
What is a Page Fault?
If the referred page is not present in the main memory then there will be a miss and
the concept is called Page miss or page fault.
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;
1. EAT = PF X S + (1 - PF) X (ma)
Inverted Page Table
Inverted Page Table is the global page table which is maintained by the Operating
System for all the processes. In inverted page table, the number of entries is equal to
the number of frames in the main memory. It can be used to overcome the
drawbacks of page table.
There is always a space reserved for the page regardless of the fact that whether it
is present in the main memory or not. However, this is simply the wastage of the
memory if the page is not present.

We can save this wastage by just inverting the page table. We can save the details
only for the pages which are present in the main memory. Frames are the indices
and the information saved inside the block will be Process ID and page number.
Page Replacement Algorithms in
Operating Systems (OS)
Today we are going to learn about Page Replacement Algorithms in Operating
Systems (OS). Before knowing about Page Replacement Algorithms in Operating
Systems let us learn about Paging in Operating Systems and also a little about
Virtual Memory.
Only after understanding the concept of Paging we will understand about Page
Replacement Algorithms.
Paging in Operating Systems (OS)
Paging is a storage mechanism. Paging is used to retrieve processes from
secondary memory to primary memory.
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.
Virtual Memory in Operating Systems (OS)
A storage method known as virtual memory gives the user the impression that their
main memory is quite large. By considering a portion of secondary memory as the
main memory, this is accomplished.
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.
Frame Allocation in Virtual Memory
Demand paging is used to implement virtual memory, an essential component of
operating systems. A page-replacement mechanism and a frame allocation algorithm
must be created for demand paging. If you have numerous processes, frame
allocation techniques are utilized to determine how many frames to provide to each
process.
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.
Frame Allocation Constraints
o The Frames that can be allocated cannot be greater than total number of frames.
o Each process should be given a set minimum amount of frames.
o When fewer frames are allocated then the page fault ration increases and the
process execution becomes less efficient
o There ought to be sufficient frames to accommodate all the many pages that a single
instruction may refer to
Frame Allocation Algorithms
There are three types of Frame Allocation Algorithms in Operating Systems. They
are:
1) Equal Frame Allocation Algorithms
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.
2) Proportionate Frame Allocation Algorithms
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.
3) Priority Frame Allocation Algorithms
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.
Page Replacement Algorithms
There are three types of Page Replacement Algorithms. They are:
o Optimal Page Replacement Algorithm
o First In First Out Page Replacement Algorithm
o Least Recently Used (LRU) Page Replacement Algorithm
First in First out Page Replacement Algorithm
This is the first basic algorithm of Page Replacement Algorithms. This algorithm is
basically dependent on the number of frames used. Then each frame takes up the
certain page and tries to access it. When the frames are filled then the actual
problem starts. The fixed number of frames is filled up with the help of first frames
present. This concept is fulfilled with the help of Demand Paging
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:
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.
Points to Remember
Page Not Found - - - > Page Fault
Page Found - - - > Page Hit
Reference String:
Number of Page Hits = 8
Number of Page Faults = 12
The Ratio of Page Hit to the Page Fault = 8 : 12 - - - > 2 : 3 - - - > 0.66
The Page Hit Percentage = 8 *100 / 20 = 40%
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.
OPTIMAL Page Replacement Algorithm
This is the second basic algorithm of Page Replacement Algorithms. This algorithm
is basically dependent on the number of frames used. Then each frame takes up the
certain page and tries to access it. When the frames are filled then the actual
problem starts. The fixed number of frames is filled up with the help of first frames
present. This concept is fulfilled with the help of Demand Paging
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:
Suppose the Reference String is:
0, 3, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3, 2, 1, 2, 0
6, 1, 2 are in the frames occupying the frames.
Now we need to enter 0 into the frame by removing one page from the page
So, let us check which page number occurs last
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:
Consider the reference string 6, 1, 1, 2, 0, 3, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3, 2, 1, 4, 0 for a
memory with three frames and calculate number of page faults by using OPTIMAL
Page replacement algorithms.
Points to Remember
Page Not Found - - - > Page Fault
Page Found - - - > Page Hit
Reference String:

Number of Page Hits = 8


Number of Page Faults = 12
The Ratio of Page Hit to the Page Fault = 8 : 12 - - - > 2 : 3 - - - > 0.66
The Page Hit Percentage = 8 *100 / 20 = 40%
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
6, 1, 5 are in the frames occupying the frames.
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.
Least Recently Used (LRU) Replacement Algorithm
This is the last basic algorithm of Page Replacement Algorithms. This algorithm is
basically dependent on the number of frames used. Then each frame takes up the
certain page and tries to access it. When the frames are filled then the actual
problem starts. The fixed number of frames is filled up with the help of first frames
present. This concept is fulfilled with the help of Demand Paging
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:
Suppose the Reference String is:
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 allot a space for the page numbered 0.
Now, we need to travel back into the past to check which page can be replaced.
6 is the oldest page which is available in the Frame.
So, replace 6 with the page numbered 0.
Let us understand this Least Recently Used (LRU) Page Replacement Algorithm
working with the help of an example.
Example:
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 Least
Recently Used (LRU) Page replacement algorithms.
Points to Remember
Page Not Found - - - > Page Fault
Page Found - - - > Page Hit
Reference String:

Number of Page Hits = 7


Number of Page Faults = 13
The Ratio of Page Hit to the Page Fault = 7 : 12 - - - > 0.5833 : 1
The Page Hit Percentage = 7 * 100 / 20 = 35%
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.

What is Main Memory?


The main memory is central to the operation of a Modern Computer. Main
Memory is a large array of words or bytes, ranging in size from hundreds of
thousands to billions. Main memory is a repository of rapidly available
information shared by the CPU and I/O devices. Main memory is the place
where programs and information are kept when the processor is effectively
utilizing them. Main memory is associated with the processor, so moving
instructions and information into and out of the processor is extremely fast.
Main memory is also known as RAM (Random Access Memory). This
memory is volatile. RAM loses its data when a power interruption occurs.

Main Memory

What is Memory Management?


In a multiprogramming computer, the Operating System resides in a part of
memory, and the rest is used by multiple processes. The task of subdividing
the memory among different processes is called Memory Management.
Memory management is a method in the operating system to manage
operations between main memory and disk during process execution. The
main aim of memory management is to achieve efficient utilization of memory.

Why Memory Management is Required?


Allocate and de-allocate memory before and after process execution.
To keep track of used memory space by processes.
To minimize fragmentation issues.
To proper utilization of main memory.
To maintain data integrity while executing of process.
Now we are discussing the concept of Logical Address Space and Physical
Address Space
Logical and Physical Address Space
Logical Address Space: An address generated by the CPU is known as a
“Logical Address”. It is also known as a Virtual address. Logical address
space can be defined as the size of the process. A logical address can be
changed.
Physical Address Space: An address seen by the memory unit (i.e the one
loaded into the memory address register of the memory) is commonly
known as a “Physical Address”. A Physical address is also known as a
Real address. The set of all physical addresses corresponding to these
logical addresses is known as Physical address space. A physical
address is computed by MMU. The run-time mapping from virtual to
physical addresses is done by a hardware device Memory Management
Unit(MMU). The physical address always remains constant.
Static and Dynamic Loading
Loading a process into the main memory is done by a loader. There are two
different types of loading :
Static Loading: Static Loading is basically loading the entire program into a
fixed address. It requires more memory space.
Dynamic Loading: The entire program and all data of a process must be in
physical memory for the process to execute. So, the size of a process is
limited to the size of physical memory. To gain proper memory utilization,
dynamic loading is used. In dynamic loading, a routine is not loaded until it
is called. All routines are residing on disk in a relocatable load format. One
of the advantages of dynamic loading is that the unused routine is never
loaded. This loading is useful when a large amount of code is needed to
handle it efficiently.
Static and Dynamic Linking
To perform a linking task a linker is used. A linker is a program that takes one
or more object files generated by a compiler and combines them into a single
executable file.
Static Linking: In static linking, the linker combines all necessary program
modules into a single executable program. So there is no runtime
dependency. Some operating systems support only static linking, in which
system language libraries are treated like any other object module.
Dynamic Linking: The basic concept of dynamic linking is similar to
dynamic loading. In dynamic linking, “Stub” is included for each appropriate
library routine reference. A stub is a small piece of code. When the stub is
executed, it checks whether the needed routine is already in memory or
not. If not available then the program loads the routine into memory.
Swapping
When a process is executed it must have resided in memory. Swapping is a
process of swapping a process temporarily into a secondary memory from the
main memory, which is fast compared to secondary memory. A swapping
allows more processes to be run and can be fit into memory at one time. The
main part of swapping is transferred time and the total time is directly
proportional to the amount of memory swapped. Swapping is also known as
roll-out, or roll because if a higher priority process arrives and wants service,
the memory manager can swap out the lower priority process and then load
and execute the higher priority process. After finishing higher priority work, the
lower priority process swapped back in memory and continued to the
execution process.
swapping in memory management
Memory Management with Monoprogramming (Without Swapping)
This is the simplest memory management approach the memory is divided
into two sections:
One part of the operating system
The second part of the user program

Fence Register

operating user
system program
In this approach, the operating system keeps track of the first and last
location available for the allocation of the user program
The operating system is loaded either at the bottom or at top
Interrupt vectors are often loaded in low memory therefore, it makes sense
to load the operating system in low memory
Sharing of data and code does not make much sense in a single process
environment
The Operating system can be protected from user programs with the help of
a fence register.
Advantages of Memory Management
It is a simple management approach
Disadvantages of Memory Management
It does not support multiprogramming
Memory is wasted
Multiprogramming with Fixed Partitions (Without Swapping)
A memory partition scheme with a fixed number of partitions was introduced
to support multiprogramming. this scheme is based on contiguous
allocation
Each partition is a block of contiguous memory
Memory is partitioned into a fixed number of partitions.
Each partition is of fixed size
Example: As shown in fig. memory is partitioned into 5 regions the region is
reserved for updating the system the remaining four partitions are for the user
program.
Fixed Size Partitioning

Operating
System

p1

p2

p3

p4
Partition Table
Once partitions are defined operating system keeps track of the status of
memory partitions it is done through a data structure called a partition table.
Sample Partition Table

Starting Address of Size of


Partition Partition Status

allocate
0k 200k
d

200k 100k free

300k 150k free

allocate
450k 250k
d

Logical vs Physical Address


An address generated by the CPU is commonly referred to as a logical
address. the address seen by the memory unit is known as the physical
address. The logical address can be mapped to a physical address by
hardware with the help of a base register this is known as dynamic relocation
of memory references.
Contiguous Memory Allocation
The main memory should accommodate both the operating system and the
different client processes. Therefore, the allocation of memory becomes an
important task in the operating system. The memory is usually divided into
two partitions: one for the resident operating system and one for the user
processes. We normally need several user processes to reside in memory
simultaneously. Therefore, we need to consider how to allocate available
memory to the processes that are in the input queue waiting to be brought into
memory. In adjacent memory allotment, each process is contained in a single
contiguous segment of memory.

Contiguous Memory Allocation

Memory Allocation
To gain proper memory utilization, memory allocation must be allocated
efficient manner. One of the simplest methods for allocating memory is to
divide memory into several fixed-sized partitions and each partition contains
exactly one process. Thus, the degree of multiprogramming is obtained by the
number of partitions.
Multiple partition allocation: In this method, a process is selected from the
input queue and loaded into the free partition. When the process
terminates, the partition becomes available for other processes.
Fixed partition allocation: In this method, the operating system maintains
a table that indicates which parts of memory are available and which are
occupied by processes. Initially, all memory is available for user processes
and is considered one large block of available memory. This available
memory is known as a “Hole”. When the process arrives and needs
memory, we search for a hole that is large enough to store this process. If
the requirement is fulfilled then we allocate memory to process, otherwise
keeping the rest available to satisfy future requests. While allocating a
memory sometimes dynamic storage allocation problems occur, which
concerns how to satisfy a request of size n from a list of free holes. There
are some solutions to this problem:
First Fit
In the First Fit, the first available free hole fulfil the requirement of the process
allocated.

First Fit
Here, in this diagram, a 40 KB memory block is the first available free hole
that can store process A (size of 25 KB), because the first two blocks did not
have sufficient memory space.
Best Fit
In the Best Fit, allocate the smallest hole that is big enough to process
requirements. For this, we search the entire list, unless the list is ordered by
size.

Best Fit
Here in this example, first, we traverse the complete list and find the last hole
25KB is the best suitable hole for Process A(size 25KB). In this method,
memory utilization is maximum as compared to other memory allocation
techniques.
Worst Fit
In the Worst Fit, allocate the largest available hole to process. This method
produces the largest leftover hole.

Worst Fit
Here in this example, Process A (Size 25 KB) is allocated to the largest
available memory block which is 60KB. Inefficient memory utilization is a
major issue in the worst fit.
Fragmentation
Fragmentation is defined as when the process is loaded and removed after
execution from memory, it creates a small free hole. These holes can not be
assigned to new processes because holes are not combined or do not fulfill
the memory requirement of the process. To achieve a degree of
multiprogramming, we must reduce the waste of memory or fragmentation
problems. In the operating systems two types of fragmentation:
1. Internal fragmentation: Internal fragmentation occurs when memory
blocks are allocated to the process more than their requested size. Due to
this some unused space is left over and creating an internal fragmentation
problem.Example: Suppose there is a fixed partitioning used for memory
allocation and the different sizes of blocks 3MB, 6MB, and 7MB space in
memory. Now a new process p4 of size 2MB comes and demands a block
of memory. It gets a memory block of 3MB but 1MB block of memory is a
waste, and it can not be allocated to other processes too. This is called
internal fragmentation.
1. External fragmentation: In External Fragmentation, we have a free
memory block, but we can not assign it to a process because blocks are
not contiguous. Example: Suppose (consider the above example) three
processes p1, p2, and p3 come with sizes 2MB, 4MB, and 7MB
respectively. Now they get memory blocks of size 3MB, 6MB, and 7MB
allocated respectively. After allocating the process p1 process and the p2
process left 1MB and 2MB. Suppose a new process p4 comes and
demands a 3MB block of memory, which is available, but we can not
assign it because free memory space is not contiguous. This is called
external fragmentation.
Both the first-fit and best-fit systems for memory allocation are affected by
external fragmentation. To overcome the external fragmentation problem
Compaction is used. In the compaction technique, all free memory space
combines and makes one large block. So, this space can be used by other
processes effectively.
Another possible solution to the external fragmentation is to allow the logical
address space of the processes to be noncontiguous, thus permitting a
process to be allocated physical memory wherever the latter is available.
Paging
Paging is a memory management scheme that eliminates the need for a
contiguous allocation of physical memory. This scheme permits the physical
address space of a process to be non-contiguous.
Logical Address or Virtual Address (represented in bits): An address
generated by the CPU.
Logical Address Space or Virtual Address Space (represented in
words or bytes): The set of all logical addresses generated by a program.
Physical Address (represented in bits): An address actually available on
a memory unit.
Physical Address Space (represented in words or bytes): The set of all
physical addresses corresponding to the logical addresses.
Example:
If Logical Address = 31 bits, then Logical Address Space = 231 words = 2 G
words (1 G = 230)
If Logical Address Space = 128 M words = 27 * 220 words, then Logical
Address = log2 227 = 27 bits
If Physical Address = 22 bits, then Physical Address Space = 222 words = 4
M words (1 M = 220)
If Physical Address Space = 16 M words = 24 * 220 words, then Physical
Address = log2 224 = 24 bits
The mapping from virtual to physical address is done by the memory
management unit (MMU) which is a hardware device and this mapping is
known as the paging technique.
The Physical Address Space is conceptually divided into several fixed-size
blocks, called frames.
The Logical Address Space is also split into fixed-size blocks, called pages.
Page Size = Frame Size
Let us consider an example:
Physical Address = 12 bits, then Physical Address Space = 4 K words
Logical Address = 13 bits, then Logical Address Space = 8 K words
Page size = frame size = 1 K words (assumption)
Paging
The address generated by the 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 a particular word in a
page or page size of Logical Address Space or word number of a page or
page offset.
Physical Address is divided into:
Frame Number(f): Number of bits required to represent the frame of
Physical Address Space or Frame number frame
Frame Offset(d): Number of bits required to represent a particular word in a
frame or frame size of Physical Address Space or word number of a frame
or frame offset.
The hardware implementation of the page table can be done by using
dedicated registers. But the usage of the register for the page table is
satisfactory only if the page table is small. If the page table contains a large
number of entries then we can use TLB(translation Look-aside buffer), a
special, small, fast look-up hardware cache.
The TLB is an associative, high-speed memory.
Each entry in TLB consists of two parts: a tag and a value.
When this memory is used, then an item is compared with all tags
simultaneously. If the item is found, then the corresponding value is
returned.

Page Map Table


Main memory access time = m
If page table are kept in main memory,
Effective access time = m(for page table)
+ m(for particular page in page table)

TLB Hit and Miss

You might also like