0% found this document useful (0 votes)
23 views51 pages

Unit - 5 - OSY - Memory MGT - PSG

Uploaded by

info.swiftgroww
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)
23 views51 pages

Unit - 5 - OSY - Memory MGT - PSG

Uploaded by

info.swiftgroww
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/ 51

Unit:05

Memory Management
Mrs.P.S.Gaidhani
CO: Calculate efficiency of different memory Management Techniques

Total Marks:14
Q.What is Memory Management?

➢ Memory Management is the process of controlling and coordinating


computer memory, assigning portions known as blocks to various running
programs to optimize the overall performance of the system.
➢ Memory Management which handles or manages primary memory.
➢ It is the most important function of an operating system that manages primary
memory.
➢ It check show much memory is to be allocated to processes.
➢ It decides which process will get memory at what time.
➢ It tracks whenever some memory gets free do run allocated and correspondingly
it updates the status.
➢ Two important terms considered while loading programs into the partition are
internal fragmentation and external fragmentation.
What is Memory Management?
Functions of memory management:-
• Process isolation - processes cannot interfere with other processes
• Automatic allocation and management - allocation is transparent, virtual
• Support of modular programming - modules can be of varying size
• Protection and access control - at times memory needs to be shared
• Long-term storage - a file system
Q. Explain Basic Memory Management Partitioning:

➢ For better utilization of memory and flow of execution, we divide the memory
into different sections to be used by the resident programs.
➢ The process of dividing the memory into sections is called memory
partitioning.
➢ There are different ways in which memory can be partitioned:

1. Fixed Partitioning/Static Partitioning


2. Variable/Dynamic Partitioning
1. Fixed Partitioning/Static Partitioning:
➢ Main memory is divided into multiple partitions of fixed size at the time of system
generation.
➢ A process may be loaded into a partition of equal size or greater size.
➢ Partitions can be of equal size or unequal size.
1. Fixed Partitioning/Static Partitioning:
Equal size partitioning:-
• Main memory is divided into equal size partitions.
• Any process with less or equal size can be loaded in any available partition.
• If all partitions are full and no process is in ready or running state then the
operating system can swap a process out of any partition and load in any other
process.
Disadvantage: -Main memory Utilization is extremely inefficient.
Unequal size partitioning:-
• Main memory is divided into multiple partitions of unequal size.
• Each process can be loaded into the smallest partition within which the process will
fit.
1. Fixed Partitioning/Static Partitioning:
1. Fixed Partitioning/Static Partitioning:

Advantages:
• Simple to implement
• It requires minimal operating system software and processing overhead as
partitions are fixed at the time of system generation.
Disadvantages:
• Memory wastage
• Inefficient use of memory due to internal fragmentation.
• Maximum number of active processes is fixed.
2. Variable Partitioning/Dynamic Partitioning:
➢ In dynamic partitioning, the primary memory is emptied, and partitions are
made during the run time according to the needs of the different processes.
➢ The size of the partition will be equal to the incoming process.
➢ The number of partitions will not be fixed and will depend on the number of
incoming processes.
➢ It will also depend on the size of the main memory.
➢ The partition size varies according to the need of the processes.
2. Variable Partitioning/Dynamic Partitioning:
For example:-Consider following table with
process and memory space
2. Variable Partitioning/Dynamic Partitioning:

Advantages:
• No internal fragmentation.
• More efficient use of main memory.
Disadvantages:
• It suffers from external fragmentation.
• It needs compaction
Example:
Consider the following memory map and assume a new process P4 comes with a
memory requirement of 3KB .Locate this process.
Q. Explain Free Space Management Techniques:
➢ The system keeps tracks of the free disk blocks for allocating space to files when
they are created.
➢ Also, to reuse the space released from deleting the files, free space management
becomes crucial.
➢ The system maintains a free space list which keeps track of the disk blocks that
are not allocated to some file or directory.
➢ The free space list can be implemented mainly as:

1. Bitmap
2. Linked list
3. Grouping
4. Counting
1. Bitmap:
➢ A Bitmap or Bit Vector is series or collection of bits where each bit
corresponds to a disk block.
➢ The bit can take two values: 0 and 1:
➢ 0 indicates that the block is allocated and 1 indicates a free block.
Example 1:
➢ The given instance of disk blocks on the disk in Figure 1 (where green blocks
are allocated)
➢ It can be represented by a bitmap of 16 bits as: 0000111000000110.
Example 2:
2. Linked List:
➢ In this approach, the free disk blocks are linked together i.e. a free block
contains a pointer to the next free block.
➢ The block number of the very first disk block is stored at a separate location
on disk and is also cached in memory.
Example 1:
➢ The given instance of disk blocks on the disk in Figure 2 (where green blocks
are allocated)
➢ The free space list head points to Block 5 which points to Block 6, the next
free block and so on. The last free block would contain a null pointer
indicating the end of free list.
Q. Define Virtual Memory:
➢ Virtual Memory is a storage mechanism which offers user an illusion
(impression) 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.
➢ As computers have RAM of capacity 64 or 128 MB to be used by the CPU resources
which is not sufficient to run all applications that are used by most users in their
expected way and all at once
Virtual Memory:
Non-Contiguous Memory Allocation-

•Non-contiguous memory allocation is a memory allocation technique.


•It allows to store parts of a single process in a non-contiguous fashion.
•Thus, different parts of the same process can be stored at different places in the main
memory.

Techniques-
There are two popular techniques used for non-contiguous memory allocation-
Q.Define paging :
➢ 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 paging technique divides the physical memory(main memory) into fixed-size blocks
that are known as Frames
➢ Divide the logical memory(secondary memory) into blocks of the same size that are
known as Pages.
➢ Paging is a fixed size partitioning scheme.
➢ In paging, secondary memory and main memory are divided into equal fixed size
partitions.
➢ The partitions of secondary memory are called as pages.
➢ The partitions of main memory are called as frames.
➢ Frame size=page size
Introduction to paging:
Example 1:
Assume 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.
Example 1:
Explanation of Paging Mechanism :
Q. Define Segmentation:
Segmentation

Characteristics-
Segmentation is a variable size partitioning scheme.
• In segmentation, secondary memory and main memory are divided into partitions
of unequal size.
• The size of partitions depend on the length of modules.
• The partitions of secondary memory are called as segments.
Example-
Consider a program is divided into 5 segments as-
Segmentation:
Segmentation:
Segment Table-
Segment table is a table that stores the information about each segment of the process.
•It has two columns.
•First column stores the size or length of the segment.
•Second column stores the base address or starting address of the segment in the main memory.
•Segment table is stored as a separate segment in the main memory.
•Segment table base register (STBR) stores the base address of the segment table.

For the above illustration, consider the segment table is-


Segmentation:
Q. Differentiate between paging and segmentation
No. Paging Segmentation
1 Paging divides program into fixed size Segmentation divides program into variable size segments.
pages.
2 OS is responsible Compiler is responsible.
3 Paging is faster than segmentation Segmentation is slower than paging
4 Paging is closer to Operating System Segmentation is closer to User
5 It suffers from internal fragmentation It suffers from external fragmentation
6 There is no external fragmentation There is no external fragmentation
7 Logical address is divided into page Logical address is divided into segment number and
number and page offset segment offset

8 Page table is used to maintain the page Segment Table maintains the segment information
information.
9 Page table entry has the frame number Segment table entry has the base address of the segment
and some flag bits to represent details and some protection bits for the segments.
about pages.
Q.Differences between Paging and Segmentation:
Paging Segmentation

Paging is a memory management technique where memory is Segmentation is also a memory management technique where
partitioned into fixed-sized blocks that are commonly known memory is partitioned into variable-sized blocks that are
as pages. commonly known as segments.

With the help of Paging, the logical address is divided into a page With the help of Segmentation, the logical address is divided
number and page offset. into section number and section offset.

This technique may lead to Internal Fragmentation. Segmentation may lead to External Fragmentation.

While in Segmentation, the size of the segment is decided by the


In Paging, the page size is decided by the hardware.
user.

In order to maintain the page data, the page table is created in the In order to maintain the segment data, the segment table is created
Paging in the Paging

The segment table mainly contains the segment number and the
The page table mainly contains the base address of each page.
offset.

This technique is faster than segmentation. On the other hand, segmentation is slower than paging.

In Paging, a list of free frames is maintained by the Operating In Segmentation, a list of holes is maintained by the Operating
system. system.

In this technique, in order to calculate the absolute address page In this technique, in order to calculate the absolute address
number and the offset both are required. segment number and the offset both are required.
Q. Define Fragmentation:
➢ Memory space in the system constantly goes through loading and releasing
processes and their resources because of which the total memory spaces gets
broken into a lot of small pieces, this causes creation small non utilized
fragmented memory spaces, which are so small that normal processes can not fit
into those small fragments, causing those memory spaces not getting utilized at
all, this is called memory Fragmentation in operating system.

➢ Fragmentation is of the following two types –


1. Internal Fragmentation
2. External Fragmentation
What is Internal Fragmentation?

• It somehow relates to fixed size partitioning.

• The system allocates memory to various programs and processes by dividing


them into small blocks as required by the program.

• However, more memory is allocated sometimes than is needed by the process,


which eventually results in excess memory going waste or left unused.

• When a program is allocated to a memory block, if that


program is lesser than this memory block and
remaining space goes wasted, this situation is called
internal fragmentation.
• Generally, internal fragmentation memory partition is
static or fixed.
1. Internal Fragmentation:
➢ Internal Fragmentation is a
problem that occurs when
the process is allocated to a
memory block whose size
is more than the size of that
process and due to which
some part of the memory is
left unused.
➢ Thus the space wasted
inside the allocated
memory block is due to the
restriction on the allowed
sizes of allocated blocks.
What is External Fragmentation?
• The main memory forms holes between portions of allocated memory that are too small
to hold any process.
• In simple terms, the non-contiguous blocks create holes in the memory resulting in
unused storage that are outside the allocated regions, meaning it cannot be used along
with the main memory for larger memory tasks.
• They end up being isolated and cannot be totally eliminated from the memory space.
• This is called external fragmentation.
• It can be removed by compaction which shuffles contents of the memory to place all
free memory together.
2. External Fragmentation:
➢ External Fragmentation
occurs when the memory
blocks of variable-size are
allocated to the processes
dynamically.
➢ When the memory space in
the system can easily
satisfy the requirement of
the processes, but this
available memory space
is non-contiguous, So it
can’t be utilized further.
Then this problem is
referred to as External
Fragmentation.
Q. What is Page Fault:
➢ A page fault is occurs when a program attempts to access a block of memory that
is not stored in the physical memory, or RAM.
➢ The fault notifies the OS that it must locate the data in Virtual Memory, then transfer
it from the storage device, such as an HDD or SSD, to the system RAM.
Q.Define -Swap in and Swap Out in OS

1.Swap-out is a method of removing a process from RAM and adding it to the hard disk.
2.Swap-in is a method of removing a program from a hard disk and putting it back into the
main memory or RAM.
Memory Management

Chapter 5:Algorithms
First In First Out (FIFO) page replacement algorithm –
This is the simplest page replacement algorithm. In this algorithm, operating system keeps track of all
pages in the memory in a queue, oldest page is in the front of the queue. When a page needs to be
replaced page in the front of the queue is selected for removal.

Example -1. Consider page reference string 1, 3, 0, 3, 5, 6 and 3 page slots.


Initially all slots are empty, so when 1, 3, 0 came they are allocated to the empty slots —> 3 Page
Faults.
when 3 comes, it is already in memory so —> 0 Page Faults.
Then 5 comes, it is not available in memory so it replaces the oldest page slot i.e 1. —>1 Page Fault.
Finally 6 comes, it is also not available in memory so it replaces the oldest page slot i.e 3 —>1 Page
Fault.
So total page faults = 5.

Example -2. Consider the following reference string: 0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1.


Using FIFO page replacement algorithm –

So, total number of page faults = 9.

• Optimal Page replacement –


In this algorithm, pages are replaced which would not be used for the longest duration of time in
the future.
Example-2:Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, with 4 page frame. Find
number of page fault.

Initially all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults
0 is already there so —> 0 Page fault.
when 3 came it will take the place of 7 because it is not used for the longest duration of time in
the future.—>1 Page fault.
0 is already there so —> 0 Page fault..

GGSP Nashik
Memory Management

4 will takes place of 1 —> 1 Page Fault.

Now for the further page reference string —> 0 Page fault because they are already available in
the memory.
Optimal page replacement is perfect, but not possible in practice as the operating system cannot
know future requests. The use of Optimal Page replacement is to set up a benchmark so that
other replacement algorithms can be analyzed against it.
Least Recently Used – (LRU)
In this algorithm page will be replaced which is least recently used.
Example-3Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2 with 4 page
frames.Find number of page faults.

Initially all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults
0 is already their so —> 0 Page fault.
when 3 came it will take the place of 7 because it is least recently used —>1 Page fault
0 is already in memory so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault
Now for the further page reference string —> 0 Page fault because they are already available in
the memory.

Q. Consider a reference string: 4, 7, 6, 1, 7, 6, 1, 2, 7, 2. the number of frames in the memory is 3. Find


out the number of page faults respective to:

1. Optimal Page Replacement Algorithm


2. FIFO Page Replacement Algorithm
3. LRU Page Replacement Algorithm

Optimal Page Replacement Algorithm

GGSP Nashik
Memory Management

Number of Page Faults in Optimal Page Replacement Algorithm = 5

LRU Page Replacement Algorithm

Number of Page Faults in LRU = 6

FIFO Page Replacement Algorithm

GGSP Nashik
Memory Management

Number of Page Faults in FIFO = 6

A system uses 3 page frames for storing process pages in main memory. It uses the First in First out
(FIFO) page replacement policy. Assume that all the page frames are initially empty. What is the total
number of page faults that will occur while processing the page reference string given below-
4 , 7, 6, 1, 7, 6, 1, 2, 7, 2
Also calculate the hit ratio and miss ratio.

Solution-

Total number of references = 10

GGSP Nashik
Memory Management

From here,
Total number of page faults occurred = 6

Calculating Hit ratio-

Total number of page hits


= Total number of references – Total number of page misses or page faults
= 10 – 6
=4

Thus, Hit ratio


= Total number of page hits / Total number of references
= 4 / 10
= 0.4 or 40%

Calculating Miss ratio-

Total number of page misses or page faults = 6

Thus, Miss ratio


= Total number of page misses / Total number of references
= 6 / 10
= 0.6 or 60%

Alternatively,
Miss ratio
= 1 – Hit ratio
= 1 – 0.4
= 0.6 or 60%

GGSP Nashik
Memory Management

Problem-02:

A system uses 3 page frames for storing process pages in main memory. It uses the Least Recently
Used (LRU) page replacement policy. Assume that all the page frames are initially empty. What is the
total number of page faults that will occur while processing the page reference string given below-
4 , 7, 6, 1, 7, 6, 1, 2, 7, 2
Also calculate the hit ratio and miss ratio.

Solution-

Total number of references = 10

From here,
Total number of page faults occurred = 6

In the similar manner as above-


• Hit ratio = 0.4 or 40%
• Miss ratio = 0.6 or 60%

Problem-03:

GGSP Nashik
Memory Management

A system uses 3 page frames for storing process pages in main memory. It uses the Optimal page
replacement policy. Assume that all the page frames are initially empty. What is the total number of
page faults that will occur while processing the page reference string given below-
4 , 7, 6, 1, 7, 6, 1, 2, 7, 2
Also calculate the hit ratio and miss ratio.

Solution-

Total number of references = 10

From here,
Total number of page faults occurred = 5

GGSP Nashik
Memory Management

Difference between bitmap and linked list free space management techniques

Bitmap:
When free space is list is implemented as bit map, each block of disk is represented by a bit.
REPORT THIS AD

If the block is free, its bit is set to 1. If the block is allocated, the bit is 0.
For Example: Apple Macintosh operating system uses bit map method to allocate the disk space.
Assume the following are free. Rest are allocated:

Advantages:
▪ It is relatively simple.
▪ Efficient to find the free space on the disk.
▪ Fast random access allocation check
Disadvantages:
▪ This may require special hardware support to find the first 1 in a word if it is not 0.
▪ Wasteful on larger disks
▪ Poor scalability
Linked List:
In this method, a linked list of all the free disk blocks is maintained.
The first free block in the list can be pointed out by a head pointer, which is kept in a special location on the
disk.

Using this disk, It is not easy to search the free list.

Advantages:
▪ Whenever a file is to be allocated a free block, the operating system can simply allocate the
first block in free space list and move the head pointer to the next free block in the list.
Disadvantages:
▪ Searching the free space list will be very time consuming; each block will have to be read
from the disk, which is read very slowly as compared to the main memory.
▪ Not Efficient for faster access.

GGSP Nashik
Memory Management

Important Questions
Questions for 2 Marks
N Question
1 Define Fragmentation and give its types.
2 Define Virtual Memory.
3 Define paging with diagram.
4 Define term a) Page fault b) Segmentation.
5 Define Pages and Frames

Questions for 4 Marks


Q.N Question
1 Explain partitioning and its types.
2 Explain Optimal Page Replacement Algorithm with example
3 Explain difference between paging and segmentation?

Questions for 6 Marks

1.Explain LRU page replacement algorithm for following reference string.


7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 Calculate the page fault.
2.Given a page reference string (arrival) with four page frames, calculate the page faults
With FIFO and LRU page replacement algorithms respectively:
12, 3, 4, 5, 1, 2, 5, 1, 2, 3, 4, 5, 1,6,7,8,7,8,9,7,8,9,5,4,4,5,4,2.

3 List free space management techniques? Describe any one in detail

GGSP Nashik
Memory Management

Assignment No:05

Q1. Question for 2 marks (2*5=10)


1 Define Fragmentation and give its types.
2 Define Virtual Memory.
3 Define paging with diagram.
4 Define term a) Page fault b) Segmentation.
5 Define Pages and Frames

Questions for 4 Marks (4*5=20)


Q.N Question
1 Explain partitioning and its types.
2 Explain Optimal Page Replacement Algorithm with example
3 Explain difference between paging and segmentation?
4.Explain LRU page replacement algorithm for following reference string.
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 Calculate the page fault.
5.Given a page reference string (arrival) with four page frames, calculate the page faults
With FIFO and LRU page replacement algorithms respectively:
12, 3, 4, 5, 1, 2, 5, 1, 2, 3, 4, 5, 1,6,7,8,7,8,9,7,8,9,5,4,4,5,4,2.

GGSP Nashik

You might also like