• No results found

DeadLock OS

N/A
N/A
Protected

Academic year: 2020

Share "DeadLock OS"

Copied!
43
0
0

Loading.... (view fulltext now)

Full text

(1)

Deadlocks

Chapter 6

6.1. Resources

6.2. Introduction to deadlocks

6.4. Deadlock detection and recovery 6.5. Deadlock avoidance

(2)

Resources

• A resource is something that can be acquired, used and released over the time.

• Examples of computer resources used by processes

– Printers

– Tape drives

(3)

Resources

• Processes need access to resources in reasonable order

– Two processes writing simultaneously to a printer will lead to gibberish (meaningless characters).

(4)

Resources

• Suppose a process holds resource A and requests resource B

– at same time another process holds B and requests A

– both are blocked and remain so

• Example Scenario

– Two processes each want to write a scanned document on the CD. Process A request permission to use the scanner and is granted it. Process B is programmed differently and request the CD recorder first and is also

granted it. Now A asks for the CD recorder, but the request is denied until

B releases it. Unfortunately, instead of releasing CD recorder B asks for the

(5)

Resources (1)

• Deadlocks occur when …

– processes are granted exclusive access to devices

– we refer to these devices generally as resources

• Preemptable resources

– can be taken away from a process with no ill effects

• Nonpreemptable resources

(6)

Resources (2)

• Sequence of events required to use a resource

1. request the resource

2. use the resource

3. release the resource

• Must wait if request is denied

– requesting process may be blocked

(7)

Introduction to Deadlocks

• Formal definition :

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

• Usually the event is release of a currently held resource • None of the processes can …

– run

– release resources

(8)

Four Conditions for Deadlock

1. Mutual exclusion condition

• each resource assigned to 1 process or is available

2. Hold and wait condition

• process holding resources can request additional

3. No preemption condition

• previously granted resources cannot forcibly taken away

4. Circular wait condition

• must be a circular chain of 2 or more processes

(9)

Deadlock Modeling (2)

• Modeled with directed graphs

a) resource R assigned to process A

b) process B is requesting/waiting for resource S

(10)

Deadlock Modeling (3)

Strategies for dealing with Deadlocks

1. just ignore the problem altogether

2. detection and recovery

• Let deadlocks occur, detect them, and take action.

3. dynamic avoidance

• careful resource allocation

4. prevention

(11)

How deadlock occurs

(12)

Deadlock Modeling (5)

How deadlock can be avoided

(13)

Detection with One Resource of Each Type (1)

a) Note the resource ownership and requests

(14)

Detection with One Resource of Each Type (1)

• Algorithm to detect Deadlock:

1. For each node, N in the graph, perform the following five steps with N as the starting node.

2. Initialize L to the empty list, and designate all the arcs as unmarked.

3. Add the current node to the end of L and check to see if the node now appears in L two times. If it does, the graph contains a cycle (listed in L) and the algorithm terminates.

4. From the given node, see if there are any unmarked outgoing arcs. If so, go to step 5; if not, go to step 6.

5. Pick an unmarked outgoing arc at random and mark it Then follow it to the new current node and go to step 3.

(15)

• End product of the algorithm

– L = [B, T, E, V, G, U, D, T ]

– Algorithm stops at this point because T is repeated twice in the list L.

– Deadlock detected.

(16)

• Matrix-based algorithm for detecting deadlock

– Among n processes P1 to Pn

m is no. of resource classes (E1 is resources of class 1, E2 is resources of class 2, Em is resources of class m)

E is the existing resource vector

• E.g. if class 1 is of tape drive then E1 = 2 means system has two tape drives

A is the available resource vector (i.e. currently available)

• E.g. if both tape drives are assigned then A1=0

(17)

• Two arrays C and R

C is the current allocation matrix

• i-th row of C tells how many instances of each resource class Pi currently holds.

• Cij is the number of instances of resource j that are held by process i.

R is the request matrix

• Similarly, Rij is the number of instances of resource j that Pi wants.

(18)

Detection with Multiple Resources of Each Type

(19)

An example for the deadlock detection algorithm

(20)

Algorithm for detection:

1. Initially all process are unmarked. Look for an

unmarked process, Pi, for which the i-th row of R is less than or equal to A.

2. If such a process is found, add the i-th row of C to A, mark the process, and go back to step 1.

3. If no such process exists, the algorithm terminates.

When the algorithm finishes, all the unmarked processes, if any, are deadlocked.

(21)

When to run this algorithm:

a) check every time a resource request is made

b) check every k minutes

c) when the CPU utilization has dropped below some threshold

• there will be few runnable processes, and the CPU will often be idle

(22)

15

Recovery from Deadlock

• Deadlock detected, what’s next ? Recovery!

• Recovery through preemption

– take a resource from some other process

– depends on nature of the resource

• For example, to take a laser printer away from its owner, the operator can collect all the sheets already printed and put them in a pile. Then the

(23)

• Recovery through rollback

Checkpoint a process periodically

• State is written to a file

– State includes 1) memory image 2) resource state

» Which resources are assigned to which process

– use this saved state

– restart the process if it is found deadlocked

(24)

Recovery from Deadlock

• Recovery through killing processes

– crudest but simplest way to break a deadlock

– kill one of the processes in the deadlock cycle

– the other processes get its resources

(25)

Deadlock Avoidance

• In deadlock detection algorithms,

– All resources were requested at once

– But in real only one resource at a time is requested by a process

(26)

Deadlock Avoidance: Safe State

• A state is said to be safe if there is some

scheduling order in which every process can run to completion even if all of them

(27)

Safe and Unsafe States (1)

Demonstration that the state in (a) is safe because the system by careful scheduling,

can avoid deadlock.

(28)

Safe and Unsafe States (2)

Demonstration that the sate in b) is unsafe because there is no sequence that guarantees completion. Hence it goes from safe to unsafe i.e. from a) to

(29)

Safe and Unsafe States (3)

• Note that an unsafe state is not a deadlocked state.

• System can still run for a while at b)

(30)

Banker's Algorithm

• It is modeled on the way a small-town banker might deal with a group of

customers to whom he has granted lines of credit.

• What the algorithm does is check to see if

(31)

Banker's Algorithm - Example

• Four customers A,B,C and D.

(32)

The Banker's Algorithm for a Single Resource

• Three resource allocation states

– safe

– safe

(33)

Banker's Algorithm for Multiple Resources

(34)

• The algorithm for checking to see if a state is safe:

– 1. Look for a row, R, whose unmet resource needs are all smaller than or equal to A. If no such row exists, the system will eventually dead-lock since no process can run to completion (assuming processes keep all resources until they exit).

– 2. Assume the process of the row chosen requests all the resources it needs (which is guaranteed to be possible) and finishes. Mark that process as terminated and add all its resources to the A vector.

– 3. Repeat steps 1 and 2 until either all processes are marked terminated (in which case the initial state was safe) or no process is left whose resource needs can be met (in which case there is a deadlock).

(35)

• Current State in shown in figure is safe.

– First D can run then either A or E then the rest of the processes.

• It can lead to unsafe state if B acquires one scanner and after that E also acquires the last scanner.

– Available vector becomes (1 0 0 0) leading to deadlock state.

(36)

• Developed by Djikstra’s but it is only good theoretically good enough. In practical, it is not that useful

– as prior needed resource information is required all the time

– and resources may crash in between.

(37)

Deadlock Prevention

• Deadlock avoidance is difficult as prior information of future requests needs to be known. So for real systems better approach is to prevent it.

• If we can ensure that at least one of four

conditions is never satisfied, then

(38)

Deadlock Prevention

1. Attacking the Mutual Exclusion Condition

• Some devices (such as printer) can be spooled

– only the printer daemon uses printer resource

– thus deadlock for printer eliminated

• Not all devices can be spooled • Principle:

– avoid assigning resource when not absolutely necessary

(39)

2. Attacking the Hold and Wait Condition

• Require processes to request resources before starting

– a process never has to wait for what it needs

• Problems

– may not know required resources at start of run

– also ties up resources other processes could be using

• Variation:

– process must give up all resources

(40)

3. Attacking the No Preemption Condition

• This is not a viable option

• Consider a process given the printer

– halfway through its job

– now forcibly take away printer

– !!??

(41)

4. Attacking the Circular Wait Condition (1)

• Normally ordered resources • A resource graph

(42)

4. Attacking the Circular Wait Condition (1)

(43)

The Difference Between Deadlock Prevention and Deadlock Avoidance

• Deadlock Prevention:

– Preventing deadlocks by constraining how requests for resources can be made in the system and how they are handled (system design).

– The goal is to ensure that at least one of the necessary conditions for deadlock can never hold.

• Deadlock Avoidance:

– The system dynamically considers every request and decides whether it is safe to grant it at this point,

– The system requires additional priori information regarding the overall potential use of each resource for each process.

– Allows more concurrency.

References

Related documents

The homogenization of Dubrovnik as an international tourist destination has helped to increase economic prosperity within the city, but the Croatian Tourism Board has to focus on

- Physical Interface: acquire pyhsical C-arm position and display DRR - Computer Interface: define virtual C-arm position and display DRR. - Validation measure Projection

With Introductory pages, containg the First American Charter, the Fir^t American Pronunziamento, a Chart of the Official Symbols and Seals, Record Blanks of

• Involved in business planning, pre-sales, engagement and program management, as program manager, account manager, project manager for application development and

Usually the parties financial and custody agreement or the court’s decisions on those issues become part of the Final Judgment of Divorce so they have the strength of a court

Cognizant Business Consulting developed a structured, proprietary process called the Test Risk Assessment for Project Improvement (TeRAPI), with the main objective

Aim: To compare the accuracy of Autoregressive Integrated Moving Average (ARIMA) model and Holt-Winters additive exponential smoothing method for forecasting the size of the total

(There are several other types of meters beyond that price range which are not in common use, but those will be discussed in some other article) When analyzing moisture