0% found this document useful (0 votes)
30 views174 pages

Unit - 3 Os-1

The document discusses various memory management strategies, including address binding, swapping, contiguous and non-contiguous memory allocation, and fragmentation. It covers concepts such as segmentation, paging, virtual memory management, and page replacement algorithms. Additionally, it explains the structure of the page table and techniques like demand paging and copy-on-write.

Uploaded by

techgyan1485
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)
30 views174 pages

Unit - 3 Os-1

The document discusses various memory management strategies, including address binding, swapping, contiguous and non-contiguous memory allocation, and fragmentation. It covers concepts such as segmentation, paging, virtual memory management, and page replacement algorithms. Additionally, it explains the structure of the page table and techniques like demand paging and copy-on-write.

Uploaded by

techgyan1485
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/ 174

UNIT -3

Memory-Management Strategies: Background, Swapping, Contiguous Memory


Allocation, Segmentation, Paging, Structure of the Page Table. Virtual-
Memory Management: Background, Demand Paging, Copy-on-Write, Page
Replacement, Allocation of Frames

Dr. M. SINTHUJA
MSRIT
Some issues happens during this process they are address binding,
relocation, address translation, dynamic loading linking, allocation
deallocation of memory etc.
Memory Management Strategies/Background
Address Binding like book binding, all the instructions, data if u want to keep in
main memory then assign address in the MM where u keep the instruction is
called as address binding.

Three techniques for address binding:

Compile time: if u know the memory location in advance then the absolute code
can be executed

LOad time: Can change the location and execute if u dont know the location that
is relocatable code

Execution time: only at the time of runtime u ll know the location of the
instruction of data.
Logical Vs Physical Address Space
Example AAdhar: Adhar no is hidden in latest id Virtual id is visible instead on
giving aadhar no to buy SIM u can use VID(virtual id). But in the AAdhar system
aadhar no only will be stored.
If u give VID who will convert the VID to aadhar. Memory Management
Example: VID is logical address it is generated by CPU
AAdhar id is my Physical address. Physical address means RAM locationla eruka
address
How the conversion takes place by memory management unit. It gets the logical
address from CPU and converts to the physical address.
Process execution will always doesn’t know the physical address. Process
generate logical address process knows only logical address.
RAM knows only physical address.
Set of logical address is known as LAS and set of physical address is known as PAS
This is called as dynamic relocation using relocation register
Key Points of Dynamic Relocation
SWAPPING
It is used in order to implement multiprogramming.

Multiple process can be stored in main memory.

The major advantage of multiprogramming is that CPU utilization is more efficient

Swapping of process from main memory to the secondary memory


Scenario 1: IF Process is executed it move to the secondary memory from main memory

When the P1 completes its execution it is transferred to the secondary memory (transferred to SM) (OS transfers the process from MM to SM bcz the size of MM is sma
So the next process enter the MM.

Scenario 2: IF Process1 is waiting for the I/O operation at that time CPU is idle it doesn’t need CPU for Process1 so OS
transfers that process1 from main memory to the secondary memory

Scenario 3: If the P1 tries to access OS area or any of the process protection violation occurs that is trap or interrupt. SO P1
Should be terminated as it tries to access other process. THe terminated process will be transferred from MM to SM.
Memory Allocation

Allocating memory to a process is memory allocation. How OS allocates memory


to the process.

1. Contiguous Memory Allocation


2. Non Contiguous Memory Allocation
Collection or block of memory is contiguous memory. For ex: P1 requires 5mb
then OS allocates 5MB in continuous is Contiguous Memory Allocation.

5MB allocated
contiguously is
Contiguous Memory
Allocation
Contiguous Memory Allocation

Partition is equal but not contineous


In Variable partition scheme, the partition can vary
Can’t fit 375kb to the partition
FRAGMENTATION
The conditions of fragmentation depend on the memory allocation system. As
the process is loaded and unloaded from memory, these areas are fragmented
into small pieces of memory that cannot be allocated to incoming processes.
It is called fragmentation.
Types of Fragmentation
There are mainly two types of fragmentation in the operating system. These
are as follows:

1. Internal Fragmentation
2. External Fragmentation

Internal Fragmentation
When a process is allocated to a memory block, and if the process is smaller
than the amount of memory requested, a free space is created in the given
memory block. Due to this, the free space of the memory block is unused,
which causes internal fragmentation.
To solve this problem,, going for the paging concept.
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.
Logical address
PAGING
Paging size and frame size should be equal
As continuous memory is not required in MM for paging it is called as non contiguous memory allocation
Paging is non contiguous memory allocation.
ADvantage : External fragmentation not required
To prevent external fragmentation compaction is required. Here no external fragmentation is required so no compaction is required
For each page no u will get corresponding frame number in
page table
OS will maintain free frame list here in this example 14.,13,18,20,15
are free space blue color are occupied now new process is entering
Page 0,1,2,3 so all the new process are assigned with the free space
in frame no 13,14,18,20 only frame no 15 is free now that will be
added to the free frame list.
Simultaneously entities can be searched is associative memory.
Translation look - aside buffer (TLB) is associative memory
In associative memory information is stored in the form of tags. Each tag is represented in the form of pair.
First component is key and second component is value pair.
If the page table or no is in TLB then u can
access directly the data/frame no to read or
If the page table not in TLB then Hit miss so here 2
write so 1 single memory is required so 100ns memory access is required so 200ns.
Hit ratio is given 0.8 so convert to percentage 80%. What it means? 80% of time u
get page table in TLB.

MEmory access is 100nano seconds.

Find effective access time?

Understand the concept: ‘p’-->page table in TLB means Hit means fetch the
frame, if not miss then u have to see the page table and then access frame
Memory Protection

● Memory protection is done with the help of protection


bits associated with each frame.
● Bits are maintained in page table. At the time of finding
frame no in page table, protection bits are also checked.
● Page is only read mode. Modify is not allowed for this
page or frame.
● Valid or invalid bit can be associated with page.
● Valid—>pages is in process logical address space
● invalid —>pages is not in process logical address space
Text editor shares its common code with the shared among process.
Structure of Page Table
The data structure that is used by the virtual memory system in the operating system of a
computer in order to store the mapping between physical and logical addresses is commonly
known as Page Table.

or Multilevel paging
Not optimised
Multilevel pages so partition page no into 12 & 10
If two level is not enough can go to three level
Virtual Memory Management
Physical address Logical or virtual address

Process P1 & P2 is divided into Pages


Virtual memory is a memory management technique where secondary
memory can be used as if it were a part of the main memory. Virtual
memory is a common technique used in a computer's operating system
(OS).
Virtual memory is bigger than the physical memory
Demand Paging
PAGE FAULT
IF the page is not in page table then it is called as page fault.

Page fault has 5 steps.

1. It checks Accessed page is valid or invalid. When u search for page E it is


invalid.
2. If i try to access A, B or C then it find free frame
3. swap the page to frame
4. change the bit to valid.
5. Restart the instruction
Page fault has 5 steps.

1. It checks Accessed page is valid or


invalid. When u search for page E it is
invalid.
2. If i try to access A, B or C then it find
free frame
3. swap the page to frame
4. change the bit to valid.
5. Restart the instruction
Some basic pages will be there in MM but when it comes to pure demand paging it
starts a process with no page in MM which results in page fault..

In one instruction multiple pages can be accessed it sometime may result page fault.
TTo fetch and decode instructions there is possibility of multiple page fault. Locality of
reference later.
Demand paging requires hardware support.
So page table with V/I bit, secondary mem, start the instruction
Copy-on-Write

Parent (P1, P2, P3)

1. Child can access all the pages of parent

2. Child can read all the pages


C1
C2 3. When it comes to write it cannot do it
C3
directly to original copy it can make a copy of it
aNd can do it.
Page Replacement Need
Need? Main Memory is full
OS

C
Page Replacement

What happens if there is no free frame?


•Page replacement - find some page in memory, but not
really in use, swap it out
-Algorithm
-Performance - want an algorithm which will result in minimum
number of page faults
•Same page may be brought into memory several times
To prevent Over allocation of memory by modifying the page fault service
routine to include page replacement

Use modify bit(dirty bit) to reduce over heads of page transfers.

Over head means Swap in swap out from MM to reduce Page replacement
algm
STEPS IN PAGE REPLACEMENT :
❖ Find the location of the desired page on the disk.

❖ Find a free frame :


❖ If there is a free frame, use it.
If there is no free frame, use a page replacement
algorithm to select a victim frame.
Write the victim frame to the disk, change the page and
frame tables accordingly.
❖ Read the desired page into the newly freed frame,
change the page and frame tables.
❖ Restart the user process.
When the page is not in the memory it is
called as page fault. Whenever the page
fault happens it takes the page from
secondary memory to main memory but
assumes that MM is completely full then
one of the page in MM is replaced with
new page from secondary memory.
Six techniques
1. FIFO
2. Optimal Numericals + important
3. Least Recently Used (LRU)
4. Least Recently Used approximate
5. Counting based
6. Page Buffering
FIFO ALGORITHM
Reference string is the pages CPU is demanding.
MM cannot store all pages bcz MM size< Logical memory.
CPU is demanding for the page no it checks whether it is
available in memory if it is not present in MM it is called as
page fault.
CPU is demanding for the page no it checks whether it is
available in memory if it is present in MM it is called as page
hit.

Increase in frames will cause more page fault.


FIFO ALGORITHM

•In all our examples, the reference string is


7,0,1,2,0,3,0,4,2,3,0,3,1,2,0

F1 7
CPU demanding for page 7. It is not in memory it is called as
F2 page fault.
As it is not in MM the CPU will transfer the control to OS from
F3 OS to Disk and from disk getting the 7th page and place it in
the MM.

Page fault 1,
FIFO ALGORITHM

•In all our examples, the reference string is


7,0,1,2,0,3,0,4,2,3,0,3,1,2,0

7 7
CPU demanding for page 0. It is not in memory it is called as
F1 0 page fault.

F2

F3
Page fault 1, 2
FIFO ALGORITHM

•In all our examples, the reference string is


7,0,1,2,0,3,0,4,2,3,0,3,1,2,0

7 7 7
CPU demanding for page 1. It is not in memory it is called as
F1 0 0 page fault.

F2 1
F3 Page fault 1, 2, 3
FIFO ALGORITHM

•In all our examples, the reference string is


7,0,1,2,0,3,0,4,2,3,0,3,1,2,0

7 7 7 2 CPU demanding for page 2. It is not memory it is called a


page fault. But the memory is full page replacement to be
F1 0 0 0 done so find the first entry(FIFO) and replace 7 with 2
F2 1 1
F3
Page fault 1, 2,3,4 7—> disk (swapped)
2—->2
FIFO ALGORITHM

n all our examples, the reference string is


7,0,1,2,0,3,0,4,2,3,0,3,1,2,0

7 7 7 2 2
Page 0 is already existing in MM it is called as
F1 0 0 0 0 Hit. No changes
F2 1 1 1
F3 hit
FIFO ALGORITHM

In all our examples, the reference string is


7,0,1,2,0,3,0,4,2,3,0,3,1,2,0
7 7 7 2 2 2
Demanded is Page 3 it is not in MM it is called
0 0 0 0 3
as page fault. So Replace 0 with 3 (FIFO)
1 1 1 1
hit

Page fault 1, 2,3,4,5


FIFO ALGORITHM

n all our examples, the reference string is


7,0,1,2,0,3,0,4,2,3,0,3,1,2,0

7 7 7 2 2 2 2
Demanded is Page 0 it is not in MM it is called
F1 0 0 0 0 3 3 as page fault. So Replace 1 with 0 (FIFO)
F2 1 1 1 1 0
F3
hit

Page fault 1, 2,3,4,5,6


FIFO ALGORITHM

n all our examples, the reference string is


7,0,1,2,0,3,0,4,2,3,0,3,1,2,0
7 7 7 2 2 2 2 4
Demanded is Page 4 it is not in MM it is called
0 0 0 0 3 3 3 as page fault. So Replace 2 with 4 (FIFO)
1 1 1 1 0 0
hit

Page fault 1, 2,3,4,5,6,7


FIFO ALGORITHM

n all our examples, the reference string is


7,0,1,2,0,3,0,4,2,3,0,3,1,2,0
7 7 7 2 2 2 2 4 4 Demanded is Page 2 it is not in MM it is called
as page fault. So Replace 3 with 2 (FIFO)
0 0 0 0 3 3 3 2

1 1 1 1 0 0 0

hit
Page fault 1, 2,3,4,5,6,7,8
FIFO ALGORITHM

n all our examples, the reference string is


7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1
7 7 7 2 2 2 2 4 4 4 Demanded is Page 3 it is not in MM it is called
as page fault. So Replace 0 with 3 (FIFO)
0 0 0 3 3 3 2 2
0
1 1 1 1 0 0 0 3
Page fault 1, 2,3,4,5,6,7,8,9
hit
FIFO ALGORITHM

n all our examples, the reference string is


7,0,1,2,0,3,0,4,2,3,0,3,1,2,0
7 7 7 2 2 2 2 4 4 4 0 Demanded is Page 0 it is not in MM it is called
as page fault. So Replace 4 with 0 (FIFO)

0 0 0 3 3 3 2 2 2
0
1 1 1 1 0 0 0 3 3
hit
Page fault 1, 2,3,4,5,6,7,8,9,10
FIFO ALGORITHM

n all our examples, the reference string is


7,0,1,2,0,3,0,4,2,3,0,3,1,2,0
7 7 7 2 2 2 2 4 4 4 0 0 Demanded is Page 3 it is in MM it is called as
page hit.

0 0 0 3 3 3 2 2 2 2
0
1 1 1 1 0 0 0 3 3 3
hit Page fault 1, 2,3,4,5,6,7,8,9,10
hit
FIFO ALGORITHM

In all our examples, the reference string is


7,0,1,2,0,3,0,4,2,3,0,3,1,2,0
7 7 7 2 2 2 2 4 4 4 0 0 0
Demanded is Page 1 it is not in MM it is
as page fault. 2 replaced with 1
0 0 0 3 3 3 2 2 2 2 1
0
1 1 1 1 0 0 0 3 3 3 3
hit hit
Page fault 1, 2,3,4,5,6,7,8,9,10,11
FIFO ALGORITHM

n all our examples, the reference string is


7,0,1,2,0,3,0,4,2,3,0,3,1,2,0
7 7 7 2 2 2 2 4 4 4 0 0 0 0
Demanded is Page 2 it is not in MM it is
as page fault. 3 replaced with 2
0 0 0 3 3 3 2 2 2 2 1 1
0
1 1 1 1 0 0 0 3 3 3 3 2
hit hit
Page fault 1, 2,3,4,5,6,7,8,9,10,12
FIFO ALGORITHM

● In all our examples, the reference string is


7,0,1,2,0,3,0,4,2,3,0,3,1,2,0
7 7 7 2 2 2 2 4 4 4 0 0 0 0 0 Page fault=12 & Hit= 3
Hit ratio= no of hits/total no of reference
= 3/15*100
0 0 0 3 3 3 2 2 2 2 1 1 1 = 20%
0 Pagefault ratio=no of pagefault/total no
reference
1 1 1 1 0 0 0 3 3 3 3 2 2
= 12/15*100
hit hit hit =80%
So the performance is worst as hit ratio
Demanded is Page 0 it is in 20%
MM it is called as hit. Page fault 1, 2,3,4,5,6,7,8,9,10,12
An increase in frames will cause more page fault.
OPTIMAL ALGORITHM
Optimal Page Replacement
Find the fault and hit ratio.
LEAST RECENTLY USED
Least Recently Used (LRU)

This may require substantial hardware assistance. For hardware


assistance 2 methods are used.
For hardware assistance 2 methods are used.

1. Counter (it will keep track of all the pages & how many
times the page is used/unused with the details)

2. Stack (recently used will on the top of stack and least used
will bottom of the stack) Linked list is used (double linked list)
Practice questions

1. Consider the following page reference string,


“1,2,3,4,2,1,5,6,2,1,2,3,7,6,3,2,1,2,3,6”.
Calculate the number of page faults and page hits with four
frames using:
● LRU replacement techniques
● Optimal replacement techniques

2. Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3,


2, 3 with 4 page frame. Find number of page fault using Optimal
Page Replacement Algorithm.
LRU (Least Recently Used) Page Replacement Algorithm:

In the LRU algorithm, the page that has not been used for the longest period is replaced when a new page is needed. We track the
usage of the pages and replace the least recently used one when a page fault occurs.

Steps:

1. 1: Fault → [1]
2. 2: Fault → [1, 2]
3. 3: Fault → [1, 2, 3]
4. 4: Fault → [1, 2, 3, 4]
5. 2: Hit → [1, 2, 3, 4] (no change)
6. 1: Hit → [1, 2, 3, 4] (no change)
7. 5: Fault → [2, 3, 4, 5] (1 is replaced)
8. 6: Fault → [3, 4, 5, 6] (2 is replaced)
9. 2: Fault → [4, 5, 6, 2] (3 is replaced)
10. 1: Fault → [5, 6, 2, 1] (4 is replaced)
11. 2: Hit → [5, 6, 2, 1] (no change)
12. 3: Fault → [6, 2, 1, 3] (5 is replaced)
13. 7: Fault → [2, 1, 3, 7] (6 is replaced)
14. 6: Fault → [1, 3, 7, 6] (2 is replaced)
15. 3: Hit → [1, 3, 7, 6] (no change)
16. 2: Fault → [1, 7, 6, 2] (3 is replaced)
17. 1: Hit → [1, 7, 6, 2] (no change)
18. 2: Hit → [1, 7, 6, 2] (no change)
19. 3: Fault → [7, 6, 2, 3] (1 is replaced)
20. 6: Hit → [7, 6, 2, 3] (no change)

Summary for LRU:

● Page faults: 14
● Page hits: 6
Optimal Page Replacement Algorithm:

The Optimal page replacement algorithm replaces the page that will not be used for the longest time in the future. This is a
theoretical algorithm and is typically used to analyze the performance of page replacement strategies.

Steps:

1. 1: Fault → [1]
2. 2: Fault → [1, 2]
3. 3: Fault → [1, 2, 3]
4. 4: Fault → [1, 2, 3, 4]
5. 2: Hit → [1, 2, 3, 4] (no change)
6. 1: Hit → [1, 2, 3, 4] (no change)
7. 5: Fault → [1, 2, 3, 5] (4 is replaced)
8. 6: Fault → [1, 2, 5, 6] (3 is replaced)
9. 2: Hit → [1, 2, 5, 6] (no change)
10. 1: Hit → [1, 2, 5, 6] (no change)
11. 2: Hit → [1, 2, 5, 6] (no change)
12. 3: Fault → [1, 2, 5, 3] (6 is replaced)
13. 7: Fault → [1, 2, 5, 7] (5 is replaced)
14. 6: Fault → [1, 2, 7, 6] (3 is replaced)
15. 3: Fault → [1, 2, 7, 3] (5 is replaced)
16. 2: Hit → [1, 2, 7, 3] (no change)
17. 1: Hit → [1, 2, 7, 3] (no change)
18. 2: Hit → [1, 2, 7, 3] (no change)
19. 3: Hit → [1, 2, 7, 3] (no change)
20. 6: Fault → [1, 2, 7, 6] (7 is replaced)

Summary for Optimal:

● Page faults: 12
● Page hits: 8
LRU Approximation Page Replacement

1. Second -Chance Algorithm

Every page will have reference bit = 0 (page not used)

Bit =1 (page is used)


Replace the page or element whose reference bit is 0.
Push new element
1 7 0 2

0 4 0 6

1 2 1 7

1 6 0 4

Pop the element


ENHANCED SECOND CHANCE ALGORITHM
It is similar to second chance algorithm but here it maintains 2 bits per each page.
( Reference bit & Modify bit)
0-Not modified 1-modified
0-Not referred 1-referred

Based on these two values of each page it will decide which place to to be replaced by new
one.
(0,0) →( Reference bit 0 & Modify bit 0) best place to replace
(0,1) —> Not quite as good
(1,0) —>May be used again soon
(1,1) —-> MAy be used again soon & need to be written on the disk
The best is (0,0)
COUNTING BASED PAGE REPLACEMENT
Page Buffering Algorithm
Page Buffering Algorithm
● As an add-on to any previous algorithm.
● A pool of free frames is maintained.
● When a page fault occurs, the desired page is read into a
free frame from the pool. The victim frame is later
swapped out if necessary and put into the free frames pool.
Advantage / disadvantage
● Plus - Process is put back to ready queue faster.
● Minus - less pages are in use overall.
● VAX/VMS version - basic FIFO replacement with a free
frame pool. A victim is put into the pool but the original
virtual address is kept. When a page fault occurs, we first
look in the pool. If we find the page there - no need for
disk operation.
Allocation of Frames
How to allocates frame? What is the minimum no of frames and maximum number
of frames to be allocated?

Based on the process minimum no of frames will be allocated.

Maximum—-> maximum memory


Equal Allocation:
Process P1 requires 10 frames

Process P2 requires 30 frames

Main memory =40frames

P1 requires 10 frames as it is equal allocation so, 20 frames is given for P1

P2requires 30 frames as it is equal allocation. 20 frames is given for P2

which is not sufficient and wastage of frames in P1. To overcome moving for

proportional allocation.
Proportional Allocation

Based on the size of the process, the available frames will be allocated.

P1=(10/10+30) * 40 = 10 frames will be allocated to P1

P2= (30/10+30)*40= 30 frames will be allocated to P2

So wastage of frames.

Disadvantage:

It doen’t distinguish between high and low priority processes.


PRIORITY ALLOCATION

Based on the priority of the process frame is allocated. Higher priority—->more


frames
Lower Priority—-->less frames
GLOBAL VS LOCAL ALLOCATION
NON UNIFORM MEMORY ACCESS (NUMA)

You might also like