Presentation is loading. Please wait.

Presentation is loading. Please wait.

Deadlocks (part II) Deadlock avoidance – (not reasonable)

Similar presentations


Presentation on theme: "Deadlocks (part II) Deadlock avoidance – (not reasonable)"— Presentation transcript:

1 Deadlocks (part II) Deadlock avoidance – (not reasonable)
Deadlock detection Resource Allocation Graphs Resource Pool and Counting Semaphores Dining Philosophers Problem Condition Variables

2 Resource Tables

3 Deadlock Definition A deadlock is a situation in which two or more competing actions are each waiting for the other to finish, and thus neither ever does. P P2 P(R1) P(R2) P(R2) P(R1) CS CS V(R2) V(R1) V(R1) V(R2)

4 Deadlock 4 necessary conditions
Mutual Exclusion – Processes claim exclusive control of the resources they require Wait For – processes hold resources already allocated to them while waiting for additional resources No Pre-emption – resources cannot be removed from the process using them until it is used to completion Circular Wait – A circular chain of processes exist in which each process holds one or more resources that are requested by the next process in the chain.

5 Deadlock Avoidance (reasonable?)
Designing an Operating System (Linux, Windows, iOS, Android etc.) that avoids deadlocks would require removing at least 1 of the 4 necessary conditions of deadlock. Many systems programmers would argue that the removal of any one of these 4 would limit the system too much and the gain of having a deadlock-free system would not be worth it. Just like driving a car, which can on (hopefully rare) occasions get a flat tire, it is popular to design systems that may on rare occasions go into deadlock and the system detects and deals with the problem at that time.

6 Deadlock Detection We need a way to detect that 2 or more processes are in a deadlock. Allow the system to run normally and occasionally have the Operating System check for deadlocks. If a deadlock is detected, the Operation System should attempt to fix the situation as best as it can which includes asking the users of a deadlocked process to volunteer it killing their process which will return resources to the system, which will then end the deadlock.

7 Asking the user to end Deadlock

8 Review of Data Structures and Algorithms
Graphs can be recorded in memory Algorithms can be run on graphs that answer questions: What is the shortest path from point A to point B? Is a graph fully connected (yes or no)? Does a graph contain a cycle (yes or no)? If an existing graph which doesn’t currently have a cycle, was to have an edge added to it, would the graph now have a cycle?

9 Representing Graphs in Memory
Adjacency matrix

10 Representing Graphs in Memory
Adjacency lists

11 Deadlock (DAG with a cycle)
A deadlock is a situation in which two or more competing actions are each waiting for the other to finish, and thus neither ever does. P P2 P(R1) P(R2) P(R2) P(R1) CS CS V(R2) V(R1) V(R1) V(R2)

12 Resource Pool A collection of identical resources Ex)
A room full of printers Computer Storage (on the stack or heap)

13 Counting Semaphores When a system has multiple identical copies of a resource, a Counting Semaphore can be used to allow mutual access to that pool of identical resources. S=8 P(S) { if (S > 0) { S = S - 1; } else OS moves the process to the queue waiting for S Update resource tables and the Resource Allocation Graph

14 Counting Semaphores And the V function for counting semaphores is V(S)
{ if (a process is waiting for an S) OS moves the process to the ready queue } else S = S + 1; Update the resource tables and the RAG )

15 RAG – Resource Allocation Graph (with counting Semaphores)

16 Dijkstra’s Banker’s Algorithm – Safe State
Total common resources is 8

17 Bankers Algorithm - 2 Resource Pools (not in a Safe State)

18 P(S) using Dijkstra’s Banker’s Algorithm
{ if (System would stay in a Safe State) { S = S - 1; } else OS moves the process to the queue waiting for S Update resource tables and the Resource Allocation Graph

19 Dining Philosophers Problem
It was originally formulated in 1965 by Edsger Dijkstra as a student exam exercise, presented in terms of computers competing for access to tape drive peripherals. Soon after, Tony Hoare gave the problem its present formulation.

20 Dining philosophers solution???
while(1) { P(chopstick i); P(chopstick ((i + 1) %n); EAT V(chopstick i); V(chopstick ((i + 1) %n); THINK } In order to deadlock, - all 5 philosophers must be involved. - all 5 just have the right chopstick and want the left

21 Avoid Deadlock – Dining philosophers
Only allow 4 (n-1) philosophers to sit at the table (only 5-way cycles an happen…add variable to count Eaters) Only allow a philosopher to get both or none of the chopsticks (don’t allow hold and wait) Odd philosophers get right chopstick first left second and even get left first and right second. Add a 6th emergency chopstick. The point is Deadlocks for some apps may be avoided on a case by case basis

22 Ex: Second Cashier for long lines
A market has a full-time cashier than handles customers. If the line get too long, a second part-time cashier snaps into action and start serving customers. We need the full-time cashier (that always runs) to wake up the part-time cashier when the line get too long (let’s say 5 customers) Part-time cashier will wait for a signal from the full-time cashier.

23 Condition Variables The value of a variable will determine if your process can proceed or must wait. Ex) full-time cashier always runs and part-time cashier only runs if the condition (customerCount >= 5) if true. This variable (i.e. customerCount) is referred to as a condition variable. You need obtain a mutex to access a condition variable. Based on that variable’s current value, part-time cashier should continue or wait.

24 Waiting while releasing condition variable
Question: You access the mutex for the condition variable, based on it’s value, you may determine that you should wait…how do you release the mutex and make yourself wait? Full-time Cashier process while(1) { P(cc) if (customerCount >= 5) V(c5); } V(cc) // Handle customers P(cc); customerCount–; V(cc); Part-time cashier process while(1) { P(cc) if (customerCount < 5) { P(c5); } V(cc); // Handle Customers P(cc); customerCount–; V(cc);

25 Condition Variables Question: You access the mutex for the condition variable, based on it’s value, you determine that you should wait…how do you release the mutex and make yourself wait? Part-time cashier process while(1) { P(cc) if (customerCount < 5) CP(cc,c5); // release CC and wait on C5 atomically } V(cc); // Handle Customers

26 CP(S,C) Conditional Atomic Operation
void CP(S,C) { V(S); // release the binary semaphore P(C); // wait on the conditional variable }


Download ppt "Deadlocks (part II) Deadlock avoidance – (not reasonable)"

Similar presentations


Ads by Google