820 likes | 1.08k Views
Operating Systems. CST 352 Deadlock. Topics. Definitions Deadlock Conditions Ostrich Algorithm Deadlock Detection Deadlock Recovery Deadlock Avoidance Deadlock Prevention. Definitions. Resources – Parts of a system that are required for a process to run (e.g. memory, disk, CPU, etc.)
E N D
Operating Systems CST 352 Deadlock CST 352 - Operating Systems
Topics • Definitions • Deadlock Conditions • Ostrich Algorithm • Deadlock Detection • Deadlock Recovery • Deadlock Avoidance • Deadlock Prevention CST 352 - Operating Systems
Definitions • Resources – Parts of a system that are required for a process to run (e.g. memory, disk, CPU, etc.) • Preemptable – A resource that can be taken from a process without adverse affects (e.g. seized memory) • Non Preemtable– A resource that cannot be taken away (e.g. a device driver connected to printer) CST 352 - Operating Systems
Definitions • Deadlock – In a set of processes, each process is waiting for a resource that one of the other process has seized. Remember the dining philosophers. CST 352 - Operating Systems
Definitions A Traffic Deadlock All Stop….. Ready….. Go! CST 352 - Operating Systems
Definitions A Traffic Deadlock Oops…. Deadlock How is this deadlock avoided under normal circumstances? CST 352 - Operating Systems
Definitions Question: • Which of the two types of resource can lead to a deadlock condition? • Why? CST 352 - Operating Systems
Definition Question: Can there be deadlock in a “first-come, first-serve” scheduling scheme? CST 352 - Operating Systems
Required Conditions Four conditions necessary for Deadlock: • Mutual Exclusion – Only one process can access a resource at any given time. • Hold and Wait – A process may hold a resource while waiting for others to become available. • No preemption – No resource may be forcibly taken from a holding process. • Circular wait – There must be at least two processes, each waiting for a resource held by the other. CST 352 - Operating Systems
Deadlock Concepts Modeling Deadlock (grid method) • X – axis: progress of process A. • Y – axis: progress of process B. This gives us a good feel for deadlock overlap, however, it becomes unmanageable for more than two processes. CST 352 - Operating Systems
Deadlock Concepts Modeling Deadlock (Resource Trajectories) Example: Proc A and Proc B competing for Resource 1 and Resource 2. CST 352 - Operating Systems
Deadlock Concepts A Executes B Executes A Executes B acquires 2 B acquires 1 B releases 2 B releases 1 • A can acquire both 1 and 2 CST 352 - Operating Systems
Deadlock Concepts A Executes B Executes A Executes B acquires 2 B acquires 1 A blocks on 1 B releases 2 B releases 1 • A can acquire both 1 and 2 CST 352 - Operating Systems
Deadlock Concepts A Executes B Executes A Executes B acquires 2 A acquires 1 B blocks on 1 X A blocks on 2 Deadlock CST 352 - Operating Systems
Deadlock Concepts To reduce resource contention, construct Process A such that it does not need both resource 1 and resource 2 at the same time. This increases the number of valid paths through the “gray” areas. CST 352 - Operating Systems
Deadlock Concepts Modeling Deadlock (grid method) • The resource trajectory model has disadvantages • Unmanageable for complex systems • Dependent on resource acquisition sequencing • This model does suggest solutions to deadlock (e.g. avoid getting into the “Deadlock Pending” region. CST 352 - Operating Systems
Deadlock Concepts Modeling Deadlock (graph method) • A circle represents a process • A square represents a resource • A directed arch from a circle to a square represents a process requesting a resource. • P1 requesting R1 • A directed arch from square to circle represents a process holding a resource • P1 holding R1 • A Deadlock is condition is represented by a “cycle” in the graph. CST 352 - Operating Systems
Deadlock Concepts Modeling Deadlock (graph method) Example: Processes P1, P2, and P3 Resources R1, R2, and R3 CST 352 - Operating Systems
Deadlock Concepts Modeling Deadlock (graph method) • P1 requests and holds R1 • P2 requests and holds R2 • P3 requests and holds R3 • P1 requests R2 • P2 requests R3 • P3 requests R1 The cyclic condition of the graph indicates this sequence of event will cause deadlock. CST 352 - Operating Systems
Ostrich Algorithm Stick your head in the sand and pretend there is no problem at all. • Mathematically speaking – this is unacceptable. • From an engineering point of view, this may be ok. CST 352 - Operating Systems
Ostrich Algorithm Considerations: • What is the half-life of your OS. • What resource contentions could cause a deadlock. • How often statistically does your OS experience a visible deadlock. CST 352 - Operating Systems
Ostrich Algorithm • The Ostrich Algorithm is the easiest solution to the deadlock problem. • In standard “user” based systems (e.g. Windows, Unix Workstations, etc.), this is perfectly acceptable. • Would you consider the “Ostrich Algorithm” an acceptable solution for an enterprise management system? CST 352 - Operating Systems
Deadlock Detection Graph based approach • For each resource access • Create a node representing the process accessing the resource. • Create a node representing the resource. • Make the appropriate directed association. CST 352 - Operating Systems
Deadlock Detection Graph based approach (example) • PA requests R1 and gets it. • PB requests R2 and gets it. • PC requests R2. • PD requests R3 and gets it. • PD requests R1. • PA requests R3. • Every time a resource request is made: • Add the appropriate node to the graph • Check the graph for cycles. CST 352 - Operating Systems
Deadlock Detection Graph based approach • To implement the graph based approach, the OS needs to: • Build a graph on the fly based on resource requests. • Provide some form of cycle detection to detect deadlock conditions in the constructed graph. CST 352 - Operating Systems
Deadlock Detection Q: • List the four conditions for deadlock. CST 352 - Operating Systems
Deadlock Detection Matrix Approach • Let “n” = number of processes == (P1, …, Pn). • Let “m” = number of resource classes. • E1 – resource of type 1 • E2 – resource of type 2 • … • Em – resource of type m CST 352 - Operating Systems
Deadlock Detection Matrix Approach • At any point in time, the allocation of resources can be represented by a two vectors. (E1, E2, …, Em) – Existing resource vector. (A1, A2, …, Am) – Available resource vector. CST 352 - Operating Systems
Deadlock Detection Matrix Approach • Represent allocation with a matrix • Columns represent resources • Rows represent processes • Represent requests with a matrix • Columns represent resources • Rows represent processes CST 352 - Operating Systems
Deadlock Detection Matrix Approach • Current Allocation and Request c11 c12 … c1m c22 c22 … c2m … … … cn1 cn2 … cnm r11 r12 … r1m r22 r22 … r2m … … … rn1 rn2 … rnm process process Current Allocation Current Request CST 352 - Operating Systems
Deadlock Detection Matrix Approach • At any point in time, the resource state must conform to the equation: For any j (j -> resource “column”): n cij+Aj = Ej i = 1 e.g. allocated instances of resource j + available instances of resource j = number of existing instances of resource j. CST 352 - Operating Systems
Deadlock Detection Matrix Approach • Process allocation is now done by comparing vectors. • Vector A <= B iff • For all i < m, Ai <= Bi • Initialize the processes to “r” for “run”. • As the algorithm proceeds, set the process flags to either “r” for processes capable of running, or “b” for processes blocked and waiting for a resource. CST 352 - Operating Systems
Deadlock Detection Matrix Approach • Every time there is a resource allocation: • Look for a process (Pi) for which the request row (Ri) <= available resources (Ai). • Looking for resource demands that can be met. • If such a process is found, add the ith row of the c matrix to the A vector. • Loop over next request. • Else • Terminate the detection algorithm and set Pi to “b” • All “b” processes are blocked. CST 352 - Operating Systems
Deadlock Detection Matrix Approach • In practical application, keep the matrices as a running total. • Each time there is a resource allocation, update the matrices. • Each time there is a resource de-allocation, update the matrices. • Deadlock occurs when two (or more) processes have cross referenced resources. CST 352 - Operating Systems
Deadlock Detection Matrix Approach (Example) Available Vector Resource Vector CD Rom CD Rom Printer Printer DVD DVD E = [3 2 2] A = [3 2 2] Current Allocation Request r r r P1 P2 P3 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 C = R = 1) P1 requests 2 Printers CST 352 - Operating Systems
Deadlock Detection Matrix Approach (Example) Available Vector Resource Vector CD Rom CD Rom Printer Printer DVD DVD E = [3 2 2] A = [1 2 2] Current Allocation Request r r r P1 P2 P3 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 C = R = CST 352 - Operating Systems
Deadlock Detection Matrix Approach (Example) Available Vector Resource Vector CD Rom CD Rom Printer Printer DVD DVD E = [3 2 2] A = [1 2 2] Current Allocation Request r r r P1 P2 P3 2 0 0 0 0 0 0 0 0 0 0 0 1 0 2 0 0 0 C = R = 2) P2 requests 1 Printer, 2 DVDs CST 352 - Operating Systems
Deadlock Detection Matrix Approach (Example) Available Vector Resource Vector CD Rom CD Rom Printer Printer DVD DVD E = [3 2 2] A = [0 2 0] Current Allocation Request r r r P1 P2 P3 2 0 0 1 0 2 0 0 0 0 0 0 0 0 0 0 0 0 C = R = CST 352 - Operating Systems
Deadlock Detection Matrix Approach (Example) Available Vector Resource Vector CD Rom CD Rom Printer Printer DVD DVD E = [3 2 2] A = [0 2 0] Current Allocation Request r r r P1 P2 P3 2 0 0 1 0 2 0 0 0 0 0 0 0 0 0 1 1 0 C = R = 3) P3 requests 1 Printer, 1 CD Rom CST 352 - Operating Systems
Deadlock Detection Matrix Approach (Example) Available Vector Resource Vector CD Rom CD Rom Printer Printer DVD DVD E = [3 2 2] A = [0 2 0] Current Allocation Request r r b P1 P2 P3 2 0 0 1 0 2 0 0 0 0 0 0 0 0 0 1 1 0 C = R = 4) P3 blocked CST 352 - Operating Systems
Deadlock Detection Matrix Approach (Example) Available Vector Resource Vector CD Rom CD Rom Printer Printer DVD DVD E = [3 2 2] A = [0 2 0] Current Allocation Request r r b P1 P2 P3 2 0 0 1 0 2 0 0 0 0 0 0 0 0 0 1 1 0 C = R = 5) P2 frees up printer CST 352 - Operating Systems
Deadlock Detection Matrix Approach (Example) Available Vector Resource Vector CD Rom CD Rom Printer Printer DVD DVD E = [3 2 2] A = [1 2 0] Current Allocation Request r r b P1 P2 P3 2 0 0 0 0 2 0 0 0 0 0 0 0 0 0 1 1 0 C = R = CST 352 - Operating Systems
Deadlock Detection Matrix Approach (Example) Available Vector Resource Vector CD Rom CD Rom Printer Printer DVD DVD E = [3 2 2] A = [0 1 0] Current Allocation Request r r r P1 P2 P3 2 0 0 0 0 2 1 1 0 0 0 0 0 0 0 0 0 0 C = R = 6) P3 can now run CST 352 - Operating Systems
Deadlock Detection Matrix Approach (Example) Available Vector Resource Vector CD Rom CD Rom Printer Printer DVD DVD E = [3 2 2] A = [0 1 0] Current Allocation Request r r r P1 P2 P3 2 0 0 0 0 2 1 1 0 0 0 1 0 0 0 0 0 0 C = R = 7) P1 request DVD CST 352 - Operating Systems
Deadlock Detection Matrix Approach (Example) Available Vector Resource Vector CD Rom CD Rom Printer Printer DVD DVD E = [3 2 2] A = [0 1 0] Current Allocation Request b r r P1 P2 P3 2 0 0 0 0 2 1 1 0 0 0 1 0 0 0 0 0 0 C = R = 8) P1 blocked CST 352 - Operating Systems
Deadlock Detection Matrix Approach (Example) Available Vector Resource Vector CD Rom CD Rom Printer Printer DVD DVD E = [3 2 2] A = [0 1 0] Current Allocation Request b r r P1 P2 P3 2 0 0 0 0 2 1 1 0 0 0 1 1 0 0 0 0 0 C = R = 9) P2 request printer CST 352 - Operating Systems
Deadlock Detection Matrix Approach (Example) Available Vector Resource Vector CD Rom CD Rom Printer Printer DVD DVD E = [3 2 2] A = [0 1 0] Current Allocation Request b b r P1 P2 P3 2 0 0 0 0 2 1 1 0 0 0 1 1 0 0 0 0 0 C = R = 10) P2 blocked CST 352 - Operating Systems
Deadlock Detection Matrix Approach (Example) Available Vector Resource Vector CD Rom CD Rom Printer Printer DVD DVD E = [3 2 2] A = [0 1 0] Current Allocation Request b b r P1 P2 P3 2 0 0 0 0 2 1 1 0 0 0 1 1 0 0 0 0 0 C = R = • This is still not deadlock since P3 holds a resource that could allow P2 to commence execution. • If P1 held all 3 printers, we would have true deadlock. CST 352 - Operating Systems
Deadlock Detection Matrix Approach (Example) Available Vector Resource Vector CD Rom CD Rom Printer Printer DVD DVD E = [3 2 2] A = [0 1 0] Current Allocation Request b b r P1 P2 P3 2 0 0 0 0 2 1 1 0 0 0 1 1 0 0 0 0 0 C = R = • What are the general conditions for true deadlock? CST 352 - Operating Systems
Deadlock Detection Hypothesis (extra credit – 50 pts) • Prove or disprove Deadlock can be detected by rotating the rows and columns of the C and R matrix (in conjunction) so that if C and R have any inverse sub-matrices, the involved processes are deadlocked. E and A must be involved. CST 352 - Operating Systems