What is deadlock

What is deadlock? How can I eliminate it?

Deadlock is one of the four particular hazards facing designers of multithreaded programs. The four hazards are: race, deadlock, livelock, and starvation. All four conditions cannot be eliminated merely by testing. 

Testing may indicate the presence of a deadlock, but it cannot be relied upon to indicate freedom from deadlock. This must happen by good design.

A clear model that aids good design is CSP or Communicating Sequential Processes (invented by Tony Hoare, who also invented monitors). CSP is a language for describing patterns of interaction. It is supported by an elegant, mathematical theory, a set of proof tools, and an extensive literature. Recent theoretical work has proven that CSP can be used completely reliably in Java. 

Doug Lea’s book describes JCSP, one of the two libraries through which this is achieved. Using JCSP (or CTJ, the other library), it is possible to design Java programs of any complexity that are provably free from deadlock, livelock, race or starvation.

The following solutions must be applied to your particular needs; sometimes it will be impossible to use one or more of them: 
 

  • Design your program so the only access to a thread-unsafe object is through a thread-safe object. · Spawn new threads to handle each part of the transaction. If each thread only locks one object at a time there can be no deadlock.

  •  
  • Check and back off: If you can test to see if another thread has a lock you want, you can “back off” instead of locking it, allowing it to complete its transaction before trying again. This can’t be done with normal Java locks, but you can try to write your own lock object to satisfy them.

  •  
  • Timeout: If you write your own lock object, it can automatically return from the “lock” method if a certain amount of time elapses.

  •  
  • Minimize or remove synchronization: If there’s no lock, there’s no deadlock!
Relevance Read:

Unix Books :-
UNIX Programming, Certification, System Administration, Performance Tuning Reference Books

Return to : - Unix System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.