Lecture 11,12 and 13 deadlocks


Published on

Published in: Technology, Business
1 Like
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Lecture 11,12 and 13 deadlocks

  1. 1. Rushdi Shams, Dept of CSE, KUET 1 Operating SystemsOperating Systems DeadlocksDeadlocks Version 1.0
  2. 2. Rushdi Shams, Dept of CSE, KUET 2 Definition of Deadlock When two trains approach each other at crossing, both shall come to a full stop and neither shall start up again until the other has gone (Kansas Legislature for railroads in 1920) A process requests resources If the resources are not available at that time, the process enters into waiting state It never again change state because resources it requested are requested by other waiting processes
  3. 3. Rushdi Shams, Dept of CSE, KUET 3 How processes utilize resources If a system has 2 CPUs, then it is said, the system has a resource type CPU of 2 instances A process must request a resource before using it And it must release after using it A process may request as many resources as it requires to carry out its designated task Number of requesting resources MUST not exceed the total number of available resources So, a process utilizes resources with three sequences: request-use-release
  4. 4. Rushdi Shams, Dept of CSE, KUET 4 Deadlock Characterization Mutual exclusion: only one process at a time can use a resource. Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes. No preemption: Process releases resources voluntarily, you cannot force it. Circular wait: there exists a set {P0, P1, …, P0} of waiting processes such that P0is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by
  5. 5. Rushdi Shams, Dept of CSE, KUET 5 Resource-Allocation Graph • A set of vertices V and a set of edges E. V is partitioned into two types: P = {P1, P2, …, Pn}, the set consisting of all the processes in the system. R = {R1, R2, …, Rm}, the set consisting of all resource types in the system. request edge – directed edge P1 → Rj assignment edge – directed edge Rj → Pi
  6. 6. Rushdi Shams, Dept of CSE, KUET 6 Resource-Allocation Graph Process Resource Type with 4 instances Pi requests instance of Rj Pi is holding an instance of Rj Pi Pi Rj Rj
  7. 7. Rushdi Shams, Dept of CSE, KUET 7 Resource Allocation Graph
  8. 8. Rushdi Shams, Dept of CSE, KUET 8 Resource Allocation Graph With A Deadlock
  9. 9. Rushdi Shams, Dept of CSE, KUET 9 Graph With A Cycle But No Deadlock
  10. 10. Rushdi Shams, Dept of CSE, KUET 10 Resource Allocation Graph If graph contains no cycles then no deadlock. If graph contains a cycle then if only one instance per resource type, then deadlock. if several instances per resource type, possibility of deadlock
  11. 11. Rushdi Shams, Dept of CSE, KUET 11 Methods for handling Deadlocks We can prevent deadlock- which is still a dream, but soon have a chance to be real We can avoid deadlocks- by using protocols, ensuring that the system will never enter a deadlock state We can allow the system to enter a deadlock state, detect it and recover We can use Ostrich Approach
  12. 12. Rushdi Shams, Dept of CSE, KUET 12 Ostrich Approach Ostrich is not the name of the inventor of the approach, it is simply an ostrich. When ostriches run, they just run!  They pay very little interest in their surroundings. It means, Ostrich Algorithm in deadlocks is simply ignoring the problem… pretending it will never occur. UNIX runs with this approach.
  13. 13. Rushdi Shams, Dept of CSE, KUET 13 Deadlock Prevention Mutual Exclusion – not required for sharable resources; must hold for nonsharable resources. Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources. Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none. Low resource utilization; starvation possible.
  14. 14. Rushdi Shams, Dept of CSE, KUET 14 Deadlock Prevention No Preemption – • If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. • Preempted resources are added to the list of resources for which the process is waiting. • Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting.
  15. 15. Rushdi Shams, Dept of CSE, KUET 15 Deadlock Avoidance • Requires that the system has some additional a priori information available. Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need. The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition. Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes.
  16. 16. Rushdi Shams, Dept of CSE, KUET 16 Safe State When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state. System is in safe state if there exists a sequence <P1, P2, …, Pn> of ALL the processes is the systems such that for each Pi, the resources that Pican still request can be satisfied by currently available resources + resources held by all the Pj, with j < i. That is: If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished. When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate. When Pi terminates, Pi+1 can obtain its needed resources, and so on.
  17. 17. Rushdi Shams, Dept of CSE, KUET 17 Basic Facts If a system is in safe state, then no deadlocks. If a system is in unsafe state, then possibility of deadlock. Avoidance means ensuring that a system will never enter an unsafe state.
  18. 18. Rushdi Shams, Dept of CSE, KUET 18
  19. 19. Rushdi Shams, Dept of CSE, KUET 19 Deadlock Avoidance: on Overhead Projector
  20. 20. Rushdi Shams, Dept of CSE, KUET 20 Deadlock Detection & Recovery  If a system does not impose deadlock prevention and avoidance mechanism, then a deadlock situation MAY occur.  In this circumstance, the system must provide 1. An algorithm to detect deadlock 2. Recovery Mechanism
  21. 21. Rushdi Shams, Dept of CSE, KUET 21 Detection Mechanism 1 We assume that the resource has only one instance. Maintain wait-for graph (WFG) Nodes are processes. Pi → Pj if Pi is waiting for Pj. Periodically searches for a cycle in the graph. If there is a cycle, there exists a deadlock.
  22. 22. Rushdi Shams, Dept of CSE, KUET 22 Detection Mechanism 1
  23. 23. Rushdi Shams, Dept of CSE, KUET 23 Detection Mechanism 2 We assume that resources have more than one instance Five processes P0 through P4;three resource types A (7 instances), B (2 instances), and C (6 instances). Snapshot at time T0: Allocation Request Available A B C A B C A B C P0 0 1 0 0 0 0 0 0 0 P1 2 0 0 2 0 2 P2 3 0 3 0 0 0 P3 2 1 1 1 0 0 P4 0 0 2 0 0 2 Sequence <P0, P2, P3, P4, P1> will result in
  24. 24. Rushdi Shams, Dept of CSE, KUET 24 Detection Algorithm Usage When should we invoke the detection algorithm? Depends on two factors- How often a deadlock is likely to occur? How many processes will be affected by deadlock when it happens? How many processes will be needed to roll back? Normally detection algorithm is used - Whenever a request is not served In every hour Whenever CPU utilization drops under 40%
  25. 25. Rushdi Shams, Dept of CSE, KUET 25 Recovery Mechanism: Process Termination Abort all deadlocked process Clearly will break the cycle but at a great price Processes may have computed for a long time and now the values are going to be lost  Abort one process at a time until the deadlock cycle is eliminated Considerable overhead
  26. 26. Rushdi Shams, Dept of CSE, KUET 26 Recovery Mechanism: Process Termination In which order should we choose to abort? Priority of the process. How long process has computed, and how much longer to completion. Resources the process has used. Resources process needs to complete. How many processes will need to be terminated. Is process interactive or batch?
  27. 27. Rushdi Shams, Dept of CSE, KUET 27 Recovery Mechanism: Resource Preemption Selecting a victim – minimize cost. Rollback – return to some safe state, restart process for that state. Starvation – same process may always be picked as victim, include number of rollback in cost factor.
  28. 28. Rushdi Shams, Dept of CSE, KUET 28 Reference Operating Systems Concept (6th Edition) by Silberschatz, Galvin and Gagne
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.