If mutual exclusion is guaranteed, say with semaphores, is a program deadlock-free?

蹲街弑〆低调 提交于 2019-12-07 04:11:11

问题


I define mutual exclusion and deadlock as below, respectively: The mutual exclusion condition exists if at every moment, each shared resource is either assigned to exactly one process, or available. A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause.

Say, binary semaphores are used, ensuring that only one of them can enter its critical region at the same time. Since each process does a down just before entering its critical region and an up just after leaving it, mutual exclusion is guaranteed.

I understand there are four conditions which must all hold for deadlock to occur, one of which is the mutual exclusion condition (no two processes may be simultaneously inside their critical sections).

Since mutual exclusion is guaranteed, is the program, in this case, deadlock-free?

Regards.


回答1:


Not necessarily. Imagine these two threads:

 Thread 1          Thread 2
 ============      =============
 Acquire A         Acquire B
 Acquire B         Acquire A
 Release B         Release A
 Release A         Release B

Here, mutual exclusion is indeed guaranteed - at each instant, semaphores A and B are either owned by one of the threads or available systemwide - but deadlock is still possible if Thread 1 and 2 each acquire their first lock (A for Thread 1, B for Thread 2), but then will deadlock waiting for the resource that the other is holding.

Hope this helps!



来源:https://stackoverflow.com/questions/15215717/if-mutual-exclusion-is-guaranteed-say-with-semaphores-is-a-program-deadlock-fr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!