Junior — Middle
What is resource deadlock and in what situations does it occur in systems?
sobes.tech AI
Answer from AI
Deadlock is a situation in multitasking systems where two or more processes (or threads) are permanently blocked, each waiting for resources held by the other.
It occurs when all four conditions are simultaneously met:
- Mutual exclusion — a resource can only be held by one process at a time.
- Hold and wait — a process holds one resource and waits for another.
- No preemption — a resource cannot be forcibly taken away from a process; it can only be released voluntarily.
- Circular wait — a cycle exists where each process waits for a resource held by the next process in the cycle.
Example: Process A has acquired resource 1 and waits for resource 2, while process B has acquired resource 2 and waits for resource 1 — both are forever blocked.
In iOS and other systems, deadlocks often occur due to improper synchronization, such as acquiring multiple locks in different orders.
To prevent deadlocks, the following strategies are used:
- Ordering resource acquisition;
- Timeouts for waiting;
- Avoiding holding resources while waiting for others;
- Using specialized synchronization mechanisms.