A condition that occurs when two processes are each waiting for the other to complete before proceeding. The result is that both processes hang. Deadlocks occur most commonly in multitasking and client/server environments. Ideally, the programs that are deadlocked, or the operating system, should resolve the deadlock, but this doesn't always happen.
A deadlock is also called a deadly embrace.
Example:
if (lock == 0) lock = myPID; /* lock free - set it */
The above example does not guarantee that the task has the lock, since more than one task can be testing the lock at the same time. Since both tasks will detect that the lock is free, both tasks will attempt to set the lock, not knowing that the other task is also setting the lock. Dekker's or Peterson's algorithm are possible substitutes if atomic locking operations are not available.
Careless use of locks can result in deadlock. This occurs when a process holds a lock and then attempts to acquire a second lock. If the second lock is already held by another process, the first process will be blocked. If the second process then attempts to acquire the lock held by the first process, the system has "deadlocked": no progress will ever be made. A number of strategies can be used to avoid or recover from deadlocks, both at design-time and at run-time.
Another example:
Deadlock Example
int A, B;
lock_t x, y;
Thread 1
lock(x);
A += 10;
lock(y);
B += 20;
A += B;
unlock(y);
A += 30;
unlock(x);
Thread 2
lock(y);
B += 10;
lock(x);
A += 20;
A += B;
unlock(x);
B += 30;
unlock(y);
2007-01-22 00:18:13
·
answer #1
·
answered by sanjaykchawla 5
·
1⤊
0⤋
Deadlock occurs when two processes try to access the same resource simultaneously. Then, when they retry, it happens again and again and again.
2007-01-22 08:01:01
·
answer #2
·
answered by Kokopelli 6
·
0⤊
0⤋