usr 发表于 2022-11-23 04:45:59

【C】Deadlock detecting

本帖最后由 usr 于 2022-11-23 04:50 编辑

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

Golden Blonde 发表于 2022-11-23 11:33:11

话说如果WINDOWS上的线程死锁了,可以通过调用KeForceResumeThread来唤醒。。。
它的工作原理就是把线程的Semaphore设置为有信号。ULONG KeForceResumeThread        (         IN PKTHREAD         Thread       )        
{

    ULONG OldCount;
    KIRQL OldIrql;

    ASSERT_THREAD(Thread);
    ASSERT(KeGetCurrentIrql() <= DISPATCH_LEVEL);

    //
    // Raise IRQL to dispatcher level and lock dispatcher database.
    //

    KiLockDispatcherDatabase(&OldIrql);

    //
    // Capture the current suspend count.
    //

    OldCount = Thread->SuspendCount + Thread->FreezeCount;

    //
    // If the thread is currently suspended, then force resumption of
    // thread execution.
    //

    if (OldCount != 0) {
      Thread->FreezeCount = 0;
      Thread->SuspendCount = 0;
      Thread->SuspendSemaphore.Header.SignalState += 1;
      KiWaitTest(&Thread->SuspendSemaphore, RESUME_INCREMENT);
    }

    //
    // Unlock dispatcher database and lower IRQL to its previous
    // value.
    //

    KiUnlockDispatcherDatabase(OldIrql);

    //
    // Return the previous suspend count.
    //

    return OldCount;
}

usr 发表于 2022-11-23 23:24:59

Golden Blonde 发表于 2022-11-23 11:33
话说如果WINDOWS上的线程死锁了,可以通过调用KeForceResumeThread来唤醒。。。
它的工作原理就是把线程的S ...

感谢科普。请问WINDOWS是怎样检测出有死锁的呢?
页: [1]
查看完整版本: 【C】Deadlock detecting