A deadlock is a situation where "one or more threads of execution" and "one or more
resources" are involved, each thread is waiting for one of the resources, but all the resources
are already held. The threads are all waiting for each other, but never make any progress toward
releasing/unlocking the resources which are already hold by them. Therefore, none of the
threads can continue, which leads to deadlock condition.
A better analogy is a four-way traffic signal. Each vehicle at the stop decides to wait for the
other vehicles before going, no vehicle makes progress to go and we have a traffic deadlock.
One of the simplest example of a deadlock is the self-deadlock, A thread of execution
try to acquire a lock which is already holds, it has to wait for the lock to be released/unlocked.
But it will never release the lock, because it is busy waiting for the lock, and the result is
deadlock:
acquire lock
(try to) acquire lock, again
wait for lock to become available
...
Explain ABBA deadlock ?
ABBA deadlock:
Thread 1 Thread 2
... ...
acquire lock A acquire lock B
... ...
try to acquire lock B try to acquire lock A
waits for lock B waits for lock A
Each thread is waiting for the other and thread will ever release its original lock;
hence, neither lock will ever become available; the result is deadlock.
resources" are involved, each thread is waiting for one of the resources, but all the resources
are already held. The threads are all waiting for each other, but never make any progress toward
releasing/unlocking the resources which are already hold by them. Therefore, none of the
threads can continue, which leads to deadlock condition.
A better analogy is a four-way traffic signal. Each vehicle at the stop decides to wait for the
other vehicles before going, no vehicle makes progress to go and we have a traffic deadlock.
One of the simplest example of a deadlock is the self-deadlock, A thread of execution
try to acquire a lock which is already holds, it has to wait for the lock to be released/unlocked.
But it will never release the lock, because it is busy waiting for the lock, and the result is
deadlock:
acquire lock
(try to) acquire lock, again
wait for lock to become available
...
Explain ABBA deadlock ?
ABBA deadlock:
Thread 1 Thread 2
... ...
acquire lock A acquire lock B
... ...
try to acquire lock B try to acquire lock A
waits for lock B waits for lock A
Each thread is waiting for the other and thread will ever release its original lock;
hence, neither lock will ever become available; the result is deadlock.