Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating Systems (OS)

Similar presentations


Presentation on theme: "Operating Systems (OS)"— Presentation transcript:

1 Operating Systems (OS)
Deadlock

2 Outline What is deadlock? Strategies for handling deadlock
Vad är deadlock? Nån som råkat ut för det?

3 Consider the following example:
What is deadlock? Consider the following example: Legend: A S1 Process B Resource S2 A owns B A B A wants B A B Controlling: Om R controls P: Då håller R P (P är i tillståndet waiting och väntar på R) Om P controls R: Då äger P R, dvs den har tillgång till resursen. “The arrow points at the controlling node”

4 Formal definition of Deadlock
A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause Vad betyder då det här?

5 Resources A hardware device or a piece of data Two types of resources
Preemptable Non-preemptable Deadlock can only occur with non-preemptable resources. The following show how a resource is used: Claim the resource Use the resource Release the resource Preemptable resource: It is possible to take the resource from the process owning it without bad sideeffects One example: Memory [sometimes] Can be swapped to disk

6 Conditions for deadlock
Mutual exclusion A resource is either assigned to 1 process or available Hold and wait condition Possible to own one resource while claiming another No preemption condition granted resources cannot taken away by force Circular wait condition Each waiting process is waiting for the next in the chain All four conditions must be met in order for a deadlock to occur Det här är VILLKOR för deadlock inte strategier...

7 Dining philosophers problem
1 table 5 forks A philosopher can either think or eat Eating procedure: Pick up left fork Pick up right fork Eat Return both forks Possible solutions: Buy more forks, how many?? Solution 1…. Då kan man nysta upp det Grab fork by force (Deadlock detection) Make some protocol, e.g., return if locked (Deadlock avoidance and Deadlock prevention) Shoot philosopher (Deadlock detection) Don’t care at all, who cares about philosophers…let them starve (Ostrich algorithm) How can we deal with the problem?

8 Deadlock management strategies
The Ostrich algorithm Deadlock detection Deadlock avoidance Deadlock prevention

9 The ostrich algorithm Stick your head in the sand and
The easiest way to deal with the problem: Stick your head in the sand and pretend that there is no problem How is it possible to use such an algorithm? Other errors are much more frequent To not add limitations to the operating system Give the fork example: Consider 10 programs, each program can create up to 12 processes, however it is not likely that is occurs at the same time. Say that unix can handle 100 processes max. No restrictions are used because the user might accept a crash every now and then instead of not beeing able to run all the programs. There is a tradeoff between convenience and correctness The ostrich algorithm is both Windows and UNIX approved!! 

10 Deadlock Detection No limitations to resource allocations
Use algorithm to detect possible deadlock Two cases: One resource of each type Multiple resources of each type Resolve any deadlock found When should algorithm be run? When resource is claimed Periodically, every n time unit During idle periods Här accepterar vi deadlock, men vi dtekterar och hanterar det när det väl uppstår Praktiskt att göra när man är idle.

11 Deadlock Detection - One resource of each type
Make a resource graph Locate any cycles Break the cycles found Exercise: PA owns R and wants S PB wants T PC owns S PD owns U and wants S and T PE owns T and wants V PF owns W and wants S PG owns V and wants U Is there deadlock in the system? Process Resource A owns B A B A wants B Studenterna gör övningen! A B “The arrow points at the controlling node”

12 Deadlock Detection - One resource of each type
YES!!! Hur kan man göra en algoritm som löser detta?

13 Deadlock Detection - One resource of each type
Algorithm for finding cycle: For every node N in the graph, perform the following steps using N as starting node. Initiate L as an empty set and unmark all arrows If current node is NOT in the set, add current node to set, else deadlock is detected and the algorithm is terminated If there are unmarked arrows from the current node go to 5 else go to 6 Randomly select one arrow and follow it.Set the new node as the current node. Go to 3 We have reached a dead end. Backtrack to previous node. If previous node is starting node go to 1 using a new starting node. If not, go to 4

14 Deadlock Detection - One resource of each type
N=PB L={} =>L={PB} =>L={PB, T} =>L={PB, T, PE} =>L={PB, T, PE, V} =>L={PB, T, PE, V, PG} =>L={PB, T, PE, V, PG, U} =>L={PB, T, PE, V, PG, U, PD} =>L={PB, T, PE, V, PG, U, PD ,T} Deadlock

15 Deadlock Detection - Many resources of each type
Solved using Matrices A vector of existing resources of type ex A vector of available resources of type ax A matrix containing the # of resources of Type n claimed by process m. A matrix containing the # of resources of Type n requested by process m. We see that:

16 Deadlock Detection - Many resources of each type
For two vectors, X and Y iff for For example: Vad betyder detta? X is less or equal to Y if all elements in x are less or equal to the corresponding element in y.

17 Deadlock Detection - Many resources of each type
The detection algorithm is applied periodically Find an unmarked process Pi for which Ri A. If such a process is found, add Ci to A. Mark the process and go to step 1. If no such process is found, terminate. All unmarked processes are deadlocked In this case C is the previously claimed resources, R is the resources claimed since last invocation. If Pi can continue, it should (as it is now) be able to terminate and thus release all its resources (adding ci to A) The Algorithm DOES NOT look into the future….it looks at the current state. How on earth does this algorithm work?!?!?

18 Deadlock Detection - Many resources of each type
Exercise: 3 2 1 Is there deadlock in the system?? Kommer de att köra i denna ordning, nej kanske inte, men om det finns ett sätt att slutföra allt så har vi inte deadlock än. NO!!!

19 Deadlock Detection How to recover from deadlock Preemption
Process rollback Kill one of the deadlocked process’s Preemption – To take the resource from the process (laser printer, put pages in pile, and then back). This is very hard, in most cases impossible, Rollback – The OS takes continuous snapshots (a process state and resource needs are recorded in a file) of the system, the system is rolled back (to a state where the process doe not have a resource) to the last snapshot. (Databaser funkar så) Kill a process that can be rerun with no ill effects (a compilation) a database update can not be rerun (=> 2 updates)

20 Deadlock Avoidance Avoids deadlock by cautious allocations
Safe and unsafe states An allocation can only be granted if it puts the system in a safe state. Vi nekar en P att ta en resurs om vi kommer att hamna i DL om vi tillåter den ta resursen

21 Deadlock Avoidance - Safe and unsafe states

22 Deadlock Avoidance - Safe and unsafe states
Algorithms to determine the state Based on the E,A,C and R matrices. Example: A total of 10 resources, with the following allocations: Proc Has Max A 3 9 B 2 4 C 7 3 1 SAFE!!! 2 Looks into the future and predicts the worst case scenario… Free 3 5 7 10 Is the system in a safe or unsafe state??

23 Deadlock Avoidance -Bankers Algorithm
Made by Dijkstra Originating from loan allocations in a bank Examines if an allocation leads to a safe or unsafe state Is run when resource is requested Two cases One type of resource Multiple types of resources

24 Deadlock Avoidance -Bankers Algorithm
Example using one type of resource: 7 2 C 4 B 9 3 A Max Has Proc Free 3 Initial state 7 2 C 4 B 9 A Max Has Proc Free 2 A requests 1 NOT GRANTED! SAFE! UNSAFE! 7 2 C 4 3 B 9 A Max Has Proc Free 2 B requests 1 7 3 C 4 B 9 A Max Has Proc Free 1 C requests 1 A Request one, B (4 free) Neither C or A can run, UNSAFE B Request one, B (5 free) C (7 free) A (10 free) SAFE C Request one, B(4 free) C (7 free) A (10 free) SAFE Problems: Not east to know the max, bad utilization (Always assuming worst case) GRANTED! GRANTED! SAFE! SAFE!

25 Deadlock Prevention Very difficult to achieve
Attack one of the conditions for deadlock Attack Mutual Exclusion Attack Hold and Wait Attack No preemption Attack Circular wait Mutual Exclusion: Spooling (daemon pretending to be the resource, serialization by daemon) Printer Not all resources can be spooled Hold and Wait: Initially request all resources (2 phase locking) Low utilization, should Word allocate the printer from the very start??? OR first release its resources then request them+the new Preemption: Allow taking resources from a process by force. Not easily done, which one to take?? Circular wait: Always allocate resources in a predefined order, often used with semaphores…

26 How would you choose to do?
In general purpose OS? In real-time systems (OSEK)? Why?

27 Dining philosophers revisited
Bottom line: Deadlock is rarely controlled by OS Therefore: You as a programmer needs to handle it One easy rule: Always take semaphores in certain order! Let one philosopher take right fork first! 5-1 1 5 1-2 4-5 Not easy to achieve in complex systems developed by different companies… 2 4 3 2-3 3-4

28 Summary What is deadlock? Strategies for handling deadlock
4 conditions Strategies for handling deadlock 4 strategies Vad är deadlock? Nån som råkat ut för det?


Download ppt "Operating Systems (OS)"

Similar presentations


Ads by Google