answersLogoWhite

0

A deadlock occurs in multi-threaded applications whenever a thread attempts to acquire a lock that can never be unlocked. Locks are necessary in order to prevent concurrent threads from accessing a shared resource while that resource is being modified. Once a thread acquires a lock, all others must wait until the lock is released. In order to prevent any unnecessary waiting, it is vital that each thread acquire a lock immediately before operating upon a shared resource, and to immediately release that lock as soon as it has finished with the resource.

Sometimes it is necessary to operate upon two (or more) resources. Ideally, those resources would be combined into a single resource with just one lock, but it is not unusual to have independent resources that are only occasionally dependant upon each other, so it makes sense to keep them as independent as possible and apply the dependency only when it is actually needed.

When we need to create a dependency to perform an operation it is vital that all the independent locks be applied in the same sequence, regardless of which order the operations must be performed. This means that we may end up holding onto locks far longer than is strictly necessary, but it is the only way to prevent a deadlock.

For example, if we have two threads, A and B, and two resources, X and Y, such that A acquires X followed by Y while B acquires Y followed by X, then we could easily end up in the situation where A acquires X while B acquires Y. A is now waiting for B to release Y, but B is waiting for A to release X. They are deadlocked. Had they both acquired X and Y in the same order, the deadlock would have been avoided.

Another situation where a deadlock often occurs is when a recursive function acquires a lock but doesn't release that lock before calling itself. The new instance cannot lock the resource because it is already locked, thus the function has deadlocked itself. The solution to that problem is to use a recursive lock rather than an ordinary lock, thus allowing a single thread to acquire the lock as often as required. Once a recursive lock is acquired, each subsequent lock by the same thread increments an internal counter while each unlock decrements that counter. When the count is zero the lock is released.

User Avatar

Wiki User

10y ago

What else can I help you with?

Related Questions

What are the various strategies are used to handle deadlock and what is centralized and distributed deadlock detection and prevention?

There are four strategies of dealing with deadlock problem:1. The Ostrich ApproachJust ignore the deadlock problem altogether.2. Deadlock Detection and RecoveryDetect deadlock and, when it occurs, take steps to recover.3. Deadlock AvoidanceAvoid deadlock by careful resource scheduling.4. Deadlock PreventionPrevent deadlock by resource scheduling so as to negate at least one of the four conditions.


What is an antonym for the word deadlock?

The antonym for deadlock is agreement.


When was Wedlock Deadlock created?

Wedlock Deadlock was created in 1947.


When was Holy Deadlock created?

Holy Deadlock was created in 1934.


When was Deadlock - film - created?

Deadlock - film - was created in 1931.


What are the release dates for House of Dreams - 2004 To Deadlock or Not to Deadlock 1-3?

House of Dreams - 2004 To Deadlock or Not to Deadlock 1-3 was released on: USA: 22 January 2004


How many pages does Holy Deadlock have?

Holy Deadlock has 311 pages.


What are the different method for handling deadlock?

deadlock handling by 2phase protocol


When was Wolves - Deadlock album - created?

Wolves - Deadlock album - was created in 2007.


When was Deadlock - metal band - created?

Deadlock - metal band - was created in 1997.


What is false deadlock?

the coordinator conculde incorrectly that a deadlock exist and kills some process --------------------------- Detecting a non existent deadlock in distributed system has been referred as false deadlock and it may occur due to communication delay.. ---->Ashok Paranjothi


What is the difference among deadlock avoidance detection and prevention?

Deadlock prevention is the name of the technique that is designed to get rid of deadlocks by changing the specifications of the system , that is the system design change. This is , basically , about how requests about resources are made and how they are permitted. However , deadlock avoidance is a technique that aims to check deadlock possibility dynamically and decides whether it is safe to grant a resource or not. It ,definitely , needs extra information about potential use of resources for each process. 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 apriori information regarding the overall potential use of each resource for each process. Allows more concurrency. Similar to the difference between a traffic light and a police officer directing traffic.