ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Deadlock Prevention, Detection, Recovery and Avoidance

Updated on October 16, 2019
Deadlock! Think about computer process deadlock being like the traffic deadlock.
Deadlock! Think about computer process deadlock being like the traffic deadlock. | Source
The red cars are causing a deadlock!
The red cars are causing a deadlock! | Source

Deadlock prevention and avoidance in a computer process are the optimal goals but when deadlock occur, detection and recovery can be difficult to initiate.

How many times do you hear “…my computer has hanged up…” or “…my work is taking ages to come out of the printer” or see this response “Program xyz is not responding” Well, one causes of these could be a Deadlock.

This Hub will explain what is a Deadlock, what causes Deadlock, how to prevent a Deadlock, how to detect and recover from a Deadlock and how to avoid Deadlock.

Definition of Deadlock

A situation may arise in shared resources whereby two or more processes (also called threads) cannot continue because the resources which each process requires are held by another process.

This scenario in software systems process management is referred to as Deadlock and in its simplest form it will occur if process 1 is allocated resource A and later it requires resource B and process 2 is allocated resource B and later it requires resource A such that the processes results into a circular wait. The diagram below show a circular waiting caused by three processes.

How deadlock occur and what causes deadlock

How deadlock occur and what causes deadlock
How deadlock occur and what causes deadlock | Source

What causes deadlock?

There are four conditions that allow Deadlock to occur in processes:-

1. The resources involved in processes cannot be shared

2. Processes hold on the resources they have already been allocated while waiting for new ones

3. Resources cannot be pre-emptied (de-allocated) while in use

4. A circular chain of processes exists such that the resource that is currently being requested is in the chain.

Process Deadlock can be solved by adapting one of the following strategies:-

1. Prevent Deadlock by ensuring that at all times at least one of the conditions mentioned above does not hold.

2. Detect Deadlock when it occurs and then employ mechanisms to try and recover it

3. Avoid Deadlock by suitable inspection actions


Deadlock prevention

To prevent Deadlock at least one of the four conditions mentioned above must be denied.

Condition 1 is difficult to deny since some resources, for example printers, by nature cannot be shared between processes. However, the use of spooling can remove Deadlock potential of non-shared peripherals.

Condition 2 can be denied by stipulating that processes request all the resources at once and that they cannot proceed until all the requests are granted. The disadvantage of this action is that resources which are used for a short time are allocated and therefore inaccessible for a long period.

Condition 3 is easily denied by imposing the rule that if a process is denied a request then it must release all the resources that it currently hold and if necessary request for them later together with additional resources. This strategy can be inconvenient in practice since pre-empting a resource like a printer can results into inter-leaving of outputs from several jobs. Further, even if a resource is conveniently pre-emptied the overhead of storing and restoring its states at the time of pre-emption can be quite high.

Condition 4 can be denied by imposing an order on resource types so that if a process has been allocated a resource of type A then it may only request resources of types that fall in that order. This ensures that the circular wait condition does not arise. The disadvantage of this strategy is the constraints imposed on the natural order of resources although this can be reduced by placing commonly used resources early in the order.

Deadlock detection and recovery

This strategy allows the possibility of Deadlock but rely on detection when it occurs and being able to stage recovery. The value of this approach depends on the frequency on which deadlock occurs and the kind of recovery that can be made. Detection algorithms work by detecting the circular wait seen in condition 4. The state of the system at any time can be represented by a state graph.

How deadlock occur and what causes deadlock
How deadlock occur and what causes deadlock | Source

How to detect deadlock and recover from deadlock

A circular wait is represented as a circular closed loop A, B and D (see above). The Deadlock detection algorithm maintains a representation of state graph and inspects it at intervals for existence of a circular loop.

The inspection may occur at every resource allocation. Since the cost of doing the inspection may be very high it may occur at fixed intervals of time and not at each allocation. Detection of deadlock is only useful if an acceptable level of recovery can be made.

The definition of acceptable can be stretched according to circumstances to include the following techniques listed according to the order of sophistication:-

1. Abort all deadlocked processes – this is the method adopted in most general purpose systems.

2. Re-start all deadlocked processes, however, this method may lead straight back to the original Deadlock.

3. Successfully (one at a time) abort deadlocked processes until Deadlock no longer exist. The order in which this is done should reduce resources already used.

4. Successfully (one at a time) pre-empty resources from deadlocked processes until Deadlock no longer exist. The order of pre-empting should be such that to minimize the cost.

Have you ever experienced Deadlock?

See results

Deadlock avoidance

Deadlock avoidance strategy uses an algorithm that anticipates that a Deadlock is likely to occur and therefore deny a resource request which would otherwise be granted.

Before granting a resource request, the state graph of the system is tentatively changed to what it will be if the resource was to be granted and then deadlock detection is applied. If the detection algorithm is clear then the request is granted otherwise the request is denied and the state graph is returned to its original form.

This technique does not work always since it relies on the premise that if the allocation is going to result into a deadlock then it will do so immediately. This is FALSE since Deadlock can occur anytime during the life of a process.

It is important to note that detection and recovery of Deadlock is sometimes left to the computer user rather than being performed by the system itself. An observant computer user will note that certain processes are stuck and realize that Deadlock has occurred. The traditional recovery action is to abort and re-start the deadlocked processes if possible.

Modern Operating Systems provide users with an option to kill a process or processes (for example ‘End Task’ command in ‘Windows Task Manager’) without necessarily shutting down the Operating System.

Ok, now you know what could be the problem with your computer, go ahead and take the necessary action.


    0 of 8192 characters used
    Post Comment

    No comments yet.


    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at:

    Show Details
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the or domains, for performance and efficiency reasons. (Privacy Policy)
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
    ClickscoThis is a data management platform studying reader behavior (Privacy Policy)