Professional Documents
Culture Documents
And
Deadlock Avoidance
Operating System
Ibrar Afzal
IT Department HU Mansehra 1
Deadlock Prevention
Do not permit one of the four necessary conditions to occur Deadlock.
Mutual exclusion
Several processes cannot simultaneously share a single resource
Automatically holds for printers and other non-sharables.
Shared entities (read only files) don't require mutual exclusion (and aren’t
susceptible to deadlock.)
Prevention not possible, since a number of devices are basically non-sharable.
2
Deadlock Prevention
Do not permit one of the four necessary conditions to occur Deadlock.
Hold and wait:
Grant resources on “all or none” basis.
Process request be granted all of the resources it needs at once i.e. prior to execution
Disallow a process from requesting resources whenever it has previously allocated
resources.
Serious waste of resources
Resources are allotted at creation time but will be used after several hours.
All the required resources may not be available at once.
Indefinite postponement (starvation)
3
Deadlock Prevention
If a request is being made a process already owner of some resources then then immediately don’t
allocate new requested resources to a process until it not released the hold resources.
After release of resource ,already preempted resources will be added to the list of resources for which
the process is waiting
Process will be restarted only when it can get back its old resources, as well as the new ones that it is
requesting
4
Deadlock Prevention
5
Deadlock Prevention
6
Deadlock Avoidance
7
Deadlock Avoidance
8
Deadlock Avoidance
UNSAFE
SAFE
DEADLOCK
9
Deadlock Avoidance
10
Deadlock Avoidance
Safety Algorithm
A method used to determine if a particular state is safe. It's safe if there exists a
sequence of processes such that for all the processes, there’s a way to avoid deadlock:
11
Deadlock Avoidance
Safety Algorithm
1. Initialize work = available
Initialize finish[i] = false, for i = 1,2,3,..n
2. Find an i such that:
finish[i] == false and need[i] <= work
If no such i exists, go to step 4.
3. work = work + allocation[i]
finish[i] = true
goto step 2
4. if finish[i] == true for all i, then the system is in a safe state.
12
Deadlock Avoidance
Safety Algorithm
Do these examples:
Consider a system with: five processes, P0 P4, three resource types, A, B, C.
Type A has 10 instances, B has 5 instances, C has 7 instances.
At time T0 the following snapshot of the system is taken.
14
Deadlock Avoidance
Banker's Algorithm
Resource allocation system with multiple instances of each resource type
When a new process enters the system, it must declare the maximum
number of instances of each resource type that it may need.
This number may not exceed the total number of resources in the system.
When a user requests set of resources, the system must determine whether the
allocation of these resources will leave the system in a safe state.
If it will, the resources are allocated; otherwise, the process must wait until some
other process releases enough resources.
15
Deadlock Avoidance
Banker's Algorithm
16
Deadlock Recovery
Banker's Algorithm
17
Deadlock Avoidance
Banker's Algorithm
7: Deadlocks
18
Deadlock Avoidance
Banker's Algorithm
7: Deadlocks
19
Thanks
20