320 likes | 552 Views
CMT603. Lecture 7 Process Deadlock. ReCap. Process interaction Critical sections Software solutions Hardware solutions Locks OS solutions Semaphores Uses Classical problem. Contents. What is Deadlock? Detecting Deadlock Preventing Deadlock Avoiding Deadlock. What is Deadlock?.
E N D
CMT603 Lecture 7 Process Deadlock
ReCap • Process interaction • Critical sections • Software solutions • Hardware solutions • Locks • OS solutions • Semaphores • Uses • Classical problem
Contents • What is Deadlock? • Detecting Deadlock • Preventing Deadlock • Avoiding Deadlock
What is Deadlock? • Two or more processes are each waiting for each other to release a resource • This can be in a circular chain • A process is said to be deadlocked if it requests a resource that will not become available.
System Model • A system consists of a finite number of resources to be distributed among a number of competing processes Process 1 Process 2 Process 3 Process n
System Model • The resources are partitioned into several types, each of which consists of some number of identical instances • In the previous system, the resource type CPU has two instances and The resource type printer has four instances
An Example • Consider a system with a printer, a CD drive and two processes P1 and P2. Suppose P1 is holding the printer and P2 is holding the CD drive. What happens if P1 requests the CD drive and P2 requests the printer? P1 P2
Another Example • Assume there are resources S1 and S2 and processes P1 and P2. P1 and P2 execute concurrently Process 1 Process 2 acquire(S1); acquire(S2); …… release(S1); release(S2); acquire(S2); acquire(S1); …… release(S2); release(S1);
Process 1 Process 2 acquire(S1); acquire(S2); …… release(S1); release(S2); acquire(S2); acquire(S1); …… release(S2); release(S1); S1 P1 P2 S2
Necessary Conditions • Necessary conditions for deadlock • Mutual Exclusion • Hold and wait • No pre-emption • Circular wait • All four conditions must hold for a deadlock to occur
Resource-Allocation Graph • Deadlock can be described more precisely in terms of a directed graph called a resource-allocation graph • set of vertices V • set of edges E
Vertices • The set of vertices V = {P, R} • P={P1, P2, … Pn}: active processes in the system • Each process Pi is represented as a circle • R={R1, R2, … Rm}: resource types in the system • Each resource type Rj is represented as a rectangle. • If there are more than one instances in a resource type, each instance is represented as a dot within the rectangle Pi Rj
Directed Edges • Request edges: Pi → Rj signifies process Pi has requested an instance of resource type Rj and is currently waiting for the resource • Assignment edges: Rj → Pi signifies an instance of Rj has been allocated to process Pi Pi Rj Pi Rj
An Example R2 R1 P3 P1 P2 R4 R3
Detecting Deadlock in a Resource-Allocation Graph • In a resource-allocation graph in which each resource type only has one instance, a cycle in the graph is both a necessary and a sufficient condition for the existence of deadlock • If the graph contains no cycles, then no process in the system is deadlocked. • If the graph contains a cycle, then deadlock exists.
An Example R2 R1 P3 P1 P2 R4 R3
Detecting Deadlock in a Resource-Allocation Graph • We can detect deadlock by attempting to reduce the resource-allocation graph • If a process’s resource requests can granted • Reduce the graph by that process • Remove the process and all the arrows to and from that process • If a graph can be reduced by all its processes, then there is no deadlock • The set of irreducible processes constitute the set of deadlock processes
Example Reduction R1 P1 P2 P3 R3
Another Example Reduction R1 R2 P1 P2 P3 R4 R3
Preventing Deadlock • For a deadlock to occur, each of the four necessary conditions must hold. • Mutual exclusion • Hold and wait • No preemption • Circular wait • The occurrence of a deadlock can be prevented by ensuring that at least one of the above conditions cannot hold.
Deadlock Prevention—Mutual Exclusion • No mutually exclusive access to the sharable resources • Not reasonable - Many resources are not sharable.
Deadlock Prevention—Hold and Wait • Use one of the following protocols • Each process requests and is allocated all its resources before the start of its execution • A process is allowed to request resources only when it has none • Disadvantages: low resource utilization and Starvation
Deadlock Prevention—Non Preemption • Use the following protocol • If a process that is holding some resources requests another resource that cannot immediately be allocated to it, then all resources currently being held are pre-empted. • Good for CPU and Memory space, Not good for printers etc
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 Play with this Idea
Avoiding Deadlock • Given the priori knowledge of the complete sequence of requests and releases for each process, we can decide for each request whether or not the process should wait in order to avoid a possible future deadlock
Resource Allocation States • Safe • Unsafe • Deadlock Unsafe Deadlock Safe
Safe State / Sequence • A system is in a safe state only if there exists a safe sequence • A safe sequence is a sequence of processes <P1, P2, …, Pn>, such that for each Pi, the resources that Pi can request can be satisfied by the current available resources plus the resources held by all the Pj, with j<i
Safe State? R1 R2 P1 P2 P3 R4 R3
Unsafe Or Deadlock? R1 (8) P1(3) P2(4) P3(6)
Summary • What is Deadlock? • Detecting Deadlock • Preventing Deadlock • Avoiding Deadlock • For the interested student… look at the Bankers Algorithm