0% found this document useful (0 votes)
15 views

Module 3 Pptos

Uploaded by

venugopal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Module 3 Pptos

Uploaded by

venugopal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 137

Operating Systems -

Module III
Deadlocks & Memory
Management
Deadlocks -
 System Model

 Deadlock Characterization

 Methods for Handling Deadlocks

 Deadlock Prevention
 Deadlock Avoidance

 Deadlock Detection

 Recovery from Deadlock

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Deadlock
 Waiting state of process → deadlock
 A situation were a waiting process will continue to be in infinite wait state,
because the resource it has requested is held by other waiting process.
Printer
R1
request
holds

P1 P2

holds
request

R2
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT CD Driver
Deadlock

• When processes request a resource and if the resources are not available at
that time the process enters into waiting state. Waiting process may not change its
state because the resources they are requested are held by other process. This
situation is called deadlock.

• A situation were a waiting process will continue to be in infinite wait state,


because the resource it has requested is held by other waiting process is called as
deadlock.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Deadlock

SYSTEM MODEL
• A system consists of finite number of resources and is distributed among number of
processes.
• A process must request a resource before using it and it must release the resource
after using it. It can request any number of resources to carry out a designated task. The
amount of resource requested may not exceed the total number of resources available.

A process may utilize the resources in only the following sequences:


1. Request:-If the request is not granted immediately then the requesting process must
wait until it can acquire the resources.
2. Use:-The process can operate on the resource.
3. Release:-The process releases the resource after using it.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


The Deadlock Problem
 A set of blocked processes each holding a resource and waiting to acquire
a resource held by another process in the set.

 Example
 System has 2 tape drives.
 P1 and P2 each hold one tape drive and each needs another one.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Bridge Crossing Example

 The bridge can be viewed as a resource.


 Some cars may need to wait, indefinetly
 Can be resolved if one car backs up (preempt resources and
rollback).
 Several cars may have to be backed up if a deadlock occurs.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


System Model
 Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices

 Each resource type Ri has W i instances. ---- Finite no. of resources


 Each process utilizes a resource as follows:
 request
 use
 Release / free

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
 Mutual exclusion: Atleast one resource exists, which can be used by only
one process at a time.
 Hold and wait: a process holding at least one resource & waiting to
acquire additional resources held by other processes.
 No preemption: resource cannot be released from a process
compulsorily.
 Circular wait: there exists a set {P0, P1, …, Pn} of waiting processes such
that P0 is waiting for a resource that is held by P1, P1 is waiting for a
resource that is held by P2, …, Pn–1 is waiting for a resource that is held by
Pn, and Pn is waiting for a resource that is held by P0.
R1 R2 R3

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT

P1 P2 P3
Resource-Allocation Graph
A set of vertices V and a set of edges E.
 V is of two types:
 P = {P1, P2, …, Pn }, the set consisting of all the processes in the
system.
P3

 R = {R1, R2, …, Rm}, the set consisting of all resource types in the
system.

 E is of three types
 Claim edge – directed edge P1 → Rj (dashed edge)
 request edge – directed edge P1 → Rj
 assignment edge / allocation edge – directed edge Rj → Pi
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Resource-Allocation Graph (Cont.)

 Process

 Resource Type with 4 instances

 Pi requests instance of Rj Pi

Rj

 Pi is holding an instance of Rj
Pi
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT Rj
Example of a Resource Allocation Graph

P= {P1,P2,P3} Resource instance:


R = {R1,R2,R3,R4} R1 – one instance
E ={P1->R1,P2->R3,R1->P2,R3->P3, R2 – two instance
R2->P1,R2->P2} R3 – one instance
R4 – three instance

R1 R3

P1 P2 P3

R2 R4

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Resource Allocation Graph With A Deadlock

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Basic Facts
 If graph contains no cycles  no deadlock.

 If graph contains a cycle 


 if only one instance per resource type, then deadlock.
 if several instances per resource type, possibility of
deadlock.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Methods for Handling Deadlocks
 Ensure that the system will never enter a deadlock state,
prevention and avoidance.

 Allow the system to enter a deadlock state, detect and then


recover.

 Ignore the problem and pretend that deadlocks never occur


in the system;

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Deadlock Prevention
 Mutual Exclusion – does not satisfy for sharable resources; holds for
non-sharable resources.
 Hold and Wait – . Mutual exclusion is not possible in sharable
resources and thus they cannot be involved in deadlock. Read-
only files are good examples for sharable resources. A process
never waits for accessing in a sharable resource. So we cannot
prevent deadlock by denying the mutual exclusion condition in
non-sharable resources.
 Hold and Wait – must guarantee that whenever a process requests
a resource, it does not hold any other resources.
 Process must request and be allocated with all its resources before it
begins execution (low resource utilization)
 Allow process to request resources only when the process has none.

CD-drive disk printer


DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Deadlock Prevention

• Request all its resources before execution begins


Eg:-consider a process that copies the data from a tape drive to the disk,
sorts the file and then prints the results to a printer. If all the resources are
allocated at the beginning then the tape drive, disk files and printer are
assigned to the process. The main problem with this is it leads to low
resource utilization because it requires printer at the last and is allocated
with it from the beginning so that no other process can use it.

• Request for resource only when it has none of the resources.


The process is allocated with tape drive and disk file, first. It performs the
required operation and releases both. Then the process once again
request for disk file and the printer.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Deadlock Prevention (Cont.)
 No Preemption –
 holding some resources requests another resource that cannot be
immediately allocated to it, then all resources currently being held
are released.
 Process will be restarted only when it can regain its old resources, as
well as the new ones that it is requesting.

 Circular Wait – impose a total ordering of all resource types, and require
that each process requests resources in an increasing order of
enumeration.
Eg - F(tape drive)=1
F(disk drive)=5
F(printer)=12
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Deadlock Prevention (Cont.)

 Circular Wait - Order all resource types


- Process should request resource in an increasing order of enumeration

Let R={R1,R2,………Rn} be the set of resource types. We assign each resource


type with a unique integer value. This will allows us to compare two resources
and determine whether one precedes the other in ordering.

Eg:-we can define a one to one function


F:R N as follows :
F(tape drive)=1
F(disk drive)=5
F(printer)=12
A process holding ‘disk drive’, cannot request for tapedrive. but it can request
for printer.
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Deadlock Prevention (Cont.)

 Circular Wait - Deadlock can be prevented by using the following protocol:


 - Each process can request the resource in increasing order. A process can
request any number of instances of resource type say Ri and it can request
instances of resource type Rj only F(Rj) > F(Ri).
 - Alternatively when a process requests an instance of resource type Rj, it has
released any resource Ri such that F(Ri) >= F(Rj).

 If these two protocol are used then the circular wait cannot hold.

 Eg: If a process is having an instance of disk drive, whose ‘N’ value is set as ‘5’.

 - Then it can request for printer. Because F(Printer) > F (disk drive).
 - If the process wants the tape drive, it has to release all the resources held.
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Deadlock Avoidance
 Deadlock prevention algorithm may lead to low device
utilization and reduces system throughput.
 Avoiding deadlocks requires additional information about
the sequence in which the resources are to be requested.
With the knowledge of the complete sequences of
requests and releases we can decide for each requests
whether or not the process should wait.
 For each requests it requires to check whether the
resources is currently available. If resources is available,
the OS checks if the resource is assigned will the system
lead to deadlock. If no then the actual allocation is done.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Deadlock Avoidance
 Each process must declare the maximum number of
resources of each type that it may need.

 The deadlock-avoidance algorithm dynamically examines


the resource-allocation state to ensure that there can never
be a circular-wait condition.
 It checks whether the system will be in safe state, even if the
requested resource is allocated. Later the actual allocation
of resource is done.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Safe State
A state is a safe state in which there exists at least one order in
which all the process will run completely without resulting in a
deadlock.
A system is in safe state if there exists a safe sequence.
A sequence of processes <P1,P2,………..Pn> is a safe sequence for
the current allocation state if for each Pi the resources, that Pi can
request can be satisfied by the currently available resources.

• If the resources that Pi requests are not currently available then Pi


can obtain all of its needed resource to complete its designated
task.
• A safe state is not a deadlock state.
• Whenever a process request a resource i.e., currently available,
the system must decide whether resources can be allocated
immediately or whether the process must wait. The request is granted
only if the allocation leaves the system in safe state.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Safe, Unsafe , Deadlock State

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


 If Pi resource needs are not immediately available, then Pi
should wait until all Pj have finished – resources become
available.

 Request R -> use R -> release R.


 When the process completes execution , all the resources held
by it are released to the system.

P1 uses 2 printers → after use → release it to system

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Basic Facts
 If a system is in safe state  no deadlock.

 If a system is in unsafe state  possibility of deadlock.

 Avoidance  ensure that a system will never enter an


unsafe state.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Resource-Allocation Graph Algorithm
• This algorithm is used only if we have one instance of a
resource type. In addition to the request edge and the
assignment edge a new edge called claim edge is used.

• Before the requested resource is granted, checks if the


system leads to deadlock

• Works in systems, when only one instance of resource is


present

• Resources must be claimed a priori in the system.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Resource-Allocation Graph Algorithm
 Claim edge Pi - - > Rj indicates that process Pj may request
resource Rj P i
Rj

 Claim edge converts to request edge when a process


requests a resource.
Pi
Rj
 Request edge converts into assignment edge , when
resource is allocated.
Pi
Rj
 When a resource is released by a process, assignment edge
reconverts to a claim edge.
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Resource-Allocation Graph For Deadlock Avoidance

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Unsafe State In Resource-Allocation Graph

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Banker’s Algorithm / Deadlock Avoidance
algorithm
 This algorithm is applicable to the system with multiple instances of
each resource types, but this is less efficient then the resource
allocation graph algorithm.
 The Banker's Algorithm gets its name because it is a method that
bankers could use to assure that when they lend out resources they will
still be able to satisfy all their clients. ( A banker won't loan out a little
money to start building a house unless they are assured that they will
later be able to loan out the rest of the money to finish the house. )
 When a process starts up, it must state in advance the maximum
allocation of resources it may request, up to the amount available on
the system.
 When a request is made, the scheduler determines whether granting
the request would leave the system in a safe state. If not, then the
process must wait until the request can be granted safely.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Data Structures for the Banker’s Algorithm
 Several data structures are used to implement the banker’s algorithm.
 Let ‘n’ be the number of processes in the system
 and ‘m’ be the number of resources types.
 We need the following data structures:
 Available:-A vector of length m indicates the number of available resources. If
Available[i]=k, then k instances of resource type Rj is available.
 Max:-An n*m matrix defines the maximum demand of each process if Max[i][j]=k,
then Pi may request at most k instances of resource type Rj.
 Allocation:-An n*m matrix defines the number of resources of each type currently
allocated to each process. If Allocation[i][j]=k, then Pi is currently k instances of
resource type Rj.
 Need:-An n*m matrix indicates the remaining resources need of each process. If
Need[i][j]=k, then Pi may need k more instances of resource type Rj to compute
its task.
 So Need[i][j]=Max[i][j] – Allocation[i
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Data Structures for the Banker’s Algorithm

Let n = number of processes, and m = number of resources types.

 Available: Vector of length m.


If available [j] = k, there are k instances of resource type Rj available.
 Max: n x m matrix.
If Max [i,j] = k, then process Pi may request at most k instances of resource
type Rj.
 Allocation: n x m matrix.
If Allocation[i,j] = k then Pi is currently allocated k instances of Rj.
 Need: n x m matrix.
If Need[i,j] = k, then Pi may need k more instances of Rj to complete its task.
Need [i,j] = Max[i,j] – Allocation [i,j].
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
 The Bankers Algorithm consists of the following two algorithms

 Request-Resource Algorithm
 Safety Algorithm

 Resource- Request Algorithm


 Whenever a process makes a request of the resources then this algorithm checks that if the
resource can be allocated or not.
 It includes three steps:
 The algorithm checks that if the request made is valid or not. A request is valid if the
number of requested resources of each resource type is less than the Need( which was
declared previously by the process ). If it is a valid request then step 2 is executed else
aborted.
 Here, the algorithm checks that if the number of requested instances of each resource is
less than the available resources. If not then the process has to wait until sufficient
resources are available else go to step 3.
 Now, the algorithm assumes that the resources have been allocated and modifies the
data structure accordingly.
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
 Available = Available - Request(i)
 Allocation(i) = Allocation(i) + Request(i)
 Need(i) = Need(i) - Request(i)

 After the allocation of resources, the new state formed may or may not be
a safe state. So, the safety algorithm is applied to check whether the
resulting state is a safe state or not.
 Safe state: A safe state is a state in which all the processes can be
executed in some arbitrary order with the available resources such that no
deadlock occurs.
 If it is a safe state, then the requested resources are allocated to the
process in actual.
 If the resulting state is an unsafe state then it rollbacks to the previous state
and the process is asked to wait longer.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Need [i,j] = Max[i,j] – Allocation [i,j].

Proce Allocated Maximum Available Need


ss Resources Required

A B C A B C A B C A B C

P0 0 1 0 7 5 3 3 3 2 7 4 3

P1 2 0 0 3 2 2 1 2 2

P2 3 0 2 9 0 2 6 0 0

P3 2 1 1 2 2 2 0 1 1

P4 0 0 2 4 3 3 4 3 1

A = 3+7=10 printers
Available
B= 3+2 = 5 CD-drive need
C= 2 +5 =7 CPU
Maximum
required by P1

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


n- processes
Safety Algorithm m- resources

1. Let Work and Finish be vectors of length m and n,


respectively. Initialize:
Work = Available //no. of resources available.
Finish [ i ] = false for i - 1,3, …, n. // not executed
2. Find an i such that both:
(a) Finish [i] = false
(b) Needi  Work
If no such i exists, go to step 4.
3. Work = Work + Allocationi //work= work + P0 allocated
Finish[i] = true
go to step 2.
4. If Finish [i] == true for all i, then the system is in a safe state.
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Proce Allocated Maximum Need
ss Resources Required

A B C A B C A B C

P0 0 1 0 7 5 3
7 4 3
P1 2 0 0 3 2 2
1 2 2
P2 3 0 2 9 0 2
6 0 0
2 1 1 2 2 2
P3 0 1 1
P4 0 0 2 4 3 3 4 3 1

Given : Available resources [ 3,3,2]. Check whether the system is in safe state.

Safety Algorithm

1) Work = available = [3, 3, 2], Finish[P0 – P4] = False.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Proc Allocated Maximum Need
ess Resources Required
A B C A B C A B C
2) Find an i such that both: P0 0 1 0 7 5 3 7 4 3

(a) Finish [i] = false P1 2 0 0 3 2 2 1 2 2


P2 3 0 2 9 0 2 6 0 0
(b) Needi  Work P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
For i = P0 Need (P0)  Work
[ 7, 4, 3]  [ 3, 3, 2] ; not true. Hence P0 cannot execute.

For i = P1 Need (P1)  Work


[ 1, 2, 2]  [ 3, 3, 2] ; true. Hence P1 executes…...
Work = Work + Allocation(P1)
Work = [3, 3, 2] + [2, 0, 0] = [5, 3, 2]
Finish[P1] = true.

For i = P2 Need (P2)  Work


[ 6, 0, 0]  [ 5, 3, 2] ; not true. Hence P2 cannot execute.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Proc Allocated Maximum Need
ess Resources Required
A B C A B C A B C
For i = P3 Need (P3)  Work P0 0 1 0 7 5 3 7 4 3
P1
[ 0, 1, 1]  [ 5, 3, 2] ; true. Hence P3 executes…... 2 0 0 3 2 2 1 2 2
P2 3 0 2 9 0 2 6 0 0
Work = Work + Allocation(P3) P3 2 1 1 2 2 2 0 1 1
Work = [5, 3, 2] + [2, 1, 1] = [7, 4, 3] P4 0 0 2 4 3 3 4 3 1
Finish[P3] = true.

For i = P4 Need (P4)  Work


[ 4, 3, 1]  [ 7, 4, 3] ; true. Hence P4 executes…...
Work = Work + Allocation(P4)
Work = [7, 4, 3] + [0, 0, 2] = [7, 4, 5]
Finish[P4] = true.

For i = P0 Need (P0)  Work


[ 7, 4, 3]  [ 7, 4, 5] ; true. Hence P0 executes…...
Work = Work + Allocation(P0)
Work = [7, 4, 5] + [0, 1, 0] = [7, 5, 5]
Finish[P0] = true.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Proc Allocated Maximum Need
ess Resources Required
A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3
P1 2 0 0 3 2 2 1 2 2
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1

For i = P2 Need (P2)  Work


[ 6, 0, 0]  [ 7, 5, 5] ; true. Hence P2 executes…...
Work = Work + Allocation(P2)
Work = [7, 5, 5] + [3, 0, 2] = [10, 5, 7]
Finish[P2] = true.

Finish[P0 – P4] = True, ie. All the processes of the system can be executed. So the
system is in safe state and the safe sequence is <P1, P3, P4, P0, P2>

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Proce Allocated Maximum Need
ss Resources Required

A B C A B C A B C

P0 0 0 2 0 0 4
0 0 2
P1 1 0 0 2 0 1
1 0 1
P2 1 3 5 1 3 7
0 0 2
6 3 2 8 4 2
P3 2 1 0
P4 1 4 3 1 5 7 0 1 4

Given : Available resources [ 1 , 0, 2]. Check whether the system is in safe state.
Solution :

Total resources – A=1+ 9 =10


B= 0+ 10 = 10
C=2+ 12 = 14
Safety Algorithm

1) Work = available = [1, 0, 2], Finish[P0 – P4] = False.


DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Pro Allocated Maximum Need
ces Resources Required
s A B C A B C A B C
2) Find an i such that both: P0 0 0 2 0 0 4 0 0 2
(a) Finish [i] = false P1 1 0 0 2 0 1 1 0 1

(b) Needi  Work P2 1 3 5 1 3 7 0 0 2


P3 6 3 2 8 4 2 2 1 0
P4 1 4 3 1 5 7 0 1 4
For i = P0 Need (P0)  Work
[ 0, 0, 2]  [ 1, 0, 2] ; true. Hence P0 executes…...
Work = Work + Allocation(P0)
Work = [1, 0, 2] + [0, 0, 2] = [1, 0, 4]
Finish[P0] = true.

For i = P1 Need (P1)  Work


[ 1, 0, 1]  [ 1, 0, 4] ; true. Hence P1 executes…...
Work = Work + Allocation(P1)
Work = [1, 0, 4] + [1, 0, 0] = [2, 0, 4]
Finish[P1] = true.

For i = P2 Need (P2)  Work


[ 0, 0, 2]  [ 2, 0, 4] ; true. Hence P2 execute….
Work = Work + Allocation(P2)
Work = [2, 0, 4] + [1, 3, 5] = [3, 3, 9]
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Finish[P2] = true.
Pro Allocated Maximum Need
ces Resources Required
s A B C A B C A B C
For i = P3 Need (P3)  Work P0 0 0 2 0 0 4 0 0 2
[ 2, 1, 0]  [ 3, 3, 9] ; true. Hence P3 executes…... P1 1 0 0 2 0 1 1 0 1
P2 1 3 5 1 3 7 0 0 2
Work = Work + Allocation(P3) 6 3 2 8 4 2 2 1 0
P3
Work = [3, 3, 9] + [6, 3, 2] = [9, 6, 11] P4 1 4 3 1 5 7 0 1 4
Finish[P3] = true.

For i = P4 Need (P4)  Work


[ 0, 1, 4]  [ 9, 6, 11] ; true. Hence P4 executes…...
Work = Work + Allocation(P4)
Work = [9, 6, 11] + [1, 4, 3] = [10, 10, 14]
Finish[P4] = true.

Finish[P0 – P4] = True, ie. All the processes of the system can be executed. So the
system is in safe state and the safe sequence is <P0, P1,P2, P3, P4>

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Apply Banker’s algorithm to answer the following questions:
i) What is the content of need matrix?
ii) Is the system in a safe state?

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Resource-Request Algorithm
Request = request vector for process Pi.
If Request i [j] = k then process Pi wants k instances of resource type Rj.
Pi [a,b,c]
1. If Request i  Needi go to step 2. else, error, since process has
exceeded its maximum claim.
2. If Request i  Available, go to step 3. else Pi must wait, since
resources are not available.
3. Pretend to allocate requested resources to Pi by modifying the
state as follows:
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
If safe  the resources are allocated to Pi.
If unsafe  Pi must wait, and the old resource-allocation state is restored
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
Available = Available - Requesti;

1000

Available
need

10,000 max -5,000


allocated - 1000
need – 4000
request - 3000

3000<= need
3000<available

Allocation = 1000+3000 = 4000


Available = 10000 – 3000= 7000
Need = 4000
DR. RAMESH -3000
BABU , DEPT =1000
OF AI & ML , RNSIT
Proc
Proc Allocated
Allocated Maximum
Maximum Need
ess
ess Resources
Resources Required
Required
Request for Resource – AA BB CC AA BB CC AA BB CC
PP00 00 11 00 77 55 33 77 44 33

P1 [ 1, 0, 2] PP11 32 00 20 33 22 22 01 22 02
PP22 33 00 22 99 00 22 66 00 00
PP33 22 11 11 22 22 22 00 11 11
1. Request (P1) Need (P1) PP 44 00 00 22 44 33 33 44 33 11
[1 , 0, 2]  [1, 2, 2]

Requested resource is needed.

2. Request(P1)  Available
[1, 0, 2]  [3,3,2]
Requested resource is available.
3. Pretend to allocate requested resources to P(1) by modifying the state as follows:
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;

Available = [3, 3, 2] - [1, 0, 2] = [2, 3, 0]


Allocation (P1) = [2, 0, 0] + [1,0, 2] = [3, 0, 2]
Need(P1) = [1, 2, 2] - [1, 0, 2] = [0, 2, 0]

With these changes in the system, check whether the system is in safe state – using
safety algorithm
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Process
request for resource

Resource Request
Algorithm , followed by If resource is allocated,
Safety Algorithm. is system in safe state
No
Yes

Actual allocation of Actual allocation of


resource done. resource not done.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Proce Allocated Maximum Need
ss Resources Required

A B C A B C A B C

P0 0 1 0 7 5 3
7 4 3
P1 3 0 2 3 2 2
0 2 0
P2 3 0 2 9 0 2
6 0 0
2 1 1 2 2 2
P3 0 1 1
P4 0 0 2 4 3 3 4 3 1

Given : Available resources [ 2, 3, 0]. Check whether the system is in safe state.
Total resources – A =2 + 8 = 10
B=3+2=5
C=0+7=7

Safety Algorithm

1) Work = available = [2, 3, 0], Finish[P0 – P4] = False.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Proc Allocated Maximum Need
ess Resources Required
A B C A B C A B C
2) Find an i such that both: P0 0 1 0 7 5 3 7 4 3
(a) Finish [i] = false P1 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0
(b) Needi  Work 2 1 1 2 2 2 0 1 1
P3
P4 0 0 2 4 3 3 4 3 1
For i = P0 Need (P0)  Work
[ 7, 4, 3]  [ 2, 3, 0] ; not true. Hence P0 cannot execute.

For i = P1 Need (P1)  Work


[ 0, 2, 0]  [ 2, 3, 0] ; true. Hence P1 executes…...
Work = Work + Allocation(P1)
Work = [2, 3, 0] + [3, 0, 2] = [5, 3, 2]
Finish[P1] = true.

For i = P2 Need (P2)  Work


[ 6, 0, 0]  [ 5, 3, 2] ; not true. Hence P2 cannot execute.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Proc Allocated Maximum Need
ess Resources Required
A B C A B C A B C
For i = P3 Need (P3)  Work P0 0 1 0 7 5 3 7 4 3
P1
[ 0, 1, 1]  [ 5, 3, 2] ; true. Hence P3 executes…... 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0
Work = Work + Allocation(P3) P3 2 1 1 2 2 2 0 1 1
Work = [5, 3, 2] + [2, 1, 1] = [7, 4, 3] P4 0 0 2 4 3 3 4 3 1
Finish[P3] = true.

For i = P4 Need (P4)  Work


[ 4, 3, 1]  [ 7, 4, 3] ; true. Hence P4 executes…...
Work = Work + Allocation(P4)
Work = [7, 4, 3] + [0, 0, 2] = [7, 4, 5]
Finish[P4] = true.

For i = P0 Need (P0)  Work


[ 7, 4, 3]  [ 7, 4, 5] ; true. Hence P0 executes…...
Work = Work + Allocation(P0)
Work = [7, 4, 5] + [0, 1, 0] = [7, 5, 5]
Finish[P0] = true.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Proc Allocated Maximum Need
ess Resources Required
A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3
P1 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1

For i = P2 Need (P2)  Work


[ 6, 0, 0]  [ 7, 5, 5] ; true. Hence P2 executes…...
Work = Work + Allocation(P2)
Work = [7, 5, 5] + [3, 0, 2] = [10, 5, 7]
Finish[P2] = true.

Finish[P0 – P4] = True, ie. All the processes of the system can be executed. So the
system is in safe state and the safe sequence is <P1, P3, P4, P0, P2>.

The actual allocation of resources can be done.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


1) Using Banker's algorithm determine whether the system is in a safe state.

If a request from process P2 arrives for (0 0 2), can the request be granted
immediately?

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Proce Allocated Maximum Need
ss Resources Required

A B C A B C A B C

P0 0 0 2 0 0 4
0 0 2
P1 1 0 0 2 0 1
1 0 1
P2 1 3 5 1 3 7
0 0 2
6 3 2 8 4 2
P3 2 1 0
P4 1 4 3 1 5 7 0 1 4

Given : Available resources [ 1 , 0, 2]. Check whether the system is in safe state.
Solution :

Total resources – A=1+ 9 =10


B= 0+ 10 = 10
C=2+ 12 = 14
Safety Algorithm

1) Work = available = [3, 3, 2], Finish[P0 – P4] = False.


DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Pro Allocated Maximum Need
ces Resources Required
s A B C A B C A B C
2) Find an i such that both: P0 0 0 2 0 0 4 0 0 2
(a) Finish [i] = false P1 1 0 0 2 0 1 1 0 1

(b) Needi  Work P2 1 3 5 1 3 7 0 0 2


P3 6 3 2 8 4 2 2 1 0
P4 1 4 3 1 5 7 0 1 4
For i = P0 Need (P0)  Work
[ 0, 0, 2]  [ 1, 0, 2] ; true. Hence P0 executes…...
Work = Work + Allocation(P0)
Work = [1, 0, 2] + [0, 0, 2] = [1, 0, 4]
Finish[P0] = true.

For i = P1 Need (P1)  Work


[ 1, 0, 1]  [ 1, 0, 4] ; true. Hence P1 executes…...
Work = Work + Allocation(P1)
Work = [1, 0, 4] + [1, 0, 0] = [2, 0, 4]
Finish[P1] = true.

For i = P2 Need (P2)  Work


[ 0, 0, 2]  [ 2, 0, 4] ; true. Hence P2 execute….
Work = Work + Allocation(P2)
Work = [2, 0, 4] + [1, 3, 5] = [3, 3, 9]
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Finish[P2] = true.
Pro Allocated Maximum Need
ces Resources Required
s A B C A B C A B C
For i = P3 Need (P3)  Work P0 0 0 2 0 0 4 0 0 2
[ 2, 1, 0]  [ 3, 3, 9] ; true. Hence P3 executes…... P1 1 0 0 2 0 1 1 0 1
P2 1 3 5 1 3 7 0 0 2
Work = Work + Allocation(P3) 6 3 2 8 4 2 2 1 0
P3
Work = [3, 3, 9] + [6, 3, 2] = [9, 6, 11] P4 1 4 3 1 5 7 0 1 4
Finish[P3] = true.

For i = P4 Need (P4)  Work


[ 0, 1, 4]  [ 9, 6, 11] ; true. Hence P4 executes…...
Work = Work + Allocation(P4)
Work = [9, 6, 11] + [1, 4, 3] = [10, 10, 14]
Finish[P4] = true.

Finish[P0 – P4] = True, ie. All the processes of the system can be executed. So the
system is in safe state and the safe sequence is <P0, P1,P2, P3, P4>

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Pro Allocated Maximum Need
ces Resources Required
Request for Resource – s A B C A B C A B C
P0 0 0 2 0 0 4 0 0 2
P2 [ 0, 0, 2] P1 1 0 0 2 0 1 1 0 1
P2 1 3 5
7 1 3 7 0 0 2
0
P3 6 3 2 8 4 2 2 1 0
1. Request (P2) Need (P1) 1 4 3 1 5 7 0 1 4
P4
[0 , 0, 2]  [0, 0, 2]

Requested resource is needed.

2. Request(P2)  Available
[0, 0, 2]  [1,0,2]
Requested resource is available.
3. Pretend to allocate requested resources to P(2) by modifying the state as follows:
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;

Available = [1, 0, 2] - [0, 0, 2] = [1, 0, 0]


Allocation (P1) = [1, 3, 5] + [0,0, 2] = [1, 3, 7]
Need(P1) = [0, 0, 2] - [0, 0, 2] = [0, 0, 0]

With these changes in the system, check whether the system is in safe state – using
safety algorithm
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Pro Allocated Maximum Need
ces Resources Required
s A B C A B C A B C
Available resources [ 1 , 0, 0]. P0 0 0 2 0 0 4 0 0 2
Check whether the system is in safe state – P1 1 0 0 2 0 1 1 0 1
P2 1 3 7 1 3 7 0 0 0
if yes , actual allocation of resource can be done. 6 3 2 8 4 2 2 1 0
P3
P4 1 4 3 1 5 7 0 1 4
Solution :

Total resources – A=1+ 9 =10


B= 0+ 10 = 10
C=0+ 14 = 14
Safety Algorithm

1) Work = available = [1, 0, 0], Finish[P0 – P4] = False.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Pro Allocated Maximum Need
ces Resources Required
s A B C A B C A B C
2) Find an i such that both: P0 0 0 2 0 0 4 0 0 2
(a) Finish [i] = false P1 1 0 0 2 0 1 1 0 1

(b) Needi  Work P2 1 3 7 1 3 7 0 0 0


P3 6 3 2 8 4 2 2 1 0
P4 1 4 3 1 5 7 0 1 4
For i = P0 Need (P0)  Work
[ 0, 0, 2]  [ 1, 0, 0] ; false. Hence P0 cannot execute…...

For i = P1 Need (P1)  Work


[ 1, 0, 1]  [ 1, 0, 0] ; false. Hence P1 cannot execute…...

For i = P2 Need (P2)  Work


[ 0, 0, 0]  [ 1, 0, 0] ; true. Hence P2 executes….
Work = Work + Allocation(P2)
Work = [1, 0, 0] + [1, 3, 7] = [2, 3, 7]
Finish[P2] = true.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Pro Allocated Maximum Need
ces Resources Required
s A B C A B C A B C
For i = P3 Need (P3)  Work P0 0 0 2 0 0 4 0 0 2
[ 2, 1, 0]  [ 2, 3, 7] ; true. Hence P3 executes…... P1 1 0 0 2 0 1 1 0 1
P2 1 3 7 1 3 7 0 0 0
Work = Work + Allocation(P3) 6 3 2 8 4 2 2 1 0
P3
Work = [2, 3, 7] + [6, 3, 2] = [8, 6, 9] P4 1 4 3 1 5 7 0 1 4
Finish[P3] = true.

For i = P4 Need (P4)  Work


[ 0, 1, 4]  [ 8, 6, 9] ; true. Hence P4 executes…...
Work = Work + Allocation(P4)
Work = [8, 6, 9] + [1, 4, 3] = [9, 10, 12]
Finish[P4] = true.

For i = P0 Need (P0)  Work


[ 0, 0, 2]  [ 9, 10, 12] ; true. Hence P0 executes…...
Work = Work + Allocation(P0)
Work = [9, 10, 12] + [0, 0, 2] = [9, 10, 14]
Finish[P0] = true.
Finish[P0 – P4] = True, ie. All the processes of the system can be executed. So the
system is in safe state and the safe sequence is <P0, P1,P2, P3, P4>
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Pro Allocated Maximum Need
ces Resources Required
s A B C A B C A B C
For i = P1 Need (P1)  Work P0 0 0 2 0 0 4 0 0 2
[ 1, 0, 0]  [ 9, 10, 14] ; true. Hence P1 executes…...
P1 1 0 0 2 0 1 1 0 1
Work = Work + Allocation(P1) P2 1 3 7 1 3 7 0 0 0
Work = [9, 10, 14] + [1, 0, 0] = [10, 10, 14] P3 6 3 2 8 4 2 2 1 0
1 4 3 1 5 7 0 1 4
Finish[P1] = true. P4

Finish[P0 – P4] = True, ie. All the processes of the system can be executed. So the
system is in safe state and the safe sequence is <P2, P3, P4, P0, P1>

As system is in safe state even after change, the actual allocation of resource can be
done.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Deadlock Detection
 Allow system to enter deadlock state

 Detection algorithm

 Recovery scheme

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Single Instance of Each Resource Type

Maintain wait-for graph


Nodes are processes.
Pi → Pj if Pi is waiting for Pj.

Periodically invoke an algorithm that searches


for a cycle in the graph.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Resource-Allocation Graph and Wait-for Graph

P5

P1 P2 P3

P4

Resource-Allocation Graph Corresponding wait-for graph

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Deadlock Detection -
Several Instances of a Resource Type

 Available: A vector of length m indicates the number of


available resources of each type.

 Allocation: An n x m matrix defines the number of resources


of each type currently allocated to each process.

 Request: An n x m matrix indicates the current request of


each process. If Request [i j] = k, then process Pi is requesting
k more instances of resource type. Rj.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Detection Algorithm
1. Let Work and Finish be vectors of length m and n, respectively
Initialize:
(a) Work = Available
(b) For i = 1,2, …, n, if Allocationi  0, then
Finish[i] = false; otherwise, Finish[i] = true.
2. Find an index i such that both:
(a) Finish[i] == false
(b) Request i  Work
If no such i exists, go to step 4.
3. Work = Work + Allocationi
Finish[i] = true
go to step 2.

4. If Finish[i] == false, for some i, 1  i  n, then the system is in deadlock


state. Moreover, if Finish[i] == false, then Pi is deadlocked.
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Example of Detection Algorithm
Consider the following snapshot of a system, at an instance of time (t0):
Allocation Request Available
ABC ABC ABC
P0 010 000 000
P1 200 202
P2 303 001
P3 211 100
P4 002 002

If the requested resources are allocated, detect whether system is in


deadlock or safe state.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Pro Allocated Request Available
Deadlock detection Algorithm: ces
s
Resources
A B C A B C A B C

1) Work = [0, 0, 0] & Finish[P0 – P4] = false P0 0 1 0 0 0 0 0 0 0


P1 2 0 0 2 0 2
2) Find an index i such that both: P2 3 0 3 0 0 1
(a) Finish[i] == false P3 2 1 1 1 0 0
0 0 2 0 0 2
(b)Request i  Work P4

For i = P0 Request(P0)  Work


[ 0, 0, 0]  [ 0, 0, 0] ; true. Hence P0 executes….
Work = Work + Allocation(P0)
Work = [0, 0, 0] + [0, 1, 0] = [0, 1, 0]
Finish[P0] = true.

For i = P1 Request(P1)  Work


[ 2, 0, 2]  [ 0, 1, 0] ; false. Hence P1 cannot execute…...

For i = P2 Need (P2)  Work


[ 0, 0, 1]  [ 0, 1, 0] ; false. Hence P2 cannot execute…...

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Pro Allocated Request Available
For i = P3 Request(P3)  Work ces Resources
s A B C A B C A B C
[1, 0, 0]  [ 0, 1, 0] ; false. Hence P3 cannotP0execute…...
0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 3 0 0 1
P3 2 1 1 1 0 0
For i = P4 Request(P4)  Work P4 0 0 2 0 0 2

[ 0, 0, 2]  [ 0, 1, 0] ; false. Hence P4 cannot execute…...

Here, the process P0 has executed and P1,P2,P3 & P4 are cannot
execute, hence P1,P2,P3 & P4 processes are in deadlock.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Recovery from Deadlock
 By Process Termination

 By Resource Preemption

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Recovery from Deadlock
 Process Termination

 • Two basic approaches to terminate processes:

 Abort all deadlocked processes. This method breaks the deadlock cycle by
terminating all the processes involved in deadlock. But, at a great expense, the
deadlocked processes may have computed for a long time, and the results of
these partial computations must be discarded and probably will have to be
recomputed later.

 Abort one process at a time until the deadlock cycle is eliminated. This method
incurs considerable overhead, since, after each process is aborted, a deadlock-
detection algorithm must be invoked to determine whether any processes are still
deadlocked.
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Recovery from Deadlock
 Abort one process at a time until the deadlock cycle is eliminated.

•In this case there are many factors that can go into deciding which processes to terminate
next:

1. Process priorities.

2. How long the process has been running, and how close it is to finishing.

3. How many and what type of resources is the process holding. ( Are they easy to preempt
and restore? )

4. How many more resources does the process need to complete.

5. How many processes will need to be terminated

6. Whether the process is interactive or batch.

7. ( Whether or not the process has made non-restorable changes to any resource. )
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Recovery from Deadlock: Resource Preemption

When preempting resources to relieve deadlock, there are three important issues to
be addressed:
 1. Selecting a victim - Deciding which resources to preempt from which
processes involves many of the same decision criteria outlined above.

 2. Rollback - Ideally one would like to roll back a preempted process to a safe
state prior to the point at which that resource was originally allocated to the
process. Unfortunately it can be difficult or impossible to determine what such a
safe state is, and so the only safe rollback is to roll back all the way back to the
beginning. ( i.e. abort the process and make it start over. )

 3. Starvation – There are chances that the same resource is picked from a victim
process, every time the deadlock occurs and this continues. This is starvation. A
count can be kept on number of rollback of a process and the process has to be a
victim for finite number of times only .

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Recovery from Deadlock: Resource Preemption

 Selecting a victim process.

 Rollback – return to some safe state, restart process for that


state.

 Starvation – same process may always be picked as victim.


P2

Safe
state
Roll back

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT Deadlock


State
Memory Management
 Background
 Swapping
 Contiguous Allocation
 Paging
 Structure of Page Table
 Segmentation

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Background

 Program must be brought into memory , to execute it.

 Each process has its own memory space

 Protection of memory is required, correct instruction should be accessed

 User programs go through several steps before being executed

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Address Binding

 • User programs typically refer to memory addresses with symbolic names such
as "i", "count", and "average Temperature". These symbolic names must be mapped
or bound to physical memory addresses, which typically occurs in several stages:
 o Compile Time - If it is known at compile time where a program will reside in
physical memory, then absolute code can be generated by the compiler,
containing actual physical addresses. However if the load address changes at
some later time, then the program will have to be recompiled.
 o Load Time - If the location at which a program will be loaded is not known at
compile time, then the compiler must generate relocatable code, which references
addresses relative to the start of the program. If that starting address changes, then
the program must be reloaded but not recompiled.
 o Execution Time - If a program can be moved around in memory during the
course of its execution, then binding must be delayed until execution time

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Multistep Processing of a User Program

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Logical vs. Physical Address Space

 Logical address – generated by the CPU; also referred to as virtual address

 Physical address – address seen by the memory unit,(location in memory)

 Logical address space

 Physical address space

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Logical vs. Physical Address Space
 Logical address – generated by the CPU; also referred to as virtual address

 Physical address – address seen by the memory unit,(location in memory)

 Logical Versus Physical Address Space


 • The address generated by the CPU is a logical address, whereas the memory address where
programs are actually stored is a physical address.

 • The set of all logical addresses used by a program composes the logical address space, and the
set of all corresponding physical addresses composes the physical address space.

 • The run time mapping of logical to physical addresses is handled by the memory-management
unit, MMU.

 o The MMU can take on many forms. One of the simplest is a modification of the base-register
scheme described earlier.

 o The base register is now termed a relocation register, whose value is added to every memory
request at the hardware level.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Logical vs. Physical Address Space

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Memory-Management Unit (MMU)
 Hardware device that maps virtual to physical address

 In MMU scheme, the value in the relocation register is added to


every logical address.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Dynamic relocation using a relocation register

P2

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


HW address protection with base and limit registers

Base + 0
Base OS
limit
(14000)
(14500)
14000
14346 P2
CPU > < 14499

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Swapping
 A process can be swapped temporarily out of memory to a backing
store, and then brought back into memory for continued execution

 Backing store – fast disk large enough to accommodate copies of


all memory images for all users; must provide direct access to these
memory images

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Schematic View of Swapping

Roll Out

Roll In

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Contiguous Allocation
 Two partitions of Main memory :
 Resident operating system, usually held in low memory with interrupt
vector
 User processes then held in high memory

 Fixed-partition allocation
 After the loading of OS, memory is divided into equal fixed size blocks
 single continuous block for each process.

 Multiple partition allocation


 After the loading of OS, memory is assigned to process, as per the
request.
DR.single continuous block for each process.
RAMESH BABU , DEPT OF AI & ML , RNSIT
Fixed-partition allocation

OS

process 5  Only fixed number of programs.


 Process 6, process 7….. In waiting
process 8

2
process 3

process 4

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Contiguous Allocation (Cont.)
 Multiple-partition allocation
 Hole – block of available memory;
 after loading of OS a large hole is available.
 When a process arrives, it is allocated memory from a hole large enough to
accommodate it
 Operating system maintains information about:
a) allocated partitions b) free partitions (hole)

OS OS OS OS OS

process 5 process 5 process 5 process 5 process 5


process 9 process 9 process 9
process 8 process 10 process 10

process 2 process 2 process 2 process 2 process 2


DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Process 8 completes New Process P9 Another New Process Process 5 completes
execution enters memory P10 enters memory execution
OS

process 9
process 10

process 4

process 2

process 5

process 12

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Dynamic Storage-Allocation Problem
How to select a memory block of size n from a list of free holes

 First-fit: Allocate the first hole that is big enough


 Best-fit: Allocate the smallest hole that is big enough; must search entire list,
unless ordered by size. Produces the smallest leftover hole.
 Worst-fit: Allocate the largest hole; must also search entire list. Produces the
largest leftover hole.

Eg -Suppose free spaces available list - 70KB , 500KB,330KB, 250KB, 720KB.


A file of size 240KB have to be placed.
500KB
First Fit –
P(240KB)
Hole (250KB)
Best Fit – 250KB
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT

Worst Fit – 720KB


Given memory partitions of 100K,500K,200K,300K and 600K. How each of the
first fit, best fit and worst fit algorithms place processes of 212KB,417KB,112 KB
and 426 KB size. Which algorithm makes efficient use of memory?
First fit File of 426KB cannot Best fit All files are

///Program5/// ///Program5///be placed ///Program5///placed

100K 100K 100K

///Program6/// ///Program6/// ///Program6///


500K /// / 212KB /// 500K /// / 417KB /// 500K
/// / 112KB /// 288K 83K
176K
///Program8/// ///Program8/// ///Program8///
/// / 112KB /// 200K
200K 200K
88K
///Program9/// ///Program9/// ///Program9///
300K 300K /// / 212KB /// 300K
88K
//Program18//// //Program18//// //Program18////

/// / 417KB /// /// / 426KB ///


600K
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
600K 600K
183K 174K
Given memory partitions of 100K,500K,200K,300K and 600K. How each of the
first fit, best fit and worst fit algorithms place processes of 212KB,417KB,112 KB
and 426 KB size. Which algorithm makes efficient use of memory?

Worst fit File of 426KB cannot


be placed
///Program5/// ///Program5///

100K 100K

///Program6/// ///Program6///
500K /// / 417KB /// 500K
83K
Conclusion:
///Program8/// ///Program8///
200K 200K Using First fit and worst fit – the file
of 426KB cannot be stored,
///Program9/// ///Program9/// whereas in BEST fit all files can be
300K 300K placed.

Hence Best fit algorithm makes


//Program18//// //Program18////
600K efficient use of memory
/// / 212KB ///
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
600K /// / 112KB /// 388K
276K
Fragmentation
 Very small unusable space in memory.
 External Fragmentation – Unusable spaces between the process
 total memory space may exists to satisfy a request, but it is not contiguous

 Internal Fragmentation – Unusable space within a process (space not required


by process)
 In fixed allocation - allocated memory may be slightly larger than requested memory;
this memory is within a process, but not being used

Ways to overcome external fragmentation -


 Reduce external fragmentation by compaction
 Shuffle memory contents to place all free memory together in one large block
 Paging & Segmentation

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Paging
 Processes are non contiguously stored.
 Process is allocated physical memory whenever required.
 Divide physical memory into fixed-sized blocks called frames (size is
power of 2, between 512 bytes and 8192 bytes)
 Divide logical memory into blocks of same size called pages.
 Keep track of all free frames

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Each block is of 256B

Pages
Frames
Page 0
frame 0 OS
Page 1
frame 1
Page 2 P1 – 1000B
frame 2 Page 0 -256B
Page 3
frame 3 Page 1 -256B
Logical Address Space
frame 4 Page 2 -256B

frame 5
Page3 – 232 B

Frame 6
Primary Memory/ Secondary Memory
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Physical Address
Space
Pages
Frames
Page 0
frame 0
Page 1
frame 1
Page 2
frame 2
Page 3
frame 3

frame 4

frame 5

Frame 6
Logical Adress Space Secoondary Memory
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT Primary Memory/
Physical Adress
Space
Each block is of 256B
Free frame list
1
Pages 3
5
Frames
4
Page 0
frame 0 OS
Page 1
frame 1 Page 0
Page 2 P1 – 1000B
frame 2 Page 0 -256B
Page 3
frame 3 Page 1 Page 1 -256B
Logical Address Space
frame 4 Page3 Page 2 -256B

frame 5 Page 2 Page3 – 232 B

Frame 6
Primary Memory/ Secondary Memory
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Physical Address
Space
Pages
Frames
Page 0
frame 0
Page 1
Page no Frame no
frame 1 Page 0
Page 2 0 1
1 3 frame 2
Page 3
2 5
frame 3 Page 1
3 4
Logical Address frame 4
Space Page Table of P1 Page 3

frame 5 Page 2

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Primary Memory/
Physical Address
Space
Paging Example

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Paging Example

Frame 0

Frame 1

Frame 2

Frame 3

Frame 4

Frame 5

Frame 6

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


 CPU generates address to the logical address space
 This address is converted to physical address using MMU.
 Here MMU is page table, consisting of page no. and frame no.
 To run a program of size n pages, need to find n free frames
and load program

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Free Frames

Before allocation After allocation

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


• Frame table maintained by OS – frames in primary memory

• It maintains information about allocated frames, free frames, total frames

available

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Address Translation Scheme
 Address generated by CPU is divided into:

 Page number (p) – used as an index into a page table which contains base
address of each page in physical memory

 Page offset (d) – combined with base address to define the physical memory
address that is sent to the memory unit

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Address Translation Architecture

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Pages
Frames
Page 0 2n
2 3 5 3
frame 0
Page 1 2m
Page no Frame no
frame 1 Page 0
Page 2 0 1
1 3 frame 2
Page 3
2 5
frame 3 Page 1
3 -
frame 4
Page Table
frame 5 Page 2

Primary Memory/
Logical Address
Physical Address
Space DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Space
Paging Example

Frame 0

Frame 1

Frame 2

Frame 3

Frame 4

Frame 5

Frame 6

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Logical Address & Physical Address -
If logical address space is 2m B, then logical address generated is of size m bits.
If page size is 2n B, then page offset is represented by n bits.
m bits
p – page number n – page offset

m-n bits n bits

If logical address space is 32 KB and size of page is 16B, then logical address
generated is of size ______ bits.
Given : logical address space – 32 KB = 25 x 210 B = 215 B (2m = 215) =>m=15
page – 16 B = 24 B (2n = 24) =>n=4

hence, no. of bits in logical address is 15 bits, page number is 11 bits and offset is 4
bits.
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Hardware support for page table -
 Different methods of storing page table
 Set of dedicated registers
 Use of PTBR ( Page Table Base Register)
 Using Translation Look-aside Buffer (TLB) / associative memory

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


PTBR -
 A register in CPU, points to PT of process in memory

CPU
P1 -Page 0
PTBR

P1 - Page 1

PT of P1

P1 - Page 2

 In this scheme, every instruction access, memory is accessed twice.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Associative Memory
 The two memory access problem can be solved by the use of a special fast-
lookup hardware cache called associative memory or translation look-aside
buffers (TLBs)
 Associative memory – parallel search
Page no. Frame no.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Paging Hardware With TLB

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


from physical memory
Effective Access Time
 Hit ratio – percentage of times that a page number is found in the associative
registers.
 Hit ratio = 
 TLB access time – ta
 Memory access time – tm
 Instruction access time after TLB hit is ta+tm
 Instruction access time after TLB miss is ta+2tm
 Effective Access Time (EAT) =[Access time for TLB hit x hit ratio]+
[Access time for TLB miss x miss ratio]

EAT = (ta+tm)  + (ta +2tm)(1 – )

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Memory Protection
 Memory protection implemented by associating protection bit with each
frame

 Valid-invalid bit attached to each entry in the page table:


 “valid” indicates that the page is in memory and is thus a legal page
 “invalid” indicates that the page is not in memory.

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Valid (v) or Invalid (i) Bit In A Page Table

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Shared Pages
 Shared code
 One copy of read-only (reentrant) code shared among processes (i.e., text
editors, compilers etc. only one copy of these files need to be maintained).
 Shared code must appear in same location in the logical address space of
all processes

 Private code and data of a process


 Each process keeps a separate copy of the code and data

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Shared Pages Example

Page 0

Page 1

Page2

Page 3

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Shared Pages Example

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Page Table Structure
 Hierarchical Paging

 Hashed Page Tables

 Inverted Page Tables

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Hierarchical Page Tables
 Break up the page table into multiple page tables

 A simple technique is a two-level page table

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Two-Level Paging Example
 A logical address is divided into:
 a page number
 a page offset

 The page number is further divided into:


 page number
 page offset
 Thus, a logical address is as follows:
page number page offset
pi p2 d

10 10 12

where pi is an index into the outer page table, and p2 is the displacement within the
page of the outer page table
DR. RAMESH BABU , DEPT OF AI & ML , RNSIT
Two-Level Page-Table Scheme

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Address-Translation Scheme

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Hashed Page Tables

 The page number is passed through a hash function to get a hashd page
table location.
 This page table contains a chain of elements hashing to the same location.

 page numbers are compared in this chain searching for a match. If a match is
found, the corresponding physical frame is extracted.

Hash
function

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Hashed Page Table

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Inverted Page Table
 One entry for each real page of memory (frame)
 Decreases memory needed to store each page table, but increases time
needed to search the table when a page reference occurs
 Use hash table to limit the search to one — or at most a few — page-table
entries

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Inverted Page Table Architecture

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Segmentation
 Memory-management scheme that supports user view of
memory
 A program is a collection of segments. A segment is a logical
unit such as:
main program,
function,
method,
object,
local variables, global variables,
common block,
stack,
symbol table, arrays
library files

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


User’s View of a Program

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Logical View of Segmentation
1

4
1

3 2
4

user space physical memory space

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Segmentation Architecture
 Logical address consists of a two tuple:
<segment-number, offset>,
 Segment table –
 base – contains the starting physical address where the segments reside in
memory
 limit – specifies the length of the segment

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Address Translation Architecture

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT


Example of Segmentation

DR. RAMESH BABU , DEPT OF AI & ML , RNSIT

You might also like