Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
14 views28 pages

Unit-4 - OS - Memory Management

The document discusses memory management in operating systems, focusing on Bare Machines and Resident Monitors as early computing concepts. It explains the advantages and disadvantages of these systems, along with memory management techniques such as contiguous memory allocation, fixed-size and variable-size partition schemes, and strategies for allocating memory. Additionally, it covers fragmentation types and their implications on memory utilization.

Uploaded by

y4593866
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)
14 views28 pages

Unit-4 - OS - Memory Management

The document discusses memory management in operating systems, focusing on Bare Machines and Resident Monitors as early computing concepts. It explains the advantages and disadvantages of these systems, along with memory management techniques such as contiguous memory allocation, fixed-size and variable-size partition schemes, and strategies for allocating memory. Additionally, it covers fragmentation types and their implications on memory utilization.

Uploaded by

y4593866
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/ 28

Operating Systems (BCS-401)

I
B.Tech CSE/IT 2nd Year/4th Semester
Unit-4 Memory Management
Er Parag Rastogi
Assistant Professor, Department of Computer Science RD Engg College

Basic bare Machine


In the early days of computing, before the development of modern operating systems, the execution of programs
was done directly on the hardware. This was made possible by two fundamental components: Bare Machine and
Resident Monitor. These components are although outdated in today's computing environment but once they
were important for memory management and task execution.
Bare Machine
A Bare Machine is a computer system that operates directly with its hardware. It executes programs in machine
language without the need for an operating system. Before the development of operating system, the only
drawback was that it accepted instructions in only machine language due to which only people with sufficient
knowledge about Computer field are able to operate a computer. Hence, after the development of the operating
system, Bare machine is considered as inefficient.
Working of Bare Machine
 The processor executes machine language instructions provided by the user.

 There is no abstraction between the user and the machine. Hence, the system is highly dependent on the
knowledge of machine code.
 Each instruction is processed by the hardware without the help of an operating system which results in
a basic, raw computing environment.
Advantages of Bare Machine
1. Direct Control: Users can directly control the hardware without any interference from an operating
system which can be beneficial in low-level tasks or specialized applications.
2. Efficiency in Simple Tasks: For basic, single-task operations, Bare Machines can be faster than using an
operating system as there is no overhead.
3. Low Resource Requirement: Bare Machines do not require resources or memory needed by an operating
system which makes them useful in environments with limited resources.
Disadvantages of Bare Machine
1. Lack of User-Friendliness: Bare Machines only accept machine language. Users must have a
understanding of low-level programming.
2. No Multitasking: Unlike systems with operating systems, Bare Machines cannot handle multiple tasks
simultaneously which makes them inefficient for most general-purpose use cases.
3. Limited Scope: Bare Machines are inefficient for complex applications as they lack the abstraction and
multitasking features provided by modern operating systems.
Resident Monitor

The Resident Monitor is program that runs on Bare Machines. The resident monitor works like a primitive
Er Parag Rastogi
CSE Department
operating system that controls the instructions and performs all necessary functions such as job scheduling,
memory management, and interrupt processing. I

Parts of Resident Monitor


It is divided into 4 parts as:

 Control Language Interpreter: The first part of the Resident monitor is control language
interpreter which is used to read and carry out the instruction from one level to the next level.

 Loader: The second part of the Resident monitor which is the main part of the Resident Monitor
is Loader which Loads all the necessary system and application programs into the main memory.

 Device Driver: The third part of the Resident monitor is Device Driver which is used to manage
the connecting input-output devices to the system. So basically it is the interface between the user
and the system. it works as an interface between the request and response. request which user made,
Device driver responds that the system produces to fulfil these requests.

 Interrupt Processing: The fourth part as the name suggests, it processes the all occurred interrupt
to the system.

Working of Resident Monitor

 It works like job sequencer as it sequences job and sends them to the processor.

 After scheduling the job Resident monitors loads the programs one by one into the main memory
according to their sequences.

 It controls the execution of instructions and ensures that programs are executed without any
interruption in between.
Er Parag Rastogi
CSE Department
 The Interrupt Processing feature allows the system to handle signals orI requests from the hardware
during execution, ensuring smooth operations.

Advantages of Resident Monitor

1. Job Sequencing: The Resident Monitor handles job sequencing and memory management.

2. Faster Execution: There is no time gap between job scheduling and execution, making the system
faster compared to manual job handling.

3. No OS Overhead: The Resident Monitor allows program execution without the overhead of a
operating system, making it lighter in terms of resource consumption.

4. Basic System Control: It provides a minimal, yet effective level of control over hardware
resources.

Disadvantages of Resident Monitor

1. Limited Functionality: The Resident Monitor provides only basic control over the system, and
lacks the extensive features of a modern operating system.

2. No Multitasking: Like the Bare Machine, Resident Monitors cannot handle multiple programs
running simultaneously, which limits their use in more complex environments.

3. Not Scalable: The Resident Monitor is not scalable for larger applications or systems that require
more advanced management.

4. Error Handling: As with Bare Machines, error handling is more difficult because there is no
higher-level operating system to catch and handle errors gracefully.

Memory Management: Basic Concept

The term Memory can be defined as a collection of data in a specific format. It is used to store instructions and process
data. The memory comprises a large array or group of words or bytes, each with its own location. The primary motive
of a computer system is to execute programs. These programs, along with the information they access, should be in the
main memory during execution. The CPU fetches instructions from memory according to the value of the program
counter.
To achieve a degree of multiprogramming and proper utilization of memory, memory management is important. Many
memory management methods exist, reflecting various approaches, and the effectiveness of each algorithm depends on
the situation.
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.
Er Parag Rastogi
CSE Department
 To proper utilization of main memory.
 To maintain data integrity while executing of process. I

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.

Logical versus physical address


An address generated by CPU is commonly referred to a logical address. The address seen by memory unit is known as
physical address
Logical address can be mapped to physical address by hardware with the help of base register this is known as dynamic
relocation of memory reference.

Contiguous memory allocation

Contiguous Memory Allocation is a type of memory allocation technique where processes are allotted a continuous block
of space in memory. Contiguous memory allocation refers to a memory management technique in which whenever there
occurs a request by a user process for the memory, one of the sections of the contiguous memory block would be given to
that process, in accordance with its requirement.

As we can see in the illustration shown above, three files are there in the directory. The starting block, along with the
length of each file, is mentioned in the table. Thus, we can see in this table that all the contiguous blocks get assigned to
every file as per their need.

As the name implies, we allocate contiguous blocks of memory to each process using this technique. So, whenever a
process wants to enter the main memory, we allocate a continuous segment from the totally empty space to the process
based on its size.

Contiguous Memory Allocation Techniques

Whenever a process has to be allocated space in the memory, following the contiguous memory allocation technique, we
Er Parag Rastogi
CSE Department
have to allot the process a continuous empty block of space to reside. This allocation can be done in two ways:
I
(a) Fixed-size Partition Scheme
(b) Variable-size Partition Scheme

Fixed-size Partition Scheme

In this type of contiguous memory allocation technique, each process is allotted a fixed size continuous block in the
main memory. That means there will be continuous blocks of fixed size into which the complete memory will be divided,
and each time a process comes in, it will be allotted one of the free blocks. Because irrespective of the size of the process,
each is allotted a block of the same size memory space. This technique is also called static partitioning.

In the diagram above, we have 3 processes in the input queue that have to be allotted space in the memory. As we are
following the fixed size partition technique, the memory has fixed-sized blocks. The first process, which is of size 3MB is
also allotted a 5MB block, and the second process, which is of size 1MB, is also allotted a 5MB block, and
the 4MB process is also allotted a 5MB block. So, the process size doesn't matter. Each is allotted the same fixed-size
memory block.

It is clear that in this scheme, the number of continuous blocks into which the memory will be divided will be decided by
the amount of space each block covers, and this, in turn, will dictate how many processes can stay in the main memory at
once.

Advantages

The advantages of a fixed-size partition scheme are:

1. Because all of the blocks are the same size, this scheme is simple to implement. All we have to do now is divide
the memory into fixed blocks and assign processes to them.
2. It is easy to keep track of how many blocks of memory are left, which in turn decides how many more processes
can be given space in the memory.
3. As at a time multiple processes can be kept in the memory, this scheme can be implemented in a system that needs
multiprogramming.

Disadvantages

Though the fixed-size partition scheme has many advantages, it also has some disadvantages:

1. As the size of the blocks is fixed, we will not be able to allot space to a process that has a greater size than the
block.
2. The size of the blocks decides the degree of multiprogramming, and only that many processes can remain in the
memory at once as the number of blocks.
Er Parag Rastogi
CSE Department
3. If the size of the block is greater than the size of the process, we have no other choice but to assign the process to
this block, but this will lead to much empty space left behind in the block. This empty
I
space could've been used
to accommodate a different process. This is called internal fragmentation. Hence, this technique may lead to space
wastage.

Variable-size Partition Scheme


In this type of contiguous memory allocation technique, no fixed blocks or partitions are made in the memory. Instead,
each process is allotted a variable-sized block depending upon its requirements. That means, whenever a new process
wants some space in the memory, if available, this amount of space is allotted to it. Hence, the size of each block depends
on the size and requirements of the process which occupies it.

In the diagram above, there are no fixed-size partitions. Instead, the first process needs 3MB memory space and hence is
allotted that much only. Similarly, the other 3 processes are allotted only that much space that is required by them.
As the blocks are variable-sized, which is decided as processes arrive, this scheme is also called Dynamic
Partitioning.

Advantages

The advantages of a variable-size partition scheme are:

1. As the processes have blocks of space allotted to them as per their requirements, there is no internal fragmentation.
Hence, there is no memory wastage in this scheme.
2. The number of processes that can be in the memory at once will depend upon how many processes are in the
memory and how much space they occupy. Hence, it will be different for different cases and will be dynamic.
Er Parag Rastogi
CSE Department
3. As there are no blocks that are of fixed size, even a process of big size can be allotted space.
I

Disadvantages

Though the variable-size partition scheme has many advantages, it also has some disadvantages:

1. Because this approach is dynamic, a variable-size partition scheme is difficult to implement.


2. It is difficult to keep track of processes and the remaining space in the memory.

Strategies Used for Contiguous Memory Allocation Input Queues

So far, we've seen the two types of schemes for contiguous memory allocation. But what happens when a new process
comes in and has to be allotted a space in the main memory? How is it decided which block or segment it will get?

Processes that have been assigned continuous blocks of memory will fill the main memory at any given
time. However, when a process completes, it leaves behind an empty block known as a hole. This space could also be
used for a new process. Hence, the main memory consists of processes and holes, and any one of these holes can be allotted
to a new incoming process. We have three strategies to allot a hole to an incoming process:

First-Fit
This is a very basic strategy in which we start from the beginning and allot the first hole, which is big enough as per the
requirements of the process. The first-fit strategy can also be implemented in a way where we can start our search for the
first-fit hole from the place we left off last time.

Here, in this diagram 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

This is a greedy strategy that aims to reduce any memory wasted because of internal fragmentation in the case of static
partitioning, and hence we allot that hole to the process, which is the smallest hole that fits the requirements of the process.
Hence, we need to first sort the holes according to their sizes and pick the best fit for the process without wasting memory.

Er Parag Rastogi
CSE Department
I

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

This strategy is the opposite of the Best-Fit strategy. We sort the holes according to their sizes and choose the largest hole
to be allotted to the incoming process. The idea behind this allocation is that as the process is allotted a large hole, it will
have a lot of space left behind as internal fragmentation. Hence, this will create a hole that will be large enough to
accommodate a few other processes.

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 cannot 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:

(i) 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 leftover and creates an internal fragmentation problem.
Example: Suppose there is a fixed partitioning is used for memory allocation and the different size of block 3MB,
6MB, and 7MB space in memory. Now a new process p4 of size 2MB comes and demand for the block of memory. It

Er Parag Rastogi
CSE Department
gets a memory block of 3MB but 1MB block memory is a waste, and it cannot be allocated to other processes too. This
is called internal fragmentation. I

(ii) External fragmentation:


In external fragmentation, we have a free memory block, but we cannot assign it to process because blocks are not
contiguous.
Example: Suppose (consider above example) three process p1, p2, p3 comes with size 2MB, 4MB, and 7MB
respectively. Now they get memory blocks of size 3MB, 6MB, and 7MB allocated respectively. After allocating process
p1 process and p2 process left 1MB and 2MB. Suppose a new process p4 comes and demands a 3MB block of memory,
which is available, but we cannot 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 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 permit a process to be allocated physical memory wherever the latter is available.

Compaction

Compaction is a technique to collect all the free memory present in form of fragments into one large chunk of free
memory, which can be used to run other processes.
It does that by moving all the processes towards one end of the memory and all the available free space towards the
other end of the memory so that it becomes contiguous.
It is not always easy to do compaction. Compaction can be done only when the relocation is dynamic and done at
execution time. Compaction cannot be done when relocation is static and is performed at load time or assembly time.
Before Compaction

Before compaction, the main memory has some free space between occupied space. This condition is known as external
fragmentation. Due to less free space between occupied spaces, large processes cannot be loaded into them.

After Compaction

After compaction, all the occupied space has been moved up and the free space at the bottom. This makes the space
contiguous and removes external fragmentation. Processes with large memory requirements can be now loaded into the
Er Parag Rastogi
CSE Department
main memory.
I

Issues with Compaction


Although the compaction technique is very useful in making memory utilization efficient and reduces external
fragmentation of memory, the problem with it is that a large amount of time is wasted in the process and during that time
the CPU sits idle hence reducing the efficiency of the system.
Advantages of Compaction

 Reduces external fragmentation.


 Make memory usage efficient.
 Memory becomes contiguous.
 Since memory becomes contiguous more processes can be loaded to memory.

Disadvantages of Compaction

 System efficiency reduces.


 A huge amount of time is wasted in performing compaction.
 CPU sits idle for a long time.
 Not always easy to perform compaction.

Paging
Paging is a storage mechanism that allows OS to retrieve processes from the secondary storage into the main
memory in the form of pages. In the Paging method, the main memory is divided into small fixed-size blocks
of physical memory, which is called frames. The size of a frame should be kept the same as that of a page to
have maximum utilization of the main memory and to avoid external fragmentation. Paging is used for faster
access to data, and it is a logical concept .

Example of Paging in OS
For example, if the main memory size is 16 KB and Frame size is 1 KB. Here, the main memory will be divided into the
collection of 16 frames of 1 KB each.

There are 4 separate processes in the system that is A1, A2, A3, and A4 of 4 KB each. Here, all the processes are divided
into pages of 1 KB each so that operating system can store one page in one frame.

At the beginning of the process, all the frames remain empty so that all the pages of the processes will get stored in a
contiguous way.

Er Parag Rastogi
CSE Department
.
I

Main Memory (Collection of frames)


Paging

In this example we can see that A2 and A4 are moved to the waiting state after some time. Therefore, eight frames become
empty, and so other pages can be loaded in that empty blocks. The process A5 of size 8 pages (8 KB) is waiting in the
ready queue.

Main Memory (Collection of frames)


Paging
In this example, we can see that there are eight non-contiguous frames which are available in the memory, and paging
offers the flexibility of storing the process at the different places. This allows us to load the pages of process A5 instead
of A2 and A4.

Er Parag Rastogi
CSE Department
Paging Protection I
The paging process should be protected by using the concept of insertion of an additional bit called Valid/Invalid bit.
Paging Memory protection in paging is achieved by associating protection bits with each page. These bits are associated
with each page table entry and specify protection on the corresponding page.

Advantages of Paging
Here, are advantages of using Paging method:

 Easy to use memory management algorithm


 No need for external Fragmentation
 Swapping is easy between equal-sized pages and page frames.

Disadvantages of Paging
Here, are drawback/ cons of Paging:

 May cause Internal fragmentation


 Page tables consume additional memory.
 Multi-level paging may lead to memory reference overhead.

Segmentation
A process is divided into Segments. The chunks that a program is divided into which are not necessarily all of
the exact sizes are called segments. Segmentation gives the user's view of the process which paging does not
provide. Here the user's view is mapped to physical memory.
Types of Segmentation in Operating Systems
 Virtual Memory Segmentation: Each process is divided into a number of segments, but the
segmentation is not done all at once. This segmentation may or may not take place at the run time of the
program.
 Simple Segmentation: Each process is divided into a number of segments, all of which are loaded into
memory at run time, though not necessarily contiguously.
There is no simple relationship between logical addresses and physical addresses in segmentation. A table stores
the information about all such segments and is called Segment Table.
What is Segment Table?
It maps a two-dimensional Logical address into a one-dimensional Physical address. It's each table entry has:
 Base Address: It contains the starting physical address where the segments reside in memory.

 Segment Limit: Also known as segment offset. It specifies the length of the segment.

Er Parag Rastogi
CSE Department
I

The address generated by the CPU is divided into:

 Segment number (s): Number of bits required to represent the segment.


 Segment offset (d): Number of bits required to represent the position of data within a segment.

Advantages of Segmentation in Operating System

 Reduced Internal Fragmentation : Segmentation can reduce internal fragmentation compared to


fixed-size paging, as segments can be sized according to the actual needs of a process. However,
internal fragmentation can still occur if a segment is allocated more space than it is actually used.

 Segment Table consumes less space in comparison to Page table in paging.

 As a complete module is loaded all at once, segmentation improves CPU utilization.

 The user's perception of physical memory is quite similar to segmentation. Users can divide user
programs into modules via segmentation. These modules are nothing more than separate processes'
codes.

 The user specifies the segment size, whereas, in paging, the hardware determines the page size.

 Segmentation is a method that can be used to segregate data from security operations.

 Flexibility: Segmentation provides a higher degree of flexibility than paging. Segments can be of
variable size, and processes can be designed to have multiple segments, allowing for more fine-
grained memory allocation.
Er Parag Rastogi
CSE Department
 Sharing: Segmentation allows for sharing of memory segments between I processes. This can be
useful for inter-process communication or for sharing code libraries.

 Protection: Segmentation provides a level of protection between segments, preventing one


process from accessing or modifying another process's memory segment. This can help increase
the security and stability of the system.

Disadvantages of Segmentation in Operating System

 External Fragmentation : As processes are loaded and removed from memory, the free memory
space is broken into little pieces, causing external fragmentation. This is a notable difference from
paging, where external fragmentation is significantly lesser.

 Overhead is associated with keeping a segment table for each activity.

 Due to the need for two memory accesses, one for the segment table and the other for main memory,
access time to retrieve the instruction increases.

 Fragmentation: As mentioned, segmentation can lead to external fragmentation as memory


becomes divided into smaller segments. This can lead to wasted memory and decreased
performance.

 Overhead: Using a segment table can increase overhead and reduce performance. Each segment
table entry requires additional memory, and accessing the table to retrieve memory locations can
increase the time needed for memory operations.

 Complexity: Segmentation can be more complex to implement and manage than paging. In
particular, managing multiple segments per process can be challenging, and the potential for
segmentation faults can increase as a result.

Virtual Memory
Virtual Memory is a storage mechanism which offers user an illusion of having a very big main memory. It is done by
treating a part of secondary memory as the main memory. In Virtual memory, the user can store processes with a bigger
size than the available main memory.

Therefore, instead of loading one long process in the main memory, the OS loads the various parts of more than one
process in the main memory. Virtual memory is mostly implemented with demand paging and demand segmentation.

Why Need Virtual Memory?


Here, are reasons for using virtual memory:

 Whenever your computer doesn’t have space in the physical memory it writes what it needs to remember to the
hard disk in a swap file as virtual memory.
 If a computer running Windows needs more memory/RAM, then installed in the system, it uses a small portion of
the hard drive for this purpose.

How Virtual Memory Works?

Er Parag Rastogi
CSE Department
In the modern world, virtual memory has become quite common these days. It is used whenever some pages require to be
loaded in the main memory for the execution, and the memory is not available for those many
I
pages.

So, in that case, instead of preventing pages from entering in the main memory, the OS searches for the RAM space that
are minimum used in the recent times or that are not referenced into the secondary memory to make the space for the new
pages in the main memory.

Let’s understand virtual memory management with the help of one example.

For example:
Let’s assume that an OS requires 300 MB of memory to store all the running programs. However, there’s currently only
50 MB of available physical memory stored on the RAM.

 The OS will then set up 250 MB of virtual memory and use a program called the Virtual Memory Manager (VMM)
to manage that 250 MB.
 So, in this case, the VMM will create a file on the hard disk that is 250 MB in size to store extra memory that is
required.
 The OS will now proceed to address memory as it considers 300 MB of real memory stored in the RAM, even if
only 50 MB space is available.
 It is the job of the VMM to manage 300 MB memory even if just 50 MB of real memory space is available.

Demand Paging
A demand paging system is quite similar to a paging system with swapping where processes reside in secondary memory
and pages are loaded only on demand, not in advance. When a context switch occurs, the operating system does not copy
any of the old program’s pages out to the disk or any of the new program’s pages into the main memory Instead, it just
begins executing the new program after loading the first page and fetches that program’s pages as they are referenced.

Er Parag Rastogi
CSE Department
While executing a program, if the program references a page which is not available in the main memory because it was
swapped out a little ago, the processor treats this invalid memory reference as a page faultI and transfers control from the
program to the operating system to demand the page back into the memory.
Advantages
Following are the advantages of Demand Paging −

 Large virtual memory.


 More efficient use of memory.
 There is no limit on degree of multiprogramming.

Disadvantages
 Number of tables and the amount of processor overhead for handling page interrupts are greater than in the case of
the simple paged management techniques.

Page Fault

It mainly occurs when any program tries to access the data or the code that is in the address space of the program, but that
data is not currently located in the RAM of the system.

 So basically when the page referenced by the CPU is not found in the main memory then the situation is termed
as Page Fault.
 Whenever any page fault occurs, then the required page has to be fetched from the secondary memory into the
main memory.

The page fault mainly generates an exception, which is used to notify the operating system that it must have to retrieve
the "pages" from the virtual memory in order to continue the execution. Once all the data is moved into the physical
memory the program continues its execution normally. The Page fault process takes place in the background and thus
goes unnoticed by the user.

Handling the Page Fault

Given below is the simple procedure to handle the page fault:

Er Parag Rastogi
CSE Department
I

Figure: Steps to Handle the Page fault

Let us understand the procedure to handle the page fault as shown with the help of the above diagram:

1. First of all, internal table (that is usually the process control block) for this process in order to determine whether
the reference was valid or invalid memory access.
2. If the reference is invalid, then we will terminate the process. If the reference is valid, but we have not bought in
that page so now we just page it in.
3. Then we locate the free frame list in order to find the free frame.
4. Now a disk operation is scheduled in order to read the desired page into the newly allocated frame.
5. When the disk is completely read, then the internal table is modified that is kept with the process, and the page
table that mainly indicates the page is now in memory.
6. Now we will restart the instruction that was interrupted due to the trap. Now the process can access the page as
though it had always been in memory.

Page Replacement Algorithms

Page replacement is needed in the operating systems that use virtual memory using Demand Paging. As we know that in
Demand paging, only a set of pages of a process is loaded into the memory. This is done so that we can have more
processes in the memory at the same time.

When a page that is residing in virtual memory is requested by a process for its execution, the Operating System needs to
decide which page will be replaced by this requested page. This process is known as page replacement and is a vital
component in virtual memory management.

Why Need Page Replacement Algorithms

To understand why we need page replacement algorithms, we first need to know about page faults. Let’s see what a page
fault is.
Er Parag Rastogi
CSE Department
Page Fault: A Page Fault occurs when a program running in CPU tries to access a page that is in the address space of that
program, but the requested page is currently not loaded into the main physical memory, the
I
RAM of the system.

Since the actual RAM is much less than the virtual memory the page faults occur. So whenever a page fault occurs, the
Operating system has to replace an existing page in RAM with the newly requested page. In this scenario, page
replacement algorithms help the Operating System in deciding which page to replace. The primary objective of all the
page replacement algorithms is to minimize the number of page faults.

Page Replacement Algorithms

First In First Out (FIFO)

FIFO algorithm is the simplest of all the page replacement algorithms. In this, we maintain a queue of all the pages that
are in the memory currently. The oldest page in the memory is at the front-end of the queue and the most recent page is at
the back or rear-end of the queue.

Whenever a page fault occurs, the operating system looks at the front-end of the queue to know the page to be replaced
by the newly requested page. It also adds this newly requested page at the rear-end and removes the oldest page from the
front-end of the queue.

Example: Consider the page reference string as 3, 1, 2, 1, 6, 5, 1, 3 with 3-page frames. Let’s try to find the number of
page faults:

 Initially, all of the slots are empty so page faults occur at 3,1,2.

Page faults = 3

 When page 1 comes, it is in the memory so no page fault occurs.

Page faults = 3

 When page 6 comes, it is not present and a page fault occurs. Since there are no empty slots, we remove the front
of the queue, i.e 3.
Er Parag Rastogi
CSE Department
Page faults = 4
I
 When page 5 comes, it is also not present and hence a page fault occurs. The front of the queue i.e 1 is removed.

Page faults = 5

 When page 1 comes, it is not found in memory and again a page fault occurs. The front of the queue i.e 2 is
removed.

Page faults = 6

 When page 3 comes, it is again not found in memory, a page fault occurs, and page 6 is removed being on top of
the queue

Total page faults = 7

Belady's anomaly: Generally if we increase the number of frames in the memory, the number of page faults should
decrease due to obvious reasons. Belady’s anomaly refers to the phenomena where increasing the number of frames in
memory, increases the page faults as well.

Advantages

 Simple to understand and implement


 Does not cause more overhead

Disadvantages

 Poor performance
 Doesn’t use the frequency of the last used time and just simply replaces the oldest page.
 Suffers from Belady’s anomaly.

Optimal Page Replacement

Optimal page replacement is the best page replacement algorithm as this algorithm results in the least number of page
faults. In this algorithm, the pages are replaced with the ones that will not be used for the longest duration of time in the
future. In simple terms, the pages that will be referred farthest in the future are replaced in this algorithm.

Example:

Let’s take the same page reference string 3, 1, 2, 1, 6, 5, 1, 3 with 3-page frames as we saw in FIFO. This also helps us
understand how Optimal Page replacement works the best.

 Initially, since all the slots are empty, pages 3, 1, 2 cause a page fault and take the empty slots.

Er Parag Rastogi
CSE Department
Page faults = 3
I
 When page 1 comes, it is in the memory and no page fault occurs.

Page faults = 3

 When page 6 comes, it is not in the memory, so a page fault occurs and 2 is removed as it is not going to be used
again.

Page faults = 4

 When page 5 comes, it is also not in the memory and causes a page fault. Similar to above 6 is removed as it is
not going to be used again.

page faults = 5

 When page 1 and page 3 come, they are in the memory so no page fault occurs.

Total page faults = 5

Advantages

 Excellent efficiency
 Less complexity
 Easy to use and understand
 Simple data structures can be used to implement
 Used as the benchmark for other algorithms

Disadvantages

 More time consuming


 Difficult for error handling
 Need future awareness of the programs, which is not possible every time

Least Recently Used (LRU) Page Replacement Algorithm

The least recently used page replacement algorithm keeps the track of usage of pages over a period of time. This algorithm
works on the basis of the principle of locality of a reference which states that a program has a tendency to access the
same set of memory locations repetitively over a short period of time. So pages that have been used heavily in the past are
most likely to be used heavily in the future also.

In this algorithm, when a page fault occurs, then the page that has not been used for the longest duration of time is
replaced by the newly requested page.

Example: Let’s see the performance of the LRU on the same reference string of 3, 1, 2, 1, 6, 5, 1, 3 with 3-page frames:

Er Parag Rastogi
CSE Department
I

 Initially, since all the slots are empty, pages 3, 1, 2 cause a page fault and take the empty slots.

Page faults = 3

 When page 1 comes, it is in the memory and no page fault occurs.

Page faults = 3

 When page 6 comes, it is not in the memory, so a page fault occurs and the least recently used page 3 is removed.

Page faults = 4

 When page 5 comes, it again causes a page fault and page 1 is removed as it is now the least recently used page.

Page faults = 5

 When page 1 comes again, it is not in the memory and hence page 2 is removed according to the LRU.

Page faults = 6

 When page 3 comes, the page fault occurs again and this time page 6 is removed as the least recently used one.

Total page faults = 7

Now in the above example, the LRU causes the same page faults as the FIFO, but this may not always be the case as it
will depend upon the series, the number of frames available in memory, etc. In fact, on most occasions, LRU is better
than FIFO.

Advantages

 It is open for full analysis


 Doesn’t suffer from Belady’s anomaly
 Often more efficient than other algorithms

Disadvantages

 It requires additional data structures to be implemented


 More complex
 High hardware assistance is required

Er Parag Rastogi
CSE Department
Thrashing
I
Thrashing is a condition or a situation when the system is spending a major portion of its time servicing the
page faults, but the actual processing done is very negligible.

Causes of thrashing:
1. High degree of multiprogramming.
2. Lack of frames.
3. Page replacement policy.

Thrashing's Causes

Thrashing has an impact on the operating system's execution performance. Thrashing also causes serious
performance issues with the operating system. When the CPU's usage is low, the process scheduling
mechanism tries to load multiple processes into memory at the same time, increasing the degree of Multi
programming.

In this case, the number of processes in the memory exceeds the number of frames available in the memory.
Each process is given a set number of frames to work with.
Thrashing occurs when a system spends more time swapping pages in and out of memory than executing
processes, leading to a significant drop in performance. Learning techniques to handle thrashing is crucial for
optimizing system performance.

If a high-priority process arrives in memory and the frame is not vacant at the moment, the other process
occupying the frame will be moved to secondary storage, and the free frame will be allotted to a higher-
priority process.

We may also argue that as soon as the memory is full, the procedure begins to take a long time to swap in the
required pages. Because most of the processes are waiting for pages, the CPU utilization drops again.

As a result, a high level of multi programming and a lack of frames are two of the most common reasons for
thrashing in the operating system.

The basic concept involved is that if a process is allocated to few frames, then there will be too many and too
frequent page faults. As a result, no useful work would be done by the CPU and the CPU utilization would
Er Parag Rastogi
CSE Department
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 multi programming. ThisI 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 multi programming, called Thrashing.

Cache Memory Organization


Cache memory is a small, high-speed storage area in a computer. The cache is a smaller and faster memory
that stores copies of the data from frequently used main memory locations. There are various independent
caches in a CPU, which store instructions and data.

 The most important use of cache memory is that it is used to reduce the average time to access data
from the main memory.
 The concept of cache works because there exists locality of reference (the same items or nearby
items are more likely to be accessed next) in processes.

By storing this information closer to the CPU, cache memory helps speed up the overall processing time.
Cache memory is much faster than the main memory (RAM). When the CPU needs data, it first checks the
cache. If the data is there, the CPU can access it quickly. If not, it must fetch the data from the slower main
memory.

Characteristics of Cache Memory

 Extremely fast memory type that acts as a buffer between RAM and the CPU.

 Holds frequently requested data and instructions, ensuring that they are immediately available to
the CPU when needed.

 Costlier than main memory or disk memory but more economical than CPU registers.

 Used to speed up processing and synchronize with the high-speed CPU.

Levels of Memory

 Level 1 or Register: It is a type of memory in which data is stored and accepted that are
immediately stored in the CPU. The most commonly used register is Accumulator, Program
counter , Address Register, etc.

 Level 2 or Cache memory: It is the fastest memory that has faster access time where data is
temporarily stored for faster access.

 Level 3 or Main Memory: It is the memory on which the computer works currently. It is small in
size and once power is off data no longer stays in this memory.
Er Parag Rastogi
CSE Department
 Level 4 or Secondary Memory: It is external memory that is not as fast
I as the main memory but
data stays permanently in this memory.

Cache Performance

When the processor needs to read or write a location in the main memory, it first checks for a corresponding
entry in the cache.

 If the processor finds that the memory location is in the cache, a Cache Hit has occurred and data
is read from the cache.

 If the processor does not find the memory location in the cache, a cache miss has occurred. For a
cache miss, the cache allocates a new entry and copies in data from the main memory, then the
request is fulfilled from the contents of the cache.
The performance of cache memory is frequently measured in terms of a quantity called Hit ratio.
Hit Ratio(H) = hit / (hit + miss) = no. of hits/total accesses
Miss Ratio = miss / (hit + miss) = no. of miss/total accesses = 1 - hit ratio(H)

Cache Mapping

Cache mapping refers to the method used to store data from main memory into the cache. It determines how
data from memory is mapped to specific locations in the cache.
There are three different types of mapping used for the purpose of cache memory which is as follows:
 Direct Mapping
 Fully Associative Mapping
 Set-Associative Mapping

1. Direct Mapping

Direct mapping is a simple and commonly used cache mapping technique where each block of main memory
is mapped to exactly one location in the cache called cache line. If two memory blocks map to the same cache
line, one will overwrite the other, leading to potential cache misses. Direct mapping's performance is directly
proportional to the Hit ratio.

The Main Memory consists of memory blocks and these blocks are made up of fixed number of words. A
typical address in main memory is split into two parts:

1. Index Field: It represent the block number. Index Field bits tells us the location of block where a
word can be.

2. Block Offset: It represent words in a memory block. These bits determines the location of word in
a memory block.

The Cache Memory consists of cache lines. These cache lines has same size as memory blocks. The address
in cache memory consists of:

1. Block Offset: This is the same block offset we use in Main Memory.
Er Parag Rastogi
CSE Department
2. Index: It represent cache line number. This part of the memory address
I determines which cache
line (or slot) the data will be placed in.

3. Tag: The Tag is the remaining part of the address that uniquely identifies which block is currently
occupying the cache line.

Fig: Memory Structure in Direct Mapping


The index field in main memory maps directly to the index in cache memory, which determines the cache line
where the block will be stored. The block offset in both main memory and cache memory indicates the exact
word within the block. In the cache, the tag identifies which memory block is currently stored in the cache
line. This mapping ensures that each memory block is mapped to exactly one cache line, and the data is
accessed using the tag and index while the block offset specifies the exact word in the block.

2. Fully Associative Mapping

Fully associative mapping is a type of cache mapping where any block of main memory can be stored in any
cache line. Unlike direct-mapped cache, where each memory block is restricted to a specific cache line based
on its index, fully associative mapping gives the cache the flexibility to place a memory block in any available
cache line. This improves the hit ratio but requires a more complex system for searching and managing cache
lines.

The address structure of Cache Memory is different in fully associative mapping from direct mapping. In fully
associative mapping, the cache does not have an index field. It only have a tag which is same as Index Field
in memory address. Any block of memory can be placed in any cache line. This flexibility means that there’s
no fixed position for memory blocks in the cache.

Er Parag Rastogi
CSE Department
Fig: Cache Memory Structure in Fully Associative Mapping
To determine whether a block is present in the cache, the tag is compared withIthe tags stored in all cache
lines. If a match is found, it is a cache hit, and the data is retrieved from that cache line. If no match is found,
it's a cache miss, and the required data is fetched from main memory.

Fig: Fully Associative Mapping

3. Set-Associative Mapping
Set-associative mapping is a compromise between direct-mapped and fully-associative mapping in cache
systems. It combines the flexibility of fully associative mapping with the efficiency of direct mapping. In this
scheme, multiple cache lines (typically 2, 4, or more) are grouped into sets.
The Cache address structure is as follows:

Cache Memory in Set Associative Mapping


This reduces the conflict misses that occur in direct mapping while still limiting the search space compared
to fully-associative mapping.

For example, consider a 2-way set associative cache, which means 2 cache lines make a set in this cache
structure. There are 8 memory blocks and 4 cache lines, thus the number of sets will be 4/2 = 2 sets. Using
direct mapping strategy first, block 0 will be in set 0, block 1 in set 1, block 2 in set 0 and so on. Then, the tag
is used to search through all cache lines in that set to find the correct block (Associative Mapping).

Er Parag Rastogi
CSE Department
I

Fig: Two Way Set Associative Cache


Advantages

 Cache Memory is faster in comparison to main memory and secondary memory.

 Programs stored by Cache Memory can be executed in less time.

 The data access time of Cache Memory is less than that of the main memory.

 Cache Memory stored data and instructions that are regularly used by the CPU, therefore it
increases the performance of the CPU.

Disadvantages

 Cache Memory is costlier than primary memory and secondary memory .


 Data is stored on a temporary basis in Cache Memory.

 Whenever the system is turned off, data and instructions stored in cache memory get destroyed.

 The high cost of cache memory increases the price of the Computer System.

Locality of Reference
Locality of reference refers to a phenomenon in which a computer program tends to access same set of
memory locations for a particular time period. In other words, Locality of Reference refers to the tendency
of the computer program to access instructions whose addresses are near one another. The property of locality
of reference is mainly shown by loops and subroutine calls in a program.
Er Parag Rastogi
CSE Department
1. In case of loops in program control processing unit repeatedly refers to the set of instructions that
constitute the loop. I

2. In case of subroutine calls, every time the set of instructions are fetched from memory.

3. References to data items also get localized that means same data item is referenced again and again.

In the above figure, we can see that the CPU wants to read or fetch the data or instruction. First, it will access
the cache memory as it is near to it and provides very fast access. If the required data or instruction is found,
it will be fetched. This situation is known as a cache hit.
But if the required data or instruction is not found in the cache memory then this situation is known as a cache
miss. Now the main memory will be searched for the required data or instruction that was being searched and
if found will go through one of the two ways:

1. First way is that the CPU should fetch the required data or instruction and use it and that's it but
what, when the same data or instruction is required again. CPU again has to access the same main
memory location for it and we already know that main memory is the slowest to access

2. The second way is to store the data or instruction in the cache memory so that if it is needed soon
again in the near future it could be fetched in a much faster way.

Er Parag Rastogi
CSE Department

You might also like