1 / 32

CMT603

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

gay-vang
Download Presentation

CMT603

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMT603 Lecture 7 Process Deadlock

  2. ReCap • Process interaction • Critical sections • Software solutions • Hardware solutions • Locks • OS solutions • Semaphores • Uses • Classical problem

  3. Contents • What is Deadlock? • Detecting Deadlock • Preventing Deadlock • Avoiding Deadlock

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

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

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

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

  8. 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);

  9. Process 1 Process 2 acquire(S1); acquire(S2); …… release(S1); release(S2); acquire(S2); acquire(S1); …… release(S2); release(S1); S1 P1 P2 S2

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

  11. Detecting Deadlock

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

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

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

  15. An Example R2 R1 P3 P1 P2 R4 R3

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

  17. An Example R2 R1 P3 P1 P2 R4 R3

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

  19. Example Reduction R1 P1 P2 P3 R3

  20. Another Example Reduction R1 R2 P1 P2 P3 R4 R3

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

  22. Deadlock Prevention—Mutual Exclusion • No mutually exclusive access to the sharable resources • Not reasonable - Many resources are not sharable.

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

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

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

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

  27. Resource Allocation States • Safe • Unsafe • Deadlock Unsafe Deadlock Safe

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

  29. Safe State? R1 R2 P1 P2 P3 R4 R3

  30. Unsafe Or Deadlock? R1 (8) P1(3) P2(4) P3(6)

  31. Summary • What is Deadlock? • Detecting Deadlock • Preventing Deadlock • Avoiding Deadlock • For the interested student… look at the Bankers Algorithm

  32. Exercise Sheet

More Related