Deadlock in Operating Systems

System resources (e.g. CPU time, Memory space, I/O devices such as printers etc.) are finite, and many processes will be competing for these resources.  If the requested resource is not readily available, the processes will have to wait. If each process holds a resource and wait for another resource, which is held by another waiting process, and if it finally forms a circular wait sequence, where everyone will be waiting indefinitely without getting the resource, it will result in a deadlock.

 

Necessary Conditions for Deadlock

There are four necessary conditions for a deadlock to occur: Mutual exclusion, hold and wait, no preemption and circular wait. 

Mutual Exclusion – There should be at least one non-shareable resource.

Hold and Wait – A deadlock will occur if a process can hold a resource and then wait for another. If only every resource can wait for a resource (without holding), there won’t be any deadlock as no one is holding on to any resource.

No preemption – If resources allocated to a process cannot be preempted there is a high change of deadlocks. But if resources allocated to a process can be preempted, meaning taken out and given to one waiting, deadlocks won’t happen.

Circular Wait – If each process holds a resource and wait for another resource, which is held by another waiting process, and if it finally forms a circular wait sequence, a deadlock will occur. For instance P1 (hold R1) waits for R2 (hold by P2), P2 (hold R2) wait for R3 (hold by P3), P3 (hold R3) wait for R1 (hold by P1). Everybody will keep on holding and waiting in a loop. There are many diagrams that will help you figure out if there is a circular wait sequence.

 

Introduction to important terms

There are some important operating system terms that are commonly used in the context of deadlocks.

Preemptive and Non-preemptive resources – A preemptive resource can be taken away from the process that is holding it whereas a non-preemptive resource cannot be taken away from the process that is holding it. As you may guess, non-preemptive property is a required condition for deadlock.

Reusable and Consumable resources – A resource is reusable if it can be used again by another process, like CPU, memory, printers etc.; and consumable if they can be used only once, like a message which is consumed by any one of the listeners. When deadlocks are discussed, we usually discuss about reusable resources.

Shareable and Non-shareable resources – Non-shareable resources need to be taken care while discussing about deadlocks, as they cannot be shared simultaneously by processes, and processes will have to wait for their turn to use them. Shareable resources cannot cause a deadlock, as they can be used by processes simultaneously without any waiting. However, we cannot make all resources non-shareable to avoid deadlock, as certain resources are cannot be shared safely.

Starvation

Starvation means that a process is denied necessary resources, without which the program can never finish its task. Starvation is often caused by errors in a scheduling algorithm, but can also be caused by resource leaks, and can be intentionally caused via a denial-of-service attack such as a fork bomb. Refer to the wiki page on resource starvation to read more: http://en.wikipedia.org/wiki/Resource_starvation.

Safe state

A system can be considered to be in safe state if it is not in a state of deadlock and can allocate resources upto the maximum available. A safe sequence of processes and allocation of resources ensures a safe state. If a system is already in a safe state, we can try to stay away from an unsafe state and avoid deadlock. Deadlocks cannot be avoided in an unsafe state.

Next we will see the necessary conditions for deadlocks, deadlock prevention, deadlock avoidance and deadlock detection.

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) ArrayLists (1) Arrays (2) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (52) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (4) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (7) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (8) Javascript Frameworks (1) Java SE Professional (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quizzes (17) references (2) resources (1) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (1) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)