Alokasi

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 85

CSE306 Operating Systems

Lecture #3
Deadlocks

Prepared & Presented by Asst. Prof. Dr. Samsun M. BAŞARICI


Topics covered
▪ Resources and their allocation

▪ What is a deadlock?

▪ How do deadlocks occur?

▪ Can deadlocks be avoided?

▪ Can deadlocks be predicted?

▪ Recovery from deadlock

2
Resources

▪ Examples of computer resources


 printers
 tape drives
 tables
▪ Processes need access to resources in reasonable order
▪ Suppose a process holds resource A and requests
resource B
 at same time another process holds B and requests A
 both are blocked and remain so

3
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.
▪ Example:
 Semaphores A and B, initialized to 1

P0 P1

wait (A); wait (B);


wait (B); wait (A);
4
Bridge Crossing Example
▪ Traffic only in one direction.
▪ Each section of a bridge can be viewed as a resource.
▪ If a deadlock occurs, it can be resolved if one car backs up
(preempt resources and roll back).
▪ Several cars may have to be backed up if a deadlock occurs.
▪ Starvation is possible.

5
Roundabout (Traffic Circle) Example

▪ Traffic only in one direction. Cars only drive forward


▪ Right-hand traffic
▪ Cars: Processes; Lanes: Resources
▪ Deadlock: Each car blocks a lane piece and wants to drive
into the lane of front one

6
Crossing Example

c b 2 3 2

4 d a 4 1

(a) Deadlock possible (b) Deadlock

Figure 6.1 Illustration of Deadlock


7
Process Example
▪ Processes access two shared resources: Mutual Exclusion

8
Resources (1)

▪ Deadlocks occur when …


 processes are granted exclusive access to devices
 we refer to these devices generally as resources
▪ Preemptable resources
 can be taken away from a process with no ill effects
▪ Nonpreemptable resources
 will cause the process to fail if taken away

9
Resources (2): System Model

▪ Resource Types R1, R2, …, Rm-1


 CPU cycles, memory space, I/O devices
▪ Each resource type Ri has Wi instances.
▪ Each process utilizes a resource as follows:
 request
 use
 release

10
Resources (3)

▪ Sequence of events required to use a resource


1. request the resource
2. use the resource
3. release the resource

▪ Must wait if request is denied


 requesting process may be blocked
 may fail with error code

11
Resource Acquisition (1)

▪ Using a semaphore to protect resources.


(a) One resource. (b) Two resources.

12
Resource Acquisition (2)

▪ (a) Deadlock-free
code.

13
Resource Acquisition (3)

▪ (b) Code with a potential


deadlock.

14
Resource Categories

Reusable
• Can be safely used by only one process at a time
and is not depleted by that use
• Processors, I/O channels, main and secondary
memory, devices, and data structures such as
files, databases, and semaphores

Consumable
• One that can be created (produced) and
destroyed (consumed)
• Interrupts, signals, messages, and
information
• In I/O buffers

15
16
Example:
Memory Request
▪ Space is available for allocation of 200Kbytes, and the
following sequence of events occur:

P1 P2
... ...
Request 80 Kbytes; Request 70 Kbytes;
... ...
Request 60 Kbytes; Request 80 Kbytes;

▪ Deadlock occurs if both processes progress to their second


request

17
Consumable Resources Deadlock

▪ Consider a pair of processes, in which each process


attempts to receive a message from the other process and
then send a message to the other process:

▪ Deadlock occurs if the Receive is blocking

18
Introduction to Deadlocks

▪ Formal definition :
A set of processes is deadlocked if each process in the set is waiting
for an event that only another process in the set can cause
▪ Usually the event is release of a currently held resource
▪ None of the processes can …
 run
 release resources
 be awakened

19
Deadlock
▪ The permanent blocking of a set of processes that either
compete for system resources or communicate with each
other
▪ A set of processes is deadlocked when each process in
the set is blocked awaiting an event that can only be
triggered by another blocked process in the set
▪ Permanent because none of the events is
ever triggered
▪ No efficient solution in the general case

20
Four Conditions for Deadlocks (1)

▪ Assumptions of the system


1. Mutual exclusion condition
• each resource assigned to 1 process or is available
2. Hold and wait condition
• process holding resources can request additional
3. No preemption condition
• previously granted resources cannot forcibly taken away

21
Four Conditions for Deadlock (2)

▪ Appearance of a particular event


4. Circular wait condition
• must be a circular chain of 2 or more processes
• each is waiting for resource held by next member of the
chain

22
Conditions for Deadlock

Mutual
Hold-and-Wait No Pre-emption Circular Wait
Exclusion
• Only one • A process • No resource • A closed
process may may hold can be chain of
use a resource allocated forcibly processes
at a time resources removed from exists, such
• No process while a process that each
may access a awaiting holding it process holds
resource until assignment of at least one
that has been other resource
allocated to resources needed by the
another next process
process in the chain

23
Resource Allocation Graph

▪ V is partitioned into two types:


 P = {P1, P2 , …, Pn}, the set consisting of all the
processes in the system.
 R = {R1, R2, …, Rm}, the set consisting of all resource
types in the system.
▪ request edge: directed edge Pi → Rj
Pi is requesting a resource of type Rj
▪ assignment edge: directed edge Rj → Pi
Pi holds a resource of type Rj

24
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
Rj

25
Example of a Graph with No Cycles

R1 R3

P1 P2 P3

R2 R4

26
Example of a Graph with a Cycle

R1 R3

P1 P2 P3

R2 R4

27
Fundamental Facts

▪ If a graph contains no cycles than no


deadlock.
▪ If a graph contains a cycle than
 if only one instance per resource type,
then deadlock
 if several instances per resource type,
possibility of deadlock.

28
Deadlock Modeling (1)
▪ Modeled with directed graphs

 resource R assigned to process A


 process B is requesting/waiting for resource S
 process C and D are in deadlock over resources T and U

29
Deadlock Modeling (2)
A B C

How deadlock occurs 30


Deadlock Modeling (3)

(o) (p) (q)

How deadlock can be avoided 31


Methods for Handling Deadlock
▪ 1. Ignoring the problem: We can pretend that
deadlocks will never occur and live happily in our
ignorance.
▪ 2. Detection and Recovery : We can allow the
system to enter a deadlock state and then recover.
▪ 3. Dynamic avoidance: Careful resource allocation
▪ 4. Prevention : We can try to design a protocol
which ensures that deadlock never occurs.

32
The Ostrich Algorithm

▪ Pretend there is no problem


▪ Reasonable if
 deadlocks occur very rarely
 cost of prevention is high
▪ UNIX and Windows takes this approach
▪ It is a trade off between
 convenience
 correctness

33
The Ostrich Algorithm (2)
▪ This method used by most operating systems. User
programs are expected to behave properly. The
system does not interface. This is understandable :
it is very hard to make general rules for every
situations which might arise.

34
Deadlock Detection

▪ Allow system to enter deadlock state


▪ Detection Algorithm
▪ Recovery Scheme

35
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.
▪ An algorithm to detect a cycle in a graph requires an
order of n2 operations, where n is the number of
vertices in the graph.

36
Detection with One Resource of Each Type

(a) Note the resource ownership and requests


(b) A cycle can be found within the graph, denoting
deadlock
37
Deadlock Detection Algorithm
▪ For each node N in graph perform following 5 steps with
N as start node (Data structure used is a list L of nodes)
1. Initialize L (L=empty), designate all arcs as unmarked
2. Add current node to L. If node occurs twice in list →
cycle, terminate algorithm
3. From given node; If any unmarked arcs go to step 4, else
go to step 5
4. Pick unmarked edge (arc), follow it to new current node,
go to step 2
5. Dead end; Remove node, go back to previous node, mark
it as current and go to step 2. If this node is initial node
→ no cycle

38
Detection with Multiple Resources of Each Type

Data structures needed by deadlock detection algorithm

39
Multiple Instances of a Resource Type

Every resource is allocated or available

C
i =1
ij + Aj = E j

40
Multiple Instances of a Resource Type
Let n = number of processes and m = number of resource types.

Available: Vector of length m. If Available[j] = k, there are k


instances of resource type Rj available.
Allocation: n x m matrix. If Allocation[i, j] = k, then Pi is
currently allocated k instances of Rj.
Request: n x m matrix. If Request[i, j] = k, then Pi is
requesting k instances of resource type Rj.

41
Detection Algorithm (Short Form)
1. Look for unmarked process Pi for which the i-th row of R is
less than or equal to A

2. If such a process is found, add the i-th row of C to A, mark


the process, go to step 1

3. If no such process exists, terminate

42
Detection Algorithm

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


Initialize:
Work := Available
For i = 1, 2, …, n, if Allocationi  0, then Finish[i] := false, else
Finish[i] := true
2. Find an index i such that both:
(a) Finish[i] = false
(b) Requesti  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 a
deadlocked state. Moreover, if Finish[i] = false, then Pi is
deadlocked.
Algorithm requires O(m  n2) operations to detect deadlock.
43
Detection with Multiple Resources of Each Type

An example for the deadlock detection algorithm

44
Example of Detection Algorithm

▪ 5 processes P0 through P4; 3 resource types A (7 instances), B (2


instances), and C (6 instances).
▪ Snapshot at time T0:

Available Allocation Request

A B C A B C A B C
0 0 0 P0 0 1 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 3 0 0 0
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2

45
Example (Cont.)

▪ P2 requests an additional instance of type C:


Available Allocation Request

A B C A B C A B C
0 0 0 P0 0 1 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
P4 0 0 2 0 0 2

▪ State of the system?

46
Detection Algorithm Usage
▪ When, and how often, to invoke depends on:
 How often is deadlock likely to occur?
 How many processes will need to be rolled back?
▪ If deadlock algorithm is invoked arbitrarily, there may
be many cycles in the resource graph and so we would
not be able to tell which of the many deadlocked
processes “caused” the deadlock.

47
Recovery from Deadlock (1)

▪ Recovery through preemption


 take a resource from some other process
 depends on nature of the resource
▪ Recovery through rollback
 checkpoint a process periodically
 use this saved state
 restart the process if it is found deadlocked

48
Recovery from Deadlock (2)

▪ Recovery through killing processes


 crudest but simplest way to break a deadlock
 kill one of the processes in the deadlock cycle
 the other processes get its resources
 choose process that can be rerun from the beginning

49
Recovery from Deadlock: Resource Preemption
▪ Selecting a victim — minimize cost.
▪ Rollback — return to some safe state, restart process
from that state.
▪ Starvation — same process may always be picked as
victim; include number of rollbacks in cost factor.

50
Recovery from Deadlock: Process
Termination
▪ Abort all deadlocked processes.
▪ Abort one process at a time until the deadlock cycle is
eliminated.
▪ In which order should be choose to abort?
 Priority of the process.
 Computation metric — current length or length to
completion
 Resources the process has used.
 Resources the process is requesting.
 Number of process requiring termination.
 Interactive versus batch.

51
Deadlock Avoidance
Resource Trajectories

Two process resource trajectories


52
Remember: Process Example
▪ Processes access two shared resources: Mutual Exclusion

53
Resource Trajectories (1)

Resource trajectories for the example


54
Resource Trajectories (2)

Resource trajectories for the example


55
Safe and Unsafe States (1)

(a) (b) (c) (d) (e)

Demonstration that the state in (a) is safe

56
Safe and Unsafe States (2)

(a) (b) (c) (d)

Demonstration that the state in (b) is not safe

57
The Banker's Algorithm for a Single Resource

(a) (b) (c)

▪ Three resource allocation states


(a) safe
(b) safe
(c) unsafe

58
Banker's Algorithm for Multiple Resources

Example of banker's algorithm with multiple resources


59
Banker's Algorithm for Multiple Resources
▪ Algorithm for checking if state is safe

1. Look for a row R, whose unmet needs are all smaller than
or equal to A. If no such row → system eventually will
deadlock
2. Assume the process of the row chosen rquests all
resources it needs and finishes. Mark that process as
terminated and add all its resources to A.
3. Repeat steps 1 and 2 until either all processes marked as
terminated (safe state) or deadlock occurs

60
Example for Banker's Algorithm (1)

R1 R2 R3 R1 R2 R3 R1 R2 R3
P1 3 2 2 P1 1 0 0 P1 2 2 2
P2 6 1 3 P2 6 1 2 P2 0 0 1
P3 3 1 4 P3 2 1 1 P3 1 0 3
P4 4 2 2 P4 0 0 2 P4 4 2 0
Claim matrix C Allocation matrix A C–A

R1 R2 R3 R1 R2 R3
9 3 6 0 1 1
Resource vector R Available vector V

(a) Initial state

Figure 6.7 (S) Determination of a Safe State

61
Example for Banker's Algorithm (2)

R1 R2 R3 R1 R2 R3 R1 R2 R3
P1 3 2 2 P1 1 0 0 P1 2 2 2
P2 0 0 0 P2 0 0 0 P2 0 0 0
P3 3 1 4 P3 2 1 1 P3 1 0 3
P4 4 2 2 P4 0 0 2 P4 4 2 0
Claim matrix C Allocation matrix A C–A

R1 R2 R3 R1 R2 R3
9 3 6 6 2 3
Resource vector R Available vector V

(b) P2 runs to completion

Figure 6.7 Determination of a Safe State

62
Example for Banker's Algorithm (3)

R1 R2 R3 R1 R2 R3 R1 R2 R3
P1 0 0 0 P1 0 0 0 P1 0 0 0
P2 0 0 0 P2 0 0 0 P2 0 0 0
P3 3 1 4 P3 2 1 1 P3 1 0 3
P4 4 2 2 P4 0 0 2 P4 4 2 0
Claim matrix C Allocation matrix A C–A

R1 R2 R3 R1 R2 R3
9 3 6 7 2 3
Resource vector R Available vector V

(c) P1 runs to completion

Figure 6.7 Determination of a Safe State

63
Example for Banker's Algorithm (4)

R1 R2 R3 R1 R2 R3 R1 R2 R3
P1 0 0 0 P1 0 0 0 P1 0 0 0
P2 0 0 0 P2 0 0 0 P2 0 0 0
P3 0 0 0 P3 0 0 0 P3 0 0 0
P4 4 2 2 P4 0 0 2 P4 4 2 0
Claim matrix C Allocation matrix A C–A

R1 R2 R3 R1 R2 R3
9 3 6 9 3 4
Resource vector R Available vector V

(d) P3 runs to completion


(d) P3 runs to completion

Figure 6.7 Determination of a Safe State

64
Example for Banker's Algorithm (5)
R1 R2 R3 R1 R2 R3 R1 R2 R3
P1 3 2 2 P1 1 0 0 P1 2 2 2
P2 6 1 3 P2 5 1 1 P2 1 0 2
P3 3 1 4 P3 2 1 1 P3 1 0 3
P4 4 2 2 P4 0 0 2 P4 4 2 0
Claim matrix C Allocation matrix A C–A

R1 R2 R3 R1 R2 R3
9 3 6 1 1 2
Resource vector R Available vector V

(a) Initial state

R1 R2 R3 R1 R2 R3 R1 R2 R3
P1 3 2 2 P1 2 0 1 P1 1 2 1
P2 6 1 3 P2 5 1 1 P2 1 0 2
P3 3 1 4 P3 2 1 1 P3 1 0 3
P4 4 2 2 P4 0 0 2 P4 4 2 0
Claim matrix C Allocation matrix A C–A

R1 R2 R3 R1 R2 R3
9 3 6 0 1 1
Resource vector R Available vector V

(b) P1 requests one unit each of R1 and R3

Figure 6.8 Determination of an Unsafe State


Figure 6.8 Determination of an Unsafe State
65
Example for Banker's Algorithm (6)

66
Deadlock Avoidance Advantages

▪ It is not necessary to preempt and rollback processes, as in


deadlock detection

▪ It is less restrictive than deadlock prevention

67
Deadlock Avoidance Restrictions

• Maximum resource requirement for each process must


be stated in advance

• Processes under consideration must be independent


and with no synchronization requirements

• There must be a fixed number of resources to allocate

• No process may exit while holding resources

68
Deadlock Strategies

Deadlock prevention strategies are very


conservative
• Limit access to resources by imposing restrictions on
processes

Deadlock detection strategies do the opposite


• Resource requests are granted whenever possible

69
Deadline Detection Algorithm

Advantages:
▪ A check for deadlock • It leads to early
detection
can be made as
• The algorithm is
frequently as each relatively simple
resource request or,
less frequently,
depending on how
Disadvantage
likely it is for a • Frequent checks
consume
deadlock to occur considerable
processor time

70
Deadlock Prevention
Attacking the Mutual Exclusion Condition
▪ Some devices (such as printer) can be spooled
 only the printer daemon uses printer resource
 thus deadlock for printer eliminated
▪ Not all devices can be spooled
▪ Principle:
 avoid assigning resource when not absolutely necessary
 as few processes as possible actually claim the resource

71
Attacking the Hold and Wait Condition
▪ Require processes to request resources before starting
 a process never has to wait for what it needs

▪ Problems
 may not know required resources at start of run
 also ties up resources other processes could be using

▪ Variation:
 process must give up all resources
 then request all immediately needed

72
Attacking the No Preemption Condition

▪ This is not a viable option


▪ Consider a process given the printer
 halfway through its job
 now forcibly take away printer
 !!??

73
Attacking the Circular Wait Condition

(a) (b)

(a) Normally ordered resources


(b) A resource graph

74
Approaches to Deadlock Prevention

Summary of approaches to deadlock prevention

75
Combined Approach to Deadlock Handling

▪ Combine the three basic approaches


 Prevention
 Avoidance
 Detection
allowing the use of the optimal approach for each
class of resources in the system.
▪ Partition resources into hierarchically ordered
classes.
▪ Use most appropriate technique for handling
deadlocks within each class.

76
Other Issues
▪ Two-phase locking
▪ Nonresource deadlocks
▪ Communication deadlocks
▪ Livelock
▪ Starvation
Two-Phase Locking

▪ Phase One
 process tries to lock all records it needs, one at a time
 if needed record found locked, start over
 (no real work done in phase one)
▪ If phase one succeeds, it starts second phase,
 performing updates
 releasing locks
▪ Note similarity to requesting all resources at once
▪ Algorithm works where programmer can arrange
 program can be stopped, restarted

78
Nonresource Deadlocks

▪ Possible for two processes to deadlock


 each is waiting for the other to do some task
▪ Can happen with semaphores
 each process required to do a down() on two semaphores
(mutex and another)
 if done in wrong order, deadlock results

79
Communication Deadlocks

▪ A resource deadlock in a network.

80
Livelock

▪ Busy waiting that can lead to livelock.

81
Starvation

▪ Algorithm to allocate a resource


 may be to give to shortest job first

▪ Works great for multiple short jobs in a system

▪ May cause long job to be postponed indefinitely


 even though not blocked

▪ Solution:
 First-come, first-serve policy

82
Summary
 Resources
 Preemptable resources
 Non-preemptable resources
 Resources allocation

 What is a deadlock?
 Background
 Deadlock Conditions
 Deadlock prevention
 Deadlock avoidance
 Deadlock prediction
 Starvation

83
Next Lecture

Memory Management

84
References

▪ Andrew S. Tanenbaum, Herbert Bos, Modern


Operating Systems 4th Global Edition, Pearson, 2015

▪ William Stallings, Operating Systems: Internals and


Design Principles, 9th Global Edition, Pearson, 2017

85

You might also like