2. HOME PREVIOUS TOPIC
NEXT
PREVIOUS QUESTION PAPERS
FOR OS
CPP TUTORIALS
2
3. Recap
In the last class, you have learnt
Necessary conditions for arising Deadlocks
• Mutual exclusion
• Hold & wait
• No preemption
• Circular wait
3
4. Objectives
On completion of this class, you would be able to know:
• Various techniques to Prevent Deadlock
4
5. Deadlock Prevention
• Four necessary conditions must hold for a deadlock
to occur
• The four necessary conditions are:
– Mutual Exclusion
– Hold and wait
– No Preemption
– Circular wait
• Deadlock can be prevented if at least one of the
above conditions does not hold
5
6. Deadlock Prevention
Mutual Exclusion:
• The mutual exclusion condition must hold for non-
sharable resources
Ex : a printer cannot be simultaneously shared by
several processes
• A process never needs to wait for a sharable resource
• We cannot prevent deadlock by denying the mutual-
exclusion condition
• Because some resources are non sharable 6
7. Deadlock Prevention
Hold & wait
• To ensure Hold & wait never occur in the system
We must guarantee that whenever a process requests a
resource, it does not hold any other resources
First protocol:
• Each process must request & be allocated all its
resources before it begins execution
8. Deadlock Prevention
Example :
Consider a process that
• copies data from a tape drive to a disk file
• Sorts the disk file and Prints the results to a printer
• All above resources are requested at the
beginning of the process
• It will hold the printer for its entire execution
• Even though it needs the printer only at the end
9. Deadlock Prevention
Second Protocol:
• A process can request resources only when it has none
• i.e., If a process request any resources it must release
all its resources that are currently allocated to it
• Refer the example in Previous slide
9
10. Deadlock Prevention
Second Protocol :
• The process request initially only the tape drive & Disk
file
• It copies from tape drive to disk & then relives both
• The process request Disk file & Printer
• After copying the Disk file to the Printer
• Releases both resources and terminates
10
11. Deadlock Prevention
Protocols - Disadvantages:
• Resource utilization is low i.e., resources are allocated but
unused for a long period. As discussed in example in
slide no. 7
• Starvation is possible, because at least one of the resources
that is requested is always allocated to some other process
• To ensure this condition does not hold
11
12. No Preemption
First protocol :
• If a process is holding some resources & requests
another resource that cannot be immediately allocated
to it
– Then all resources currently being hold by that process are
preempted
• The preempted resources are added to list of resources
for which the process is waiting
• The process is restarted only when it regains its old &
as well as new ones
12
13. No Preemption
Second Protocol :
• Process requests some resources
• Check whether they are available, if they are available
allocate them to the requesting process
• If they are not, check whether they are allocated to some
other processes that are waiting
• If so, preempt the desired resources from the waiting
process & allocate them to requesting process
13
14. No Preemption
• If the resources are neither available nor held by
waiting process, the requesting process must wait
• Process can be restarted only when it gains the
new resources it is requesting
14
15. Circular wait
• To ensure that this condition never holds impose total
ordering of all resource type
• Each process requests resources in an increasing
order of enumeration
• Let R={R1,R2,…..Rm} be the set of resources types
• We define one-to-one F:R N, where N is set of
natural numbers
15
16. Circular wait
For Example :
• If the set of resource types R includes tape drive,
disk drives & printers
• Then the function F might be defined as follows
F ( tape drive )= 1
F ( disk drives)= 5
F (printer)= 12
16
17. Circular wait ( Protocol )
• Each process request resources only in an increasing order of
enumeration. i.e., a process can initially request any no. of resource
type say Ri
• The process can request resource type Rj if and only if
F (R j)>F (Ri)
• Whenever a process requests a resource type R j, it can release Ri
such that F (Ri)>=F (Rj)
• Set of Process in circular wait be {P0,P1,…..,Pn}, where Pi is waiting
for Ri
17
18. Circular wait
• Process Pi+1 is holding resource Ri while requesting
Ri+1
• Then we must have F (Ri) < F(Ri+1), for all I
• It means F(R0) < F(R1) <….< F( Rn) < F(R0)
• By transitivity, F(R0) < F(R0), which is impossible
• Therefore there can be no circular wait 18
19. Summary
In this class, you have learnt
• Deadlock prevention
• To Prevent dead lock at least one of the necessary
conditions for the dead lock does not hold
19
20. Frequently Asked Questions
1. Explain Deadlock Prevention ?
2. Write the protocols used hold & wait for deadlock
prevention ?
3. Write the protocols used in No preemption for
deadlock prevention ?
20
21. Quiz
1. To prevent a dead lock
a) At least are of the necessary conditions does not
hold
b) All the necessary conditions does not hold
c) Any two of the necessary conditions does not hold
d) None
21
22. Quiz
2. Mutual exclusion condition must hold for _______
a) Renewable resources
b) Non renewable resources
c) CPU
d) None
22
24. Quiz
3. “Each process must request & be allocated all its
resources before it begins execution” is the protocol of
a) Mutual exclusion
b) Hold & wait
c) No preemption
d) None
24
25. Quiz
4. “Each process requests resources in an increasing
order” is the protocol of
a) Mutual exclusion
b) Hold & wait
c) Circular wait
d) None
25