DEP
DEP
Branch Unspecified
04 Mar 2007

what are deadlocks?

hi all,

i am studying Operating system these days and finding it very interesting but a little bit complicated as well.

i was studying about deadlocks and til now have not been able to understand the difference between deadlock PREVENTION and AVOIDANCE.i am referring to galvin gain. can anyone please explain me the difference??

is there some specific website except WIKIPEDIA which i can refer to for getting into the details of such things?

thanks.
Neha

Neha

Branch Unspecified
04 Mar 2007
Deadlock Prevention is basically preventing deadlocks by constraining how requests for resources can be made, ensuring that one of the four necessary conditions for deadlock never hold.
On the other hand, deadlock avoidance avoids actions that may lead to deadlock. We have different algorithms for the same.
desijays

desijays

Branch Unspecified
05 Mar 2007
a deadlock happens when a thread decides its time for the other thread to have its share of the CPU's resources and the other thread decides likewise.

Am i wrong? yikes !!!!!
DEP

DEP

Branch Unspecified
06 Mar 2007
Neha
Deadlock Prevention is basically preventing deadlocks by constraining how requests for resources can be made, ensuring that one of the four necessary conditions for deadlock never hold.
On the other hand, deadlock avoidance avoids actions that may lead to deadlock. We have different algorithms for the same.

hi neha,

thanks for replying.
but this much i hve already studied. can you please explain this by citing an eg. from the real world.
miteshmanani

miteshmanani

Branch Unspecified
07 Apr 2007
DEP
hi neha,

thanks for replying.
but this much i hve already studied. can you please explain this by citing an eg. from the real world.
hi all ,

first let me define Deadlock. its as the permanent blocking of a set of processes that either compete for a system resources or communicate with each other. or deadlock involves comflicting needs for resources by 2 or more processes. whether or not deadlock occurs depends upon both the dynamics of application or the details of application.

here is an Example.
in a multi programing system , suppose 2 processes are there and each want to print a very large tape file. Process A request permission to use the printer and its granted. Process B then request for tape drive and then it is also granted. now A asks for the tape drive and A is denied until B releases it. Instead of releasing the tape drive B asks for the printer. At this stage both the process are blocked and will remain forever. This situation is called deadlock.

you might have also studies the 4 condition for Deadlock pervention.

so lets see how its different from deadlock Avoidance....

Deadlock prevention is concerned with imposing certain restriction on the enviornment of processes, so that deadlock can never occur. The OS aims at avoiding a deadlock rather than preventing it.

Deadlock Avoidance is concerned with the Starting with an Enviornment, where a deadlock is theorotically possible. but by some algorithm followed by the OS. it is ensured, before allocating any resource that after allocating it a deadlock can be avoided. If that cannot be guranteed the OS cannot grant the request of a process for a resource at the first place.


Mitesh Manani
Website: https://getpass.netfirms.com
sruti

sruti

Branch Unspecified
10 Apr 2007
You may need to write code that acquires more than one lock. This opens up the possibility of deadlock. Consider the following piece of code: Lock *l1, *l2;
void p() {
l1->Acquire();
l2->Acquire();
code that manipulates data that l1 and l2 protect
l2->Release();
l1->Release();
}
void q() {
l2->Acquire();
l1->Acquire();
code that manipulates data that l1 and l2 protect
l1->Release();
l2->Release();
}
If p and q execute concurrently, consider what may happen. First, p acquires l1 and q acquires l2. Then, p waits to acquire l2 and q waits to acquire l1. How long will they wait? Forever. This case is called deadlock. What are conditions for deadlock?
  • Mutual Exclusion: Only one thread can hold lock at a time.
  • Hold and Wait: At least one thread holds a lock and is waiting for another process to release a lock.
  • No preemption: Only the process holding the lock can release it.
  • Circular Wait: There is a set t1, ..., tn such that t1 is waiting for a lock held by t2, ..., tn is waiting for a lock held by t1.
How can p and q avoid deadlock? Order the locks, and always acquire the locks in that order. Eliminates the circular wait condition.
Occasionally you may need to write code that needs to acquire locks in different orders. Here is what to do in this situation.
  • First, most locking abstractions offer an operation that tries to acquire the lock but returns if it cannot. We will call this operation TryAcquire. Use this operation to try to acquire the lock that you need to acquire out of order.
  • If the operation succeeds, fine. Once you've got the lock, there is no problem.
  • If the operation fails, your code will need to release all locks that come after the lock you are trying to acquire. Make sure the associated data structures are in a state where you can release the locks without crashing the system.
  • Release all of the locks that come after the lock you are trying to acquire, then reacquire all of the locks in the right order. When the code resumes, bear in mind that the data structures might have changed between the time when you released and reacquired the lock.
Here is an example. int d1, d2;
// The standard acquisition order for these two locks
// is l1, l2.
Lock *l1, // protects d1
*l2; // protects d2
// Decrements d2, and if the result is 0, increments d1
void increment() {
l2->Acquire();
int t = d2;
t--;
if (t == 0) {
if (l1->TryAcquire()) {
d1++;
} else {
// Any modifications to d2 go here - in this case none
l2->Release();
l1->Acquire();
l2->Acquire();
t = d2;
t--;
// some other thread may have changed d2 - must recheck it
if (t == 0) {
d1++;
}
}
l1->Release();
}
d2 = t;
l2->Release();
}
This example is somewhat contrived, but you will recognize the situation when it occurs. There is a generalization of the deadlock problem to situations in which processes need multiple resources, and the hardware may have multiple kinds of each resource - two printers, etc. Seems kind of like a batch model - processes request resources, then system schedules process to run when resources are available.
In this model, processes issue requests to OS for resources, and OS decides who gets which resource when. A lot of theory built up to handle this situation.
Process first requests a resource, the OS issues it and the process uses the resource, then the process releases the resource back to the OS.
Reason about resource allocation using resource allocation graph. Each resource is represented with a box, each process with a circle, and the individual instances of the resources with dots in the boxes. Arrows go from processes to resource boxes if the process is waiting for the resource. Arrows go from dots in resource box to processes if the process holds that instance of the resource. See Fig. 7.1.
If graph contains no cycles, is no deadlock. If has a cycle, may or may not have deadlock. See Fig. 7.2, 7.3.
System can either
  • Restrict the way in which processes will request resources to prevent deadlock.
  • Require processes to give advance information about which resources they will require, then use algorithms that schedule the processes in a way that avoids deadlock.
  • Detect and eliminate deadlock when it occurs.
First consider prevention. Look at the deadlock conditions listed above.
  • Mutual Exclusion - To eliminate mutual exclusion, allow everybody to use the resource immediately if they want to. Unrealistic in general - do you want your printer output interleaved with someone elses?
  • Hold and Wait. To prevent hold and wait, ensure that when a process requests resources, does not hold any other resources. Either asks for all resources before executes, or dynamically asks for resources in chunks as needed, then releases all resources before asking for more. Two problems - processes may hold but not use resources for a long time because they will eventually hold them. Also, may have starvation. If a process asks for lots of resources, may never run because other processes always hold some subset of the resources.
  • Circular Wait. To prevent circular wait, order resources and require processes to request resources in that order.
Deadlock avoidance. Simplest algorithm - each process tells max number of resources it will ever need. As process runs, it requests resources but never exceeds max number of resources. System schedules processes and allocates resoures in a way that ensures that no deadlock results.
Example: system has 12 tape drives. System currently running P0 needs max 10 has 5, P1 needs max 4 has 2, P2 needs max 9 has 2.
Can system prevent deadlock even if all processes request the max? Well, right now system has 3 free tape drives. If P1 runs first and completes, it will have 5 free tape drives. P0 can run to completion with those 5 free tape drives even if it requests max. Then P2 can complete. So, this schedule will execute without deadlock.
If P2 requests two more tape drives, can system give it the drives? No, because cannot be sure it can run all jobs to completion with only 1 free drive. So, system must not give P2 2 more tape drives until P1 finishes. If P2 asks for 2 tape drives, system suspends P2 until P1 finishes.
Concept: Safe Sequence. Is an ordering of processes such that all processes can execute to completion in that order even if all request maximum resources. Concept: Safe State - a state in which there exists a safe sequence. Deadlock avoidance algorithms always ensure that system stays in a safe state.
How can you figure out if a system is in a safe state? Given the current and maximum allocation, find a safe sequence. System must maintain some information about the resources and how they are used. See OSC 7.5.3.


Share this content on your social channels -

Only logged in users can reply.