What is Deadlock?

Answer:

Deadlock – Occurs when resources needed by one process are held by some other waiting process.

Assume we have the following operating system:

1. Finite number of resources to be distributed among some number of competing processes

2. Resources may be of several types and there may be several instances of each

3. When a process requests a resource any instance of that resource will satisfy the process

4. Processes can

  • request a resource
  • use the resource
  • release the resource

5. A set of processes is in a deadlock state when every process in the set is waiting for an event that can be caused only by another process in the set.

a. Same resource type – three tape drives, three processes request a tape drive then they each request another. Dining philosophers request chopsticks held by another.

b. Different resource type – process A has a printer process B has a file, Each requests the other’s resource.

Four Necessary Conditions for Deadlock

1. Mutual exclusion: At least one resource is not sharable, i.e. can only be used by one process at a time

2. Hold and wait: A process holds at least one resource and requests resources held by other processes

3. No preemption: resource cannot be preempted, it must be voluntarily released by the process.

4. Circular wait: Given a set of processes { P1, P2, P3, …Pn} P1 has a resource needed by P2, P2 has a resource needed by P3, …, Pn has a resource needed by P1.

System resource-allocation graph

G = (V,E) where V is a set of vertices and E is a set of edges.

The set of vertices is partitioned into Processes and Resources. A resource-allocation graph is a directed graph where an edge from process Pi to resource Ri indicates that Pi has requested Ri (request edge). An edge from Ri to Pi indicates that Ri has been allocated to Pi (assignment edge).

When drawing the graph, processes are represented by circles and resources by squares. Multiple instances of a resource are represented by a dot in the square.

When a process requests a resource a request edge is drawn. When the resource is allocated, the resource edge becomes an assignment edge. Resource edges points to the resource square, but assignment edges are from the dots within the square.

Resource allocation graph, no deadlock

If the graph contains no cycles, then there is no deadlock

If the graph contains a cycle then a deadlock condition may exist.

Resource allocation graph with cycle and no deadlock. P4 can release an instance of R2 and P3 will be assigned the resource

 

How can we handle deadlocks?

1. Try to prevent them from happening.

2. After system is deadlocked employ some mechanism to detect the deadlock and then recover from deadlock.

3. Ignore the problem, theoretically rare, and pretend deadlocks never occur (UNIX) Since deadlocks are infrequent this may be cheaper alternative

Deadlock Prevention – Make sure one of the 4 necessary conditions for deadlock doesn’t hold.

1. Mutual Exclusion – some resources are sharable. Some cannot be shared or be made sharable. A printer can be used by only one process at a time.

2. Hold and Wait – Whenever a process requests one resource make sure it is not holding another resource.

· method 1 – request all resources before it begins execution,

· method 2 – request resources only when process has none, it may request some resources, use them and then release all resources.

· Downside – method 1 – assume process needs to copy file from tape to disk and then print. It will be holding printer entire time even though it needs it only at the end.

· Downside method 2 – process can request tape and disk, release these and then request printer. Only if file remains on disk!! No guarantee.

· Poor resource utilization, chance of starvation

3. No Preemption-

· method 1 – process holds resources and needs another resource that is not in use. Process must release all resources, then waits on released resources in addition to the needed one

· method 2 – check if resources are available when requested, if so allocate them. If not check if they are allocated to another process that is waiting for resources. If so, preempt the desired resources from the waiting process and allocate them to the requesting process. If the requested resource is not available, then the process waits. It can then be preempted by another process in the same way.

4. Circular Wait

· Place an ordering on the resource types. Processes have to request resources in increasing order of numbering

· Alternately, when a process requests a resource of a lower number, resources of higher number are deallocated.

deadlock prevention can have the side effect of reduced system throughput and low device utilization.

Deadlock Avoidance

Processes declare in advance what its maximum resource usage will be. Use this a prior information about a process’ resource needs to make sure a system never enters deadlock. (deadlock avoidance approach) Dynamically makes sure that the circular wait conditions doesn’t exist.

Def. A system is in a safe state if it can allocate resources to each process, in some order and avoid deadlock.

A system is a safe system if there exists a safe sequence. A sequence of processes is a safe sequence for the current allocation state if for each Pi, the resources that Pi can still request can be satisfied by the currently available resources plus the resources held by all the Pj, with j<i.

In this state if a process requests a resource and it isn’t currently available, it can wait until the other processes finish and then use the resources. The next process in the sequence can finish in the same manner. If this doesn’t happen the state is unsafe. Unsafe states may lead to deadlock.

 

Recover from deadlock.

  • Tell operator, he decides who shall live and who shall die!
  • Abort all deadlocked processes – cost is high. All work done by processes is lost.
  • Abort one at a time until deadlock no longer exists – need to run deadlock detection algorithm after each process is aborted – high overhead this way.
  • Resource Preemption -Choose a blocked resource

-Preempt it (releasing its resources)

-Run the detection algorithm

-Iterate if until the state is not a deadlock state

Problem: Starvation – same process may always be picked as victim, include number of rollback in cost factor.

 

About these ads

About Aroniez

Loves innovation and exploration in technology. AndroidDeveloper
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s