Module 3 Pptos
Module 3 Pptos
Module III
Deadlocks & Memory
Management
Deadlocks -
System Model
Deadlock Characterization
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
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.
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.
Example
System has 2 tape drives.
P1 and P2 each hold one tape drive and each needs another one.
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
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
R1 R3
P1 P2 P3
R2 R4
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.)
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.
Request-Resource Algorithm
Safety Algorithm
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.
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
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
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>
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 :
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>
1000
Available
need
3000<= need
3000<available
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]
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;
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
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
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>.
If a request from process P2 arrives for (0 0 2), can the request be granted
immediately?
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 :
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>
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;
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 :
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.
Detection algorithm
Recovery scheme
P5
P1 P2 P3
P4
Here, the process P0 has executed and P1,P2,P3 & P4 are cannot
execute, hence P1,P2,P3 & P4 processes are in deadlock.
By Resource Preemption
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? )
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 .
Safe
state
Roll back
• 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
• 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.
P2
Base + 0
Base OS
limit
(14000)
(14500)
14000
14346 P2
CPU > < 14499
Roll Out
Roll In
Fixed-partition allocation
After the loading of OS, memory is divided into equal fixed size blocks
single continuous block for each process.
OS
2
process 3
process 4
OS OS OS OS OS
process 9
process 10
process 4
process 2
process 5
process 12
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.
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 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
Frame 0
Frame 1
Frame 2
Frame 3
Frame 4
Frame 5
Frame 6
available
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
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
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
CPU
P1 -Page 0
PTBR
P1 - Page 1
PT of P1
P1 - Page 2
Page 0
Page 1
Page2
Page 3
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
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
4
1
3 2
4