Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating Systems Part III: Process Management (Deadlocks)

Similar presentations


Presentation on theme: "Operating Systems Part III: Process Management (Deadlocks)"— Presentation transcript:

1 Operating Systems Part III: Process Management (Deadlocks)

2 Deadlocks A set of processes P1, P2, P3,..., is in a deadlock state if every process in the set is waiting for an event E1 that can be caused only by one of them, say P1. But P1 can not make E1 happen, because P1 is waiting for possibly another event E2, which can be caused only by another one of them, say P2. But P2 can not make E2 happen because it is waiting for E1 to happen. The events of interest are resource allocation and release.

3 Example: Deadlock waiting on two semaphores P0 does wait(S), context switch, P1 does wait(Q), context switch: P0 P1 wait(S) wait(Q) wait(Q) wait(S) … signal(S) signal(Q) signal(Q) signal(S)

4 Example 2: Cars face to face on a one-lane bridge, no backing up 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 rollback). Several cars may have to be backed up if a deadlock occurs. Starvation is possible.

5 How a process utilizes a resource request the resource. If the resource is not available right away, then the process must wait until resource becomes available use the resource release the resource

6 Deadlock Characterization 4 conditions hold simultaneously in a deadlock situation: – Mutual exclusion At least one resource is held in non-shareable mode If another process requests that resource, it is delayed – Hold and wait There is a process holding at least one resource and waiting to acquire other resources held by other processes

7 Deadlock Characterization 4 conditions in a deadlock (cont’d) – No preemption Resource can only be released voluntarily by a process after completing its task – Circular wait Set of processes {P 0, P 1, …, P n } P 0 is waiting for P 1, P 1 waiting for P 2, … P n-1 waiting for P n, P n waiting for P 0

8 Deadlock Characterization System resource-allocation graphs – Consists of: Processes and resources in the system Directed edge – Request edge P i -> R j : a process P i requested a resource R j – Assignment edge R j -> P i

9 Deadlock Characterization System resource-allocation graphs (cont’d) – Process denoted by circle – Resource denoted by a square and each instance by a dot in the square – If graph contains no cycles, no deadlock – If graph contains cycles, a deadlock may have occurred – A cycle may exist but no deadlocks occur

10 Methods for Handling Deadlocks Ensure that the system will not enter a deadlock state (deadlock prevention or deadlock avoidance) Allow system to enter deadlock and then recover Ignore the problem altogether and pretend that deadlocks do not exist in the system (ostrich algorithm)

11 Methods for Handling Deadlocks Deadlock prevention – Disadvantages: low resource utilization and reduced system throughput – Mutual exclusion Needed for non-shareable resources (e.g. printers) but not for shareable resources (e.g. read-only files) Cannot prevent mutex for all resources: some resources are intrinsically non-shareable

12 Methods for Handling Deadlocks Deadlock prevention (cont’d) – Hold and wait For this not to happen, guarantee that process does not hold a resource when requesting another Example: allocate all resource to process before execution – No preemption Example: when process waits (request not granted), it must release all resources it is holding -> restarted only if it regains old & new resources

13 Methods for Handling Deadlocks Deadlock prevention (cont’d) – Circular wait Impose total ordering of resources and require process to request resource in increasing number If process holds R i, it can only request another, R j, if and only if R j > R i If process hold R j and requests R i (assuming R j > R i ), then it must release R j

14 Methods for Handling Deadlocks Deadlock avoidance – Needs additional information of how processes will request resources (e.g. process requests printer first then tape drive next) – Safe state System can allocate to each process (up to its maximum) in some order and still avoid a deadlock In case of over-allocation system becomes unsafe Deadlock safe unsafe

15 Methods for Handling Deadlocks Deadlock avoidance (continued) – Resource allocation graph algorithm Extends allocation graph to include claim edge (process may request resource at some future time) denoted by P ---> R Request is granted only if no cycle is formed Limited only to resources with a single instance

16 Methods for Handling Deadlocks Deadlock avoidance (continued) – Banker’s algorithm Used in banks: ensure system does not allocate available cash such that it can no longer satisfy needs of all its customers System determines if allocating requested resources maintains safe state -> allocated, if unsafe state, process waits for others to release resources Applicable to multiple resource instances but less efficient than resource-allocation graph scheme

17 Methods for Handling Deadlocks Deadlock detection – Algorithm to determine if deadlock occurred – Algorithm to recover from the deadlock – Detection algorithm depends on two factors: How often is a deadlock likely to occur? How many processes are affected by the deadlock?

18 Methods for Handling Deadlocks Deadlock detection (continued) – Two scenarios: Single instance of each resource type – Uses wait-for graph Several instances of a resource type – Uses time-varying data structures similar to those used in the Banker’s Algorithm Resource-Allocation GraphCorresponding wait-for graph

19 Methods for Handling Deadlocks Deadlock recovery – Process termination Two methods: – Abort all deadlocked process -> great expense -> some processes may have computed for a long time and may need to be restarted – Partial termination: abort one process at a time until cycle is eliminated -> considerable overhead because deadlock detection invoked after each abort Termination may not be easy (e.g. process updating a file or spooling to printer)

20 Methods for Handling Deadlocks Deadlock recovery – Process termination (continued) Which process to select in case of partial termination? Abort process that will incur minimum cost (depends on process priority, how long process has executed, etc.)

21 Methods for Handling Deadlocks Deadlock recovery (continued) – Resource preemption/removal Successively preempt until cycle is broken Three factors to consider: – Selecting a victim - choose process with minimal cost – Rollback - restart process from some safe state (difficult to determine) -> requires system to keep information about state of all running processes – Starvation - process is always picked as victim -> ensure it is picked only a finite number of times

22 Methods for Handling Deadlocks Combined use of three basic methods (prevention, avoidance, detection) – Assumes that resources can be partitioned into classes – Optimal approach used for each resource class – Example: Class 1: internal resources (e.g. PCB) uses prevention through resource ordering Class 2: central memory (for user jobs) uses prevention through preemption


Download ppt "Operating Systems Part III: Process Management (Deadlocks)"

Similar presentations


Ads by Google