0% found this document useful (0 votes)
6 views49 pages

Unit IV

The document discusses memory management in operating systems, outlining key requirements such as relocation, protection, and sharing. It covers various memory allocation techniques including fixed and dynamic partitioning, as well as algorithms like First Fit, Best Fit, and Worst Fit. Additionally, it explains concepts of paging and segmentation, highlighting their roles in optimizing memory usage and managing virtual memory effectively.

Uploaded by

Ajit kumar
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)
6 views49 pages

Unit IV

The document discusses memory management in operating systems, outlining key requirements such as relocation, protection, and sharing. It covers various memory allocation techniques including fixed and dynamic partitioning, as well as algorithms like First Fit, Best Fit, and Worst Fit. Additionally, it explains concepts of paging and segmentation, highlighting their roles in optimizing memory usage and managing virtual memory effectively.

Uploaded by

Ajit kumar
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/ 49

Unit IV

Memory Management

Operating System

Dr. Jyoti Chavhan


Assistant Professor

Jyoti Chavhan Dr. Jyoti Chavhan


Memory Management Requirements

1. Relocation

2. Protection

3. Sharing

4. Logical Organization

5. Physical Organization

Jyoti Chavhan Dr. Jyoti Chavhan


Need for Memory Management in OS

Memory management technique is needed for the following reasons:

● This technique helps in placing the programs in memory in such a way so that memory is utilized at its fullest extent.
● This technique helps to protect different processes from each other so that they do not interfere with each other's operations.
● It helps to allocate space to different application routines.
● This technique allows you to check how much memory needs to be allocated to processes that decide which processor should get
memory at what time.
● It keeps the track of each memory location whether it is free or allocated.
● This technique keeps the track of inventory whenever memory gets freed or unallocated and it will update the status accordingly.

Jyoti Chavhan Dr. Jyoti Chavhan


Memory Partitioning

1.Fixed Partitioning

2.Variable Partitioning

Jyoti Chavhan Dr. Jyoti Chavhan


Dynamic Partitioning

Jyoti Chavhan Dr. Jyoti Chavhan


Pros of Dynamic Partitioning in Operating System

1. No Internal Fragmentation

2. No Limitation on the Process Size

3. Dynamic Degree of Multiprogramming

Cons of Dynamic Partitioning in Operating System

1. External Fragmentation

2. Complex Memory Allocation

Jyoti Chavhan Dr. Jyoti Chavhan


Fixed Partitioning

Jyoti Chavhan Dr. Jyoti Chavhan


There are various cons of using this technique.

1. Internal Fragmentation

2. External Fragmentation

3. Limitation on the size of the process

4. Degree of multiprogramming is less

Jyoti Chavhan Dr. Jyoti Chavhan


Memory Allocations in OS

The four most commonly used allocation schemes are

● First Fit

● Best Fit

● Worst Fit

Jyoti Chavhan Dr. Jyoti Chavhan


The operating system uses different memory management schemes to optimize memory/resource block allocation to
different processes. We will look at one of such memory allocation processes in OS called First Fit in Operating
System. In this approach we allot the first available space that is large enough to accommodate the process.

How first fit works?


Whenever a process (p1) comes with memory allocation request the following happens –

● OS sequentially searches available memory blocks from the first index

● Assigns the first memory block large enough to accommodate process

Whenever a new process P2 comes, it does the same thing. Search from the first index again.

Jyoti Chavhan Dr. Jyoti Chavhan


First Fit
Example :

Input : blockSize[] = {100, 500, 200, 300, 600};

processSize[] = {212, 417, 112, 426};

Output:

Process No. Process Size Block no.

1 212 2

2 417 5

3 112 2

4 426 Not Allocated

Jyoti Chavhan Dr. Jyoti Chavhan


Advantages
● Easy to implement

● OS can allocate processes quickly as algorithm to allocate processes will be quick as compared to other

methods (Best Fit, Worst Fit, Next Fit etc)

Disadvantages
● Causes huge internal fragmentation

● Smarter allocation may be done by best-fit algorithm

● High chances of unallocated for some processes due to poor algorithm

● More overhead as compared to next fit

Jyoti Chavhan Dr. Jyoti Chavhan


Best Fit Algorithm in Operating System

In the case of the best fit memory allocation scheme, the operating system searches for the empty memory block.
When the operating system finds the memory block with minimum wastage of memory, it is allocated to the process
this is known as Best Fit Algorithm in Operating System.

How does it work?


In Best fit memory allocation scheme, the operating system searches that can –

● Accommodate the process


● Also, leaves the minimum memory wastage

Example –

If you see the image below/right you will see that the process size is 40.

While blocks 1, 2 and 4 can accommodate the process. Block 2 is chosen as it leaves the lowest memory wastage

Jyoti Chavhan Dr. Jyoti Chavhan


Jyoti Chavhan Dr. Jyoti Chavhan
Advantage:
It is allocated to the process.

This scheme is considered the best approach as it results in the most optimized memory allocation.

Also reduces internal fragmentation.

Disadvantage:
However, finding the best fit memory allocation may be time-consuming.

Jyoti Chavhan Dr. Jyoti Chavhan


How Worst Fit Works?
Worst fit works in the following way, for any given process Pn.

The algorithms searches sequentially starting from first memory block and searches for the memory block that

fulfills the following condition –

● Can accommodate the process size


● Leaves the largest wasted space (fragmentation) after the process is allocated to given memory block

Jyoti Chavhan Dr. Jyoti Chavhan


Jyoti Chavhan Dr. Jyoti Chavhan
Jyoti Chavhan Dr. Jyoti Chavhan
4. Next Fit Allocation

This strategy is the modified version of the First fit because in Next Fit and in this memory is searched for empty
spaces similar to the first fit memory allocation scheme. But it differs from the first fit as when called Next time it
starts from where it let off and not from the beginning.

Jyoti Chavhan Dr. Jyoti Chavhan


Jyoti Chavhan Dr. Jyoti Chavhan
Jyoti Chavhan Dr. Jyoti Chavhan
Paging in OS
● Paging is a storage mechanism used in OS to retrieve processes from secondary storage to the
main memory as pages.
● The primary concept behind paging is to break each process into individual pages. Thus the
primary memory would also be separated into frames.
● One page of the process must be saved in one of the given memory frames.
● These pages can be stored in various memory locations, but finding contiguous
frames/holes is always the main goal.
● Process pages are usually only brought into the main memory when they are needed;
else, they are stored in the secondary storage.
● The frame sizes may vary depending on the OS.
● Each frame must be of the same size.
● Since the pages present in paging are mapped on to the frames, the page size should be
similar to the frame size.

Jyoti Chavhan Dr. Jyoti Chavhan


Paging in OS
• Partition memory into small equal fixed-size chunks and
divide each process into the same size chunks
• The chunks of a process are called pages
• The chunks of memory are called frames

Jyoti Chavhan Dr. Jyoti Chavhan


• Operating system maintains a page table for
each process
– Contains the frame location for each page in the
process
– Memory address consist of a page number and
offset within the page

Jyoti Chavhan Dr. Jyoti Chavhan


Processes and Frames
A.0
A.1
A.2
A.3
D.0
B.0
D.1
B.1
D.2
B.2
C.0
C.1
C.2
C.3
D.3
D.4

Jyoti Chavhan Dr. Jyoti Chavhan


Page Table

Jyoti Chavhan Dr. Jyoti Chavhan


Page Number Offset
Page Table:

Jyoti Chavhan Dr. Jyoti Chavhan


Example
Assuming that the main memory is 16 KB and the frame size is 1 KB, the main memory will be partitioned into a collection of 16 1
KB frames. P1, P2, P3, and P4 are the four processes in the system, each of which is 4 KB in size. Each process is separated into 1
KB pages, allowing one page to be saved in a single frame.

Because all of the frames are initially empty, the pages of the processes will be stored in a continuous manner. The graphic below
depicts frames, pages, and the mapping between them.

Jyoti Chavhan Dr. Jyoti Chavhan


When to Use Paging
● You have too many processes running and not enough physical memory to store them all.
● When you have a large process that can be split into multiple pages.
● When you want to load a process into physical memory without loading the entire process
Let‘s understand the real-life analogy first:
Let’s assume you have a cake which you want to store in the refrigerator. There will be two situations in this case.
● When you have enough space:
● When you don’t have enough space:

Jyoti Chavhan Dr. Jyoti Chavhan


Segmentation in OS (Operating System)
In Operating Systems, Segmentation is a memory management technique in which the memory is divided into the variable
size parts. Each part is known as a segment which can be allocated to a process.

The details about each segment are stored in a table called a segment table. Segment table is stored in one (or many) of
the segments.

Segment table contains mainly two information about segment:

1. Base: It is the base address of the segment


2. Limit: It is the length of the segment.

Jyoti Chavhan Dr. Jyoti Chavhan


Jyoti Chavhan Dr. Jyoti Chavhan
Segmentation in OS
Segmentation divides processes into smaller subparts known as modules. The divided segments need not be placed
in contiguous memory. Since there is no contiguous memory allocation, internal fragmentation does not take place.
The length of the segments of the program and memory is decided by the purpose of the segment in the user
program.We can say that logical address space or the main memory is a collection of segments.

Jyoti Chavhan Dr. Jyoti Chavhan


Segment Table and its Uses
Segment Table is used to store the information of all segments of the process. As we know, the CPU generates a logical address and
for its conversion to the physical address, a segment table is used. The mapping of a two-dimensional Logical address to a
one-dimensional Physical address is done using the segment table.

The Segment Table has two components:

1. Segment Base: The segment base is also known as the base address of a segment. The segment base contains the
starting physical address of the segments residing in the memory.
2. Segment Limit: The segment limit is also known as segment offset. The segment contains the specific length of the
segment.
The basic overview of the Segment Table is shown below.

Jyoti Chavhan Dr. Jyoti Chavhan


Types of Segmentation
Segmentation can be divided into two types:

1. Virtual Memory Segmentation: Virtual Memory Segmentation divides the processes into n number of segments. All
the segments are not divided at a time. Virtual Memory Segmentation may or may not take place at the run time of a
program.
2. Simple Segmentation: Simple Segmentation also divides the processes into n number of segments but the
segmentation is done all together at once. Simple segmentation takes place at the run time of a program. Simple
segmentation may scatter the segments into the memory such that one segment of the process can be at a different
location than the other(in a noncontinuous manner).

Jyoti Chavhan Dr. Jyoti Chavhan


Example of Segmentation
● Let's take the example of segmentation to understand how it works.
● Let us assume we have five segments namely: Segment-0, Segment-1, Segment-2, Segment-3, and Segment-4.
● Initially, before the execution of the process, all the segments of the process are stored in the physical memory
space.
● We have a segment table as well.
● The segment table contains the beginning entry address of each segment (denoted by base). The segment table
also contains the length of each of the segments (denoted by limit).
● As shown in the image below, the base address of Segment-0 is 1400 and its length is 1000, the base address
of Segment-1 is 6300 and its length is 400, the base address of Segment-2 is 4300 and its length is 400, and so
on.
● The pictorial representation of the above segmentation with its segment table is shown below.

Jyoti Chavhan Dr. Jyoti Chavhan


Jyoti Chavhan Dr. Jyoti Chavhan
What is 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.
How Virtual Memory Works?
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 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.

Jyoti Chavhan Dr. Jyoti Chavhan


What is Demand Paging?
A demand paging mechanism is very much similar to a paging system with swapping where processes stored in the secondary memory
and pages are loaded only on demand, not in advance.
So, when a context switch occurs, the OS never copy any of the old program’s pages from the disk or any of the new program’s pages
into the main memory. Instead, it will start executing the new program after loading the first page and fetches the program’s pages,
which are referenced.
During the program execution, if the program references a page that may not be available in the main memory because it was
swapped, then the processor considers it as an invalid memory reference. That’s because the page fault and transfers send control back
from the program to the OS, which demands to store page back into the memory.

Jyoti Chavhan Dr. Jyoti Chavhan


Page Replacement in OS

In Virtual Memory Management, Page Replacement Algorithms play an important role. The main objective of all the Page
replacement policies is to decrease the maximum number of page faults.

Page Fault – It is basically a memory error, and it occurs when the current programs attempt to access the memory page
for mapping into virtual address space, but it is unable to load into the physical memory then this is referred to as Page
fault.

Basic Page Replacement Algorithm in OS

Page Replacement technique uses the following approach. If there is no free frame, then we will find the one that is not
currently being used and then free it. A-frame can be freed by writing its content to swap space and then change the page
table in order to indicate that the page is no longer in the memory.

1. First of all, find the location of the desired page on the disk.
2. Find a free Frame: a) If there is a free frame, then use it. b) If there is no free frame then make use of the
page-replacement algorithm in order to select the victim frame. c) Then after that write the victim frame to the disk
and then make the changes in the page table and frame table accordingly.
3. After that read the desired page into the newly freed frame and then change the page and frame tables.
4. Restart the process.

Jyoti Chavhan Dr. Jyoti Chavhan


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.

Jyoti Chavhan Dr. Jyoti Chavhan


Figure: Page Replacement

Jyoti Chavhan Dr. Jyoti Chavhan


Page Replacement Algorithms in OS

This algorithm helps to decide which pages must be swapped out from the main memory in order to create a room for the
incoming page. This Algorithm wants the lowest page-fault rate.

Various Page Replacement algorithms used in the Operating system are as follows;

Jyoti Chavhan Dr. Jyoti Chavhan


7 0 1 2 0 3 0 4 2 3 0 3 1 2 0
Size of frame=3
FIFO
Find out: Total No.of hit=3
Total No. of fault/miss=12
Hit Ratio=?(No.of hits/Total no.of references)*100=20
LRU
Find out: Total No.of hit=
Total No. of fault/miss=
Jyoti Chavhan Dr. Jyoti Chavhan
Translation lookaside 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.

Jyoti Chavhan Dr. Jyoti Chavhan


Jyoti Chavhan Dr. Jyoti Chavhan
In translation lookaside 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 lookaside 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) %.

Jyoti Chavhan Dr. Jyoti Chavhan


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.

Jyoti Chavhan Dr. Jyoti Chavhan


GATE Question on TLB
GATE | GATE-CS-2014-(Set-3)
Consider a paging hardware with a TLB. Assume that the entire page table and all the pages are in
the physical memory. It takes 10 milliseconds to search the TLB and 80 milliseconds to access the
physical memory. If the TLB hit ratio is 0.6, the effective memory access time (in milliseconds) is
_________.
A. 120
B. 122
C. 124
D. 118
Given,
1. TLB hit ratio = 0.6
2. Therefore, TLB miss ratio = 0.4
3. Time taken to access TLB (t) = 10 ms
4. Time taken to access main memory (m) = 80 ms

Jyoti Chavhan Dr. Jyoti Chavhan


1.
Effective Access Time (EAT) = 0.6 ( 10 + 80 ) + 0.4 ( 10 + 80 + 80 ) = 90 X 0.6 + 0.4 X
170 = 122
Hence, the right answer is option B.

Jyoti Chavhan Dr. Jyoti Chavhan

You might also like