Sub topic 16
Deadlock Caused by Wrong Semaphore Ordering
This chapter assumes that you know process and thread basics, CPU preemption, and the wait and signal operations on semaphores. A semaphore is a synchronization object that controls access to a limited resource. wait acquires a permit, and signal releases one.
How wrong ordering creates deadlock
A deadlock is a state in which a group of threads waits forever because each thread holds something that another thread needs.
Consider two binary semaphores:
Aprotects resource A.Bprotects resource B.
Thread 1 needs both resources, so it executes:
wait(A)
wait(B)
use both resources
signal(B)
signal(A)Thread 2 also needs both resources, but it acquires them in the opposite order:
wait(B)
wait(A)
use both resources
signal(A)
signal(B)A possible schedule is:
- Thread 1 completes
wait(A)and holds A. - The scheduler switches to Thread 2.
- Thread 2 completes
wait(B)and holds B. - Thread 1 executes
wait(B)and blocks. - Thread 2 executes
wait(A)and blocks.
Neither thread can continue. Each holds one semaphore and waits for the other.
The four necessary conditions
Deadlock can occur only when all four conditions hold at the same time:
| Condition | Meaning in this example |
|---|---|
| Mutual exclusion | Only one thread can hold each semaphore. |
| Hold and wait | A thread holds one semaphore while waiting for another. |
| No preemption | The system cannot forcibly take the semaphore away. |
| Circular wait | Thread 1 waits for Thread 2, which waits for Thread 1. |
Removing any one condition prevents this form of deadlock.
The most practical solution is to impose a global lock order. For example, require every thread to acquire A before B. Both threads then use this order:
wait(A)
wait(B)
use both resources
signal(B)
signal(A)A thread may wait for A, but no thread holding A can be waiting for B while another thread holds B and waits for A. Therefore, the circular dependency cannot form.
Runnable demonstration
This Python program uses timed acquisition to expose the deadlock pattern without hanging forever. The timeout is only for demonstration. Real synchronization code should establish a safe ordering rather than rely on timeouts.
import threading
import time
a = threading.Semaphore(1)
b = threading.Semaphore(1)
def thread_one():
a.acquire()
print("Thread 1 acquired A")
time.sleep(0.1)
if not b.acquire(timeout=0.5):
print("Thread 1 detected possible deadlock while waiting for B")
else:
b.release()
a.release()
def thread_two():
b.acquire()
print("Thread 2 acquired B")
time.sleep(0.1)
if not a.acquire(timeout=0.5):
print("Thread 2 detected possible deadlock while waiting for A")
else:
a.release()
b.release()
t1 = threading.Thread(target=thread_one)
t2 = threading.Thread(target=thread_two)
t1.start()
t2.start()
t1.join()
t2.join()Avoiding related errors
Release semaphores in the reverse order of acquisition. Also release every semaphore on every error path. If a thread exits while holding a semaphore, other threads may wait forever even without a circular wait.
A safe design rule is:
- Assign each resource a rank.
- Require threads to acquire resources from lower rank to higher rank.
- Release resources in reverse order.
This rule prevents circular wait by construction.
Exercise
Thread P acquires semaphore X and then waits for Y. Thread Q acquires Y and then waits for X. Which deadlock condition is created by this ordering, assuming the other three conditions already hold?
Show answer
Circular wait. P waits for Q to release Y, while Q waits for P to release X.