Although semaphore is a powerful tool to synchronize, using semaphore can cause problems such as deadlock.
Deadlock can be defined as 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.
A simplest deadlock module can be process P1 is using resource R1 and acquiring R2, meanwhile process P2 is using R2 and requiring R1.
When we inquire deeper inside deadlock problem, we can find that deadlock can occur when there is a cycle in a wait for graph.
To detect deadlock needs us to determine whether there is a cycle in the graph.
Therefor the problem can be easy that we need some algorithms to find cycles.
Here is a simple way to do this.
First, we do a topological sorting on a graph.
The result of topological sorting is a sequence of vertices.
Then we compare the number of vertices after topological sorting to the number of vertices that has already in the graph.
If we cannot get an equal value of these two parameters, it is sure that the graph has a cycle.
Here is the code to detect deadlocks in C.
Caution that this following code needs StoneValley library.
#include <stdio.h>
#include "StoneValley/src/svgraph.h"
P_GRAPH_L pWaitingGrp = NULL;
/* (B)
* ^ \
* / v
* (A)<-(C)
* |
* v
* (D)
*
*/
int main()
{
pWaitingGrp = grpCreateL();
if (NULL != pWaitingGrp)
{
P_ARRAY_Z parr;
grpInsertVertexL(pWaitingGrp, 'A');
grpInsertVertexL(pWaitingGrp, 'B');
grpInsertVertexL(pWaitingGrp, 'C');
grpInsertVertexL(pWaitingGrp, 'D');
grpInsertEdgeL(pWaitingGrp, 'A', 'B', 0);
grpInsertEdgeL(pWaitingGrp, 'B', 'C', 0);
grpInsertEdgeL(pWaitingGrp, 'C', 'D', 0);
grpInsertEdgeL(pWaitingGrp, 'C', 'A', 0);
parr = grpTopologicalSortL(pWaitingGrp);
if (NULL != parr && grpVerticesCountL(pWaitingGrp) == parr->num)
printf("No deadlock.\n");
else
printf("deadlocked.\n");
if (parr)
strDeleteArrayZ(parr);
grpDeleteL(pWaitingGrp);
}
return 0;
}
复制代码
BIBLIOGRAPHY:
MODERN OPERATING SYSTEMS FOURTH EDITIO by ANDREW S. TANENBAUM HERBERT BOS
EMBEDDED AND REAL-TIME OPERATING SYSTEMS by K.C.WANG