Presentation is loading. Please wait.

Presentation is loading. Please wait.

Deadlock and Starvation

Similar presentations


Presentation on theme: "Deadlock and Starvation"— Presentation transcript:

1 Deadlock and Starvation
In-class quiz 17 Allow 15 minutes for students to do Midterm Survey on Angel

2 Deadlock Permanent blocking of a set of processes
Processes are waiting on events, but the events can only be triggered by other blocked processes No simple, efficient solution

3 Bridge Crossing Example
Traffic only in one direction. Each section of a bridge can be viewed as a resource. If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback). Several cars may have to be backed up if a deadlock occurs. Starvation is possible.

4 Can deadlock occur? Process P … Get A Get B Release A Release B ….
Process Q … Get B Get A Release B Release A ….

5 Joint Progress Diagram (Fig.6.2)
The occurrence of deadlock depends on both of the execution rates and the details of the application.

6 Can deadlock occur? Process P … Get A Release A Get B Release B ….
Process Q … Get B Get A Release B Release A ….

7 Types of Resources Reusable vs. Consumable
Preemptible vs Non-preemptible

8 Reusable resources Description: Example:
Used by one process at a time and not depleted by that use Processes obtain resources that they later release for reuse by other processes Example: Processor time, I/O channels, main and secondary memory, files, databases, and semaphores, printers Deadlock occurs if each process holds one resource and requests the other

9 Deadlock with Reusable Resources
P1 Request disk Lock disk Request tape Lock tape Unlock disk Unlock tape P1 Request tape Lock tape Request disk Lock disk Unlock tape Unlock disk

10 Consumable Resources Description: Examples: Deadlock:
Created (produced) and destroyed (consumed) by a process Examples: Interrupts, signals, messages, and information in I/O buffers Deadlock: May occur if a Receive message is blocking May take a rare combination of events to cause deadlock

11 Example of Deadlock with a Consumable Resource
Deadlock occurs if at some point each is waiting to receive a message from the other, assuming Receive is blocking. Caused by a design error. Process P … Receive (Q) … Send (Q, message1) Process Q … Receive (P) … Send (P, message2)

12 Preemptible Resources
Can be taken away (preempted) from the owning process with no ill effects Can be restored to the process at a later time Examples: memory processor Cannot cause deadlock

13 Nonpreemptible Resources
Cannot be taken away from the owning process without adversely affecting its computation Examples: printer tape disk semaphore Can cause deadlock

14 Four Conditions for Deadlock
Necessary policies Mutual exclusion must be enforced. No preemption – no resource can be forcibly removed from the process. Hold and Wait – a process may hold a resource while waiting for another. Sufficient circumstance Circular wait – closed chain of processes exists, such that each process holds at least one resource needed by the next process in the chain. Conditions 1-3 create a situation that could result in deadlock Condition 4 indicates that deadlock has occurred.

15 Resource Allocation Graphs
A directed graph used to represent the current snapshot of a system, i.e. current allocations and pending requests. A circle represents a process A square represents a resource (can have more than one instance of a resource) A graph edge from a resource instance to a process implies that the resource instance is allocated to the process A graph edge from a process to a resource implies that the process is requesting an instance of the resource Can be used to determine if a system is deadlocked.

16 RAG Example with Deadlock
Q2

17 RAG Example with No Deadlock

18 Basic Facts If graph contains no cycles If graph contains a cycle
no deadlock. If graph contains a cycle if only one instance per resource type, then deadlock. if several instances per resource type, possibility of deadlock.

19 Approaches to Deadlock Handling
Ostrich Approach Prevention Avoidance Detection and Recovery

20 Ostrich Approach Treat deadlock as a rare annoyance by ignoring it
Used by most operating systems, including Windows and UNIX Stick your head in the sand and pretend that it never happened. Mathematicians and engineers respond to deadlocks differently. Mathematicians believe it is unacceptable. Engineers think about the severity of the problem, the frequency of the problem, and the cost associated with a solution.

21 Ostrich Approach Not Always Viable
A real-time control system monitoring a gasoline refinery ensures the safe and proper operation of the refinery. An avoidance strategy or prevention strategy must be employed. A computerized pacemaker – can’t afford to miss a single beat deadlock avoidance or prevention must be employed Embedded computers – cell phones, PDAs. These are SoCs or system on chips i.e. everything is on one chip and SoCs are characterized by a small set of resources and real-time processing constraints. Can’t have a system administrator come in and decide which process to abort after deadlock has been detected. Either a prevention or avoidance strategy must be employed.

22 Deadlock Prevention Restrain ways a request can be made
Indirect method: prevent any one of the three necessary conditions from occurring Mutual Exclusion No preemption Hold and Wait Direct method: prevent circular wait from occurring

23 Prevention – Indirect Method
Option 1 – Don’t enfoce mutual exclusion Rarely an option Option 2 – Allow preemption Resource(s) must be preemptible Priority must be used to decide which process should be preempted.

24 Prevention – Indirect Method (cont.)
Option 3 – Don’t allow “Hold and wait” One approach – a process must request all its resources at the same time. If it does not get all of them, then it must release all and block until all resources simultaneously available. Another approach – a process, if denied a resource, must release all of its previously allocated resources. If necessary, it must request them all again together with the next resource. Drawbacks: Processes can be held up for a long time waiting to acquire all resources Processes can hold on to resources they do not immediately need Processes may not know in advance all the resources they need

25 Prevention – Direct Method
Prevent circular wait Organize resources into groups that are linearly ordered: G1 , G2 , G3 … Gn. A process can request a resource from G1 and then from G2 and then from G3 and so on, but not vice versa. Drawbacks Processes may have to request and hold on to resources that may be used much later, thus wasting resources. Processes must know in advance all the resources they need.

26 Deadlock Avoidance Two strategies:
Process initiation denial Resource allocation denial Each process declares the maximum number of resources of each type that it may need Requires that system has a priori information 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/claims of the processes

27 Process Initiation Denial
The following conditions will apply All resources are either available or allocated. No process can claim more than the total amount of resources. No process can claim or hold more resources than it originally claimed. Start a process only if all the needs of the current processes and the new process can be met. Pessimistic approach as the assumption is that all processes will require their maximum claims at the same time.

28 Resource Allocation Denial: Bank Analogy
Credit limit Allocated Ann $ $1000 Mark $ $2000 Judy $ $1000 Greg $ $1000 Bank had a total of $12,000. Current total = $7000. Should Ann’s request for $5000 be granted? If yes, amount remaining is $2000.

29 Resource Allocation Denial: Bank Analogy (cont.)
Credit limit Allocated Ann $ $6000 Mark $ $2000 Judy $ $1000 Greg $ $1000 Bank had a total of $12,000. Current total = $2000. Can any other customer get her/his max credit limit? Judy can take the $2000 and finish, leaving $3000.

30 Resource Allocation Denial: Bank Analogy (cont.)
Credit limit Allocated Ann $ $6000 Mark $ $2000 Greg $ $1000 Bank had a total of $12,000. Current total = $3000. No other customer can get her/his max credit limit According to Banker’s algorithm, Ann’s original request for $5000 should not have been granted.

31 Avoidance Using the Bankers algorithm
A request is granted only if the request keeps the system in a safe state Otherwise, the process blocks A safe state is one which there is at least one sequence of resource allocations such that every process can claim its maximum resources and complete. An unsafe state does not imply deadlock, only the possibility of deadlock All processes do not request maximum claims. Even if they do, deadlock will occur depending on the sequence in which these demands are made.

32 Determination of a Safe State

33 Determination of a Safe State (cont.)

34 Determination of a Safe State (cont.)

35 Determination of a Safe State (cont.)
Q3

36 Deadlock Detection and Recovery
Construct a resource allocation graph If only one instance of each resource exists Circular wait implies deadlock If there are two or more instances of any resource Circular wait could mean deadlock Must reduce the RAG to see if deadlock exists

37 Recovery from Deadlock
Abort all deadlocked processes. Back up each deadlocked process to some previously defined checkpoint and restart all processes. Widely used in current database systems. Possibility of the same situation occurring again. Successively abort all deadlocked processes, until deadlock no longer exists. Must decide which process to abort first. Successively pre-empt resources until deadlock no longer exists. Must decide which process to pre-empt from

38 Recovery from Deadlock (cont.)
To choose which process to abort or to pre-empt a device from: Least amount of processor time consumed so far Least amount of output produced so far Most estimated remaining time Least total resources allocated so far Lowest priority

39 An Integrated Approach
Group resources into a number of classes. Use linear ordering to prevent circular wait between classes. Within a class use an algorithm that is most appropriate for the class.

40 Dining Philosophers Five philosophers who alternately think and eat spaghetti/sushi Share a fork/chopstick with each neighbor Assume each philosopher enters, picks up left utensil, then right utensil, then eats Deadlock if all enter at once Figure 6.11, page 276

41 Dining Philosophers’ problem
Solution 1: Show with an example why this solution is not a correct solution. semaphore fork[5] = {1,1,1,1,1}; Philosopher(int i){ while(true){ Think(); semwait(fork[i]); semwait(fork[(i+1)mod5]); eat(); semsignal(fork[(i+1)mod5]); semsignal(fork[i]); } int main(){ parbegin(Philosopher(0), Philosopher(2), Philosopher(3), Philosopher(4), Philosopher(1));

42 Solution 2: Show with an example why this solution is not a correct solution.
semaphore x = 1; Philosopher(int i){ while(true){ Think(); semwait(x); pick up fork[i]; pick up fork[(i+1)mod5]; semsignal(x); eat(); place fork[i] back on the table; place fork[(i+1)mod5] back on the table; } int main(){ parbegin(Philosopher(1), Philosopher(2), Philosopher(3), Philosopher(4), Philosopher(5));

43 Solution 3: Provide a correct solution for the dining philosophers’ problem.

44 Solutions to Dining Philosophers’ Problem
Buy more utensils Equivalent to increasing resources Put utensil down if 2nd utensil busy Can produce “livelock” if philosophers stay synchronized Room attendant Only let 4 of the philosophers into the room at once May have 4 philosophers in room, but only 1 can eat Invite some left-handed philosophers Grab forks in the other order (right fork, then left fork) Any mix will avoid deadlock (effective linear ordering on forks)


Download ppt "Deadlock and Starvation"

Similar presentations


Ads by Google