Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating System: DEADLOCKS

Similar presentations


Presentation on theme: "Operating System: DEADLOCKS"— Presentation transcript:

1 Operating System: DEADLOCKS
Lecture#10

2 What is Deadlock? A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set. 8 November 2018

3 What is Deadlock? Example System has 2 tape drives.
P1 and P2 each hold one tape drive and each needs another one. 8 November 2018

4 The Deadlock Problem Example: semaphores A and B, initialized to 1
P0 P1 wait (A); wait(B); wait (B); wait(A); 8 November 2018

5 The Deadlock Problem signal(A); P0 P1 signal(B); 8 November 2018

6 The Deadlock Problem The Dining Philosophers problem … all philosophers become hungry at the same time, pick up the chopsticks on their right and wait for getting the chopsticks on their left. 8 November 2018

7 Bridge Crossing Example
Traffic only in one direction. Each section of a bridge can be viewed as a resource. 8 November 2018

8 Bridge Crossing Example
If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback). Several cars may have to be backed up if a deadlock occurs. Starvation is possible. 8 November 2018

9 System Model Resource types R1, R2, . . ., Rm (CPU cycles, memory space, I/O devices) Each resource type Ri has Wi instances. Each process utilizes a resource as follows: request → use → release 8 November 2018

10 Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously. Mutual exclusion: only one process at a time can use a resource. 8 November 2018

11 Deadlock Characterization
Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes. 8 November 2018

12 Deadlock Characterization
No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task. 8 November 2018

13 Deadlock Characterization
Circular wait: There exists a set {P0, P1, …, P0} 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 P0 is waiting for a resource that is held by P0. P0 → P1 → P2 → … → Pn → P0 8 November 2018

14 Resource Allocation Graph
A set of vertices V and a set of edges E. V is partitioned into two types: P = {P1, P2, …, Pn} R = {R1, R2, …, Rm} E = {Request Edges, Assignment Edges} Request Edge: P1  Rj Assignment Edge: Rj  Pi 8 November 2018

15 Resource Allocation Graph
Process Resource Type with 4 instances Pi requests instance of Rj Pi is holding an instance of Rj Pi Pi 8 November 2018

16 Example Graph 8 November 2018

17 Graph with a Deadlock P1 -> R1 -> P2 -> R3 -> P3 -> R2 -> P1 P2 -> R3 -> P3 -> R2 -> P1 8 November 2018

18 Graph with a Cycle but No Deadlock
P1 -> R1 -> P3 -> R2 -> P1 No deadlock P4 may release its instance of resource R2 Then it can be allocated to P3 8 November 2018

19 RESOURCE-ALLOCATION GRAPH
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.

20 Ensure that the system will never enter a deadlock state.
Deadlock Handling Ensure that the system will never enter a deadlock state. Allow the system to enter a deadlock state and then recover. Ignore the problem and pretend that deadlocks never occur in the system. 8 November 2018

21 Deadlock Handling Deadlock prevention Deadlock avoidance
Deadlock detection and recovery 8 November 2018

22 Deadlock Prevention Restrain the ways resource allocation requests can be made to insure that at least one of the four necessary conditions is violated. 8 November 2018

23 Deadlock Prevention Mutual Exclusion
Cannot be prevented for all resources. Some resources are inherently non-sharable because their states cannot be saved and restored without ill effects, such as a printer. 8 November 2018

24 Deadlock Prevention Hold and Wait – we must guarantee that whenever a process requests a resource, it does not hold any other resources. 8 November 2018

25 Deadlock Prevention Require a process to request and be allocated all its resources before it begins execution, or allow a process to request resources only when the process has none. Low resource utilization; starvation possible. 8 November 2018

26 Deadlock Prevention No Preemption If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. 8 November 2018

27 Deadlock Prevention Preempted resources are added to the list of resources for which the process is waiting for. Process will be restarted only when it can regain its old resources as well as the new ones that it has requested. 8 November 2018

28 Deadlock Prevention Circular Wait – impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration. 8 November 2018

29 Deadlock Prevention We assign a unique number to each resource type by using function F: R → N and make sure that processes request resources in an increasing order of enumeration. For example, tape drive = 1, disk drive = 5, and printer = 12. 8 November 2018

30 Deadlock Prevention Proof Let’s assume that there is a cycle
P0 → P1 → P2 → … → Pk → P0 R R R Rk R0  F(R0) < F(R1) < … F(Rk) < F(R0)  F(R0) < F(R0), which is impossible  There can be no circular wait. 8 November 2018

31 Deadlock Avoidance Requires that the system has additional a priori information available about the use of resources by processes. Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need. 8 November 2018

32 Deadlock Avoidance The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition. Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes. 8 November 2018

33 Safe State When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state. System is in a safe state if there exists a safe sequence of all processes. 8 November 2018

34 Safe State Sequence <P1, P2, …, Pn> is safe if for each Pi, the resources that Pi can still request can be satisfied by the currently available resources, plus the resources held by all the Pj, with j<i. In other words, a safe sequence specifies the order in which processes can be finished. 8 November 2018

35 Safe State If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished. When Pj are finished, Pi can obtain needed resources, execute, return allocated resources, and terminate. When Pi terminates, Pi+1 can obtain its needed resources, and so on. 8 November 2018

36 Basic Facts If a system is in safe state  no deadlocks.
If a system is in unsafe state  possibility of deadlock due to the behavior of processes. Avoidance  ensure that a system never enters an unsafe state. 8 November 2018

37 Safe, Unsafe, and Deadlock States
8 November 2018

38 Example System with 12 tape drives and three processes
Current system state: Process Max Need Allocated P0 10 5 P1 4 2 P2 9 8 November 2018

39 Example System is in a safe state with the safe sequence <P1, P0, P2> P2 requests and is allocated one more tape drive. Process Max Need Allocated P0 10 5 P1 4 2 P2 9 8 November 2018

40 Example Assuming the the tape drive is allocated to P2, the new system state will be: Process Max Need Allocated P0 10 5 P1 4 2 P2 9 3 System gets into an unsafe state. 8 November 2018

41 Deadlock Detection Allow system to enter deadlock state
Detection algorithm Recovery scheme

42 Single Instance of Each Resource Type
Maintain a wait-for graph Nodes are processes. Pi  Pj if Pi is waiting for Pj.

43 Single Instance of Each Resource Type
Periodically invoke an algorithm that searches for a cycle in the wait-for graph. The algorithm is expensive—it requires an order of n2 operations, where n is the number of vertices in the graph.

44 RAG and Wait-for Graph Resource-Allocation Graph
Corresponding wait-for graph

45 Multiple Instance of Each 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.

46 Multiple Instance of Each Resource Type
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. Pj.

47 Detection Algorithm 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;

48 If no such i exists, go to step 4.
Detection Algorithm Find an index i such that both: (a) Finish[i] == false (b) Requesti  Work If no such i exists, go to step 4.

49 Detection Algorithm Work = Work + Allocationi Finish[i] = true go to step 2. 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.

50 Example of Detection Algorithm
Consider the following system: P = { P0, P1, P2, P3, P4 } R = { A, B, C } A: 7 instances B: 2 instances C: 6 instances

51 Example System State Finish Sequence: <> Process P0 P1 P2 P3 P4
Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 1 0 0 0 0 2 Work A B C Finish Sequence: <>

52 Example System State Finish Sequence: <P0> Process P0 P1 P2 P3
Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 1 0 0 0 0 2 Work A B C 1 Finish Sequence: <P0>

53 Example System State Finish Sequence: <P0, P2> Process P0 P1 P2
Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 1 0 0 0 0 2 Work A B C 1 3 2 Finish Sequence: <P0, P2>

54 Example System State Finish Sequence: < P0, P2, P3> Process P0
Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 1 0 0 0 0 2 Work A B C 1 3 2 5 Finish Sequence: < P0, P2, P3>

55 Example System State Finish Sequence: < P0, P2, P3, P4 > Process
Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 1 0 0 0 0 2 Work A B C 1 3 2 5 Finish Sequence: < P0, P2, P3, P4 >

56 Example System State Final finish sequence:
< P0, P2, P3, P4 , P1 > Other possible finish sequences: < P0, P2, P3, P1 , P4 > < P0, P2, P4, P1 , P3 > < P0, P2, P4, P3 , P1 >

57 Example System State P2 requests an additional instance of C.
Do we have a finish sequence? Process P0 P1 P2 P3 P4 Request A B C 0 0 0 2 0 2 0 0 1 1 0 0 0 0 2

58 Example System State Finish Sequence: <> Process P0 P1 P2 P3 P4
Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 0 0 1 1 0 0 0 0 2 Work A B C Finish Sequence: <>

59 Example System State Finish Sequence: <P0> Process P0 P1 P2 P3
Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 0 0 1 1 0 0 0 0 2 Work A B C 1 Finish Sequence: <P0>

60 Example System State P0’s request can be satisfied with currently available resources, but request for no other process can be satisfied after that.  Deadlock exists, consisting of processes P1, P2, P3, and P4.

61 Detection Algorithm How often should the detection algorithm be invoked? Every time a request for allocation cannot be granted immediately—expensive but process causing the deadlock is identified, along with processes involved in deadlock

62 Detection Algorithm Periodically, or based on CPU utilization
Arbitrarily—there may be many cycles in the resource graph and we would not be able to tell which of the many deadlocked processes “caused” the deadlock.

63 Recovery from Deadlock: Process Termination
Abort all deadlocked processes. Abort one process at a time until the deadlock cycle is eliminated.

64 Recovery from Deadlock: Process Termination
In which order should we choose to abort processes? Priority of a process. How long the process has computed, and how much longer to completion.

65 Recovery from Deadlock: Process Termination
Resources the process has used. Resources the process needs to complete. How many processes will need to be terminated. Is the process interactive or batch?

66 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.


Download ppt "Operating System: DEADLOCKS"

Similar presentations


Ads by Google