A method for detecting inter-task mutual exclusion semaphore deadlock
By using a method for detecting deadlocks caused by mutual exclusion semaphores between multiple tasks, and utilizing task bitmaps and blocking bitmaps to detect deadlocks, the problem of task stagnation caused by deadlocks in the operating system is solved, ensuring normal system operation and resource utilization.
Patent Information
- Application Number
- CN202211617160.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-15
- Publication Date
- 2025-11-04
- Estimated Expiration
- 2042-12-15
AI Technical Summary
In existing technologies, once an operating system deadlocks, tasks cannot complete, resource utilization decreases, and a stagnant state may occur. Furthermore, prolonged blocking may trigger new deadlocks, and there is a lack of effective detection and resolution methods.
Design a method for detecting deadlock of mutual exclusion semaphores between multiple tasks. By using a custom task bitmap and blocking bitmap, detect whether there is deadlock among tasks and report the results to ensure that the system can detect and resolve deadlock in a timely manner.
It enables timely detection and feedback of deadlock situations in the operating system, ensuring system continued operation, improving resource utilization, and avoiding prolonged stagnation and the occurrence of new deadlocks.
Smart Images

Figure CN116204329B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer system software, in particular to a method for detecting mutual exclusion semaphore deadlock among multiple tasks. BACKGROUND
[0002] A task (also known as a process or a thread) is a basic unit of resource allocation and scheduling by an operating system, and a mutual exclusion semaphore is a method for solving mutual exclusion problems among tasks. For example, when two or more tasks are in an infinite waiting state for a mutual exclusion semaphore resource held by the other task, each task will be blocked and suspended, resulting in the problem that the tasks cannot continue to run. When a task is blocked and suspended in a general system, resulting in task lock, the system needs to handle the deadlock. The deadlock handling strategy includes three aspects: prevention of deadlock, avoidance of deadlock, and detection and resolution of deadlock.
[0003] Currently, when a deadlock occurs in an operating system, the tasks that cause the deadlock cannot run to completion, the resources held by the tasks that cause the deadlock cannot be used by other tasks, the resource utilization rate is reduced, and the long-term blocking of the deadlock can cause new deadlocks and other hazards, which can cause the operating system to be in a stagnant state.
[0004] Therefore, it is necessary to detect the deadlock among tasks to ensure the normal operation of the operating system. SUMMARY
[0005] In order to actively detect whether a deadlock occurs during the operation of an operating system and notify which task causes the deadlock, thereby providing support for the resolution of the deadlock and ensuring the normal operation of the operating system, the present application designs a method for detecting mutual exclusion semaphore deadlock among multiple tasks.
[0006] The technical solution for achieving the purpose of the application is as follows: a method for detecting mutual exclusion semaphore deadlock among multiple tasks, comprising the following steps:
[0007] S1, obtaining all tasks of an operating system;
[0008] S2, extracting tasks in a waiting mutual exclusion semaphore state and storing them in a self-defined task bitmap;
[0009] S3, detecting whether any to-be-detected task in the task bitmap exists a deadlock;
[0010] If a deadlock exists, reporting a deadlock detection result;
[0011] If no deadlock exists, entering and performing S4;
[0012] S4, obtaining other tasks waiting for the same mutual exclusion semaphore as the to-be-detected task, and detecting whether the other tasks exist a deadlock;
[0013] If there is a deadlock, report the task deadlock detection result;
[0014] If there is no deadlock, enter and execute S5;
[0015] S5, extract the task holding the mutual exclusion semaphore, and detect whether the task has a deadlock;
[0016] If there is no deadlock, delete the task related to the mutual exclusion semaphore, enter and execute S6;
[0017] If there is a deadlock, take the task as a new task to be detected, enter and execute S4;
[0018] S6, repeat steps S3-S5 until all tasks in the task bitmap are detected.
[0019] In an embodiment, the detection method of whether any one of the tasks to be detected in the task bitmap has a deadlock in step S3 above comprises:
[0020] S301, take out any one of the tasks to be detected in the task bitmap;
[0021] S302, mark the task to be detected in the self-defined blocking bitmap;
[0022] S303, judge whether the task to be detected is repeatedly marked;
[0023] If it is repeatedly marked, it is judged that the task to be detected has a deadlock, and the deadlock detection result is reported;
[0024] If it is initially marked, it is judged that the task to be detected has no deadlock, and then S4 is entered and executed.
[0025] In an embodiment, the method for detecting whether other tasks waiting for the same mutual exclusion semaphore as the task to be detected have a deadlock in step S4 above comprises:
[0026] S401, extract the mutual exclusion semaphore that the task to be detected is waiting for;
[0027] S402, extract other tasks waiting for the same mutual exclusion semaphore in the task bitmap;
[0028] S403, mark the other tasks in the blocking bitmap;
[0029] S404, judge whether the other tasks are repeatedly marked;
[0030] If they are repeatedly marked, it is judged that the other tasks have a deadlock, and the deadlock detection result is reported;
[0031] If the task is marked for the first time, go to and execute S5.
[0032] In one embodiment, the method for detecting whether the task holding the mutual exclusion semaphore has deadlock in step S5 comprises:
[0033] S501, extract the task holding the mutual exclusion semaphore;
[0034] S502, extract and judge whether the task has a waiting mutual exclusion semaphore;
[0035] If the task does not have the waiting mutual exclusion semaphore, it is judged that the task does not have deadlock, go to and execute S503,
[0036] If the task has the waiting mutual exclusion semaphore, the task is taken as a new task to be detected, go to and execute S4;
[0037] S503, delete the task related to the mutual exclusion semaphore in the blocking bitmap.
[0038] In one improved embodiment of the above embodiment, the waiting mutual exclusion semaphore state in step S1 is a permanent waiting mutual exclusion semaphore state.
[0039] In another improved embodiment of the above embodiment, before obtaining all tasks of the operating system in step S1, an empty task bitmap and an empty blocking bitmap are defined. The task bitmap stores the tasks in the operating system in the waiting mutual exclusion semaphore state, and the blocking bitmap stores the task to be detected and the related tasks thereof.
[0040] Compared with the prior art, the beneficial effects of the present application are that the method for detecting the deadlock of the mutual exclusion semaphore between multiple tasks can discover and feedback whether the deadlock phenomenon caused by two or more tasks waiting for the mutual exclusion semaphore occurs as early as possible when the tasks in the operating system cannot be completed or are stalled, so that corresponding deadlock removal measures can be taken according to the feedback deadlock result (including the tasks having deadlock and the related mutual exclusion semaphores), thereby ensuring the system to continue running. BRIEF DESCRIPTION OF DRAWINGS
[0041] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings needed in the embodiment description will be briefly introduced.
[0042] Figure 1 The flowchart of the method for detecting the deadlock of the mutual exclusion semaphore between multiple tasks in the specific embodiment. DETAILED DESCRIPTION
[0043] The present application will be further described below in connection with specific embodiments. The advantages and features of the present application will become apparent with the description. However, these embodiments are only exemplary and do not limit the scope of the present application. Those skilled in the art should understand that the details and forms of the technical solutions of the present application can be modified or replaced without departing from the spirit and scope of the present application, and such modifications and replacements fall within the protection scope of the present application.
[0044] Generally, the operating system includes a plurality of tasks (also referred to as processes or threads), and mutual exclusion operations between the tasks are implemented through mutual exclusion semaphores. Meanwhile, each task records the mutual exclusion semaphores it waits for and the mutual exclusion semaphores it has acquired, and each mutual exclusion semaphore records the tasks waiting for the mutual exclusion semaphore. Since the plurality of tasks in the operating system are executed concurrently, it is possible that several tasks are waiting for each other to release mutual exclusion semaphores so as to continue running, and otherwise, the tasks are blocked and wait forever.
[0045] On this basis, the present embodiment discloses a method for detecting mutual exclusion semaphore deadlock between multiple tasks, as shown in Figure 1 The method for detecting mutual exclusion semaphore deadlock between multiple tasks includes the following steps:
[0046] S1, acquiring all tasks of an operating system.
[0047] S2, extracting tasks in a waiting mutual exclusion semaphore state and storing the tasks in a self-defined task bitmap.
[0048] All tasks of the operating system can be acquired, and each task of the tasks saves mutual exclusion semaphore related information of the task, and thus all tasks waiting for mutual exclusion semaphore resources can be screened out for subsequent analysis.
[0049] S3, detecting whether any to-be-detected task in the task bitmap exists deadlock;
[0050] If the deadlock exists, a deadlock detection result is reported;
[0051] If the deadlock does not exist, S4 is entered and executed.
[0052] S4, acquiring other tasks waiting for the same mutual exclusion semaphore as the to-be-detected task, and detecting whether the other tasks exist deadlock;
[0053] If the deadlock exists, a deadlock detection result is reported;
[0054] If the deadlock does not exist, S5 is entered and executed.
[0055] S5, extracting a task holding the mutual exclusion semaphore, and detecting whether the task exists deadlock;
[0056] If there is no deadlock, delete the task related to the mutual exclusion semaphore, enter and execute S6;
[0057] If there is a deadlock, take the task as a new task to be detected, enter and execute S4.
[0058] S6, repeat steps S3-S5 until all tasks in the task bitmap are detected.
[0059] In one embodiment, in the above step S3, the method for detecting whether any one of the tasks in the task bitmap has a deadlock comprises:
[0060] S301, take out any one of the tasks to be detected in the task bitmap;
[0061] S302, mark the task to be detected in the self-defined blocking bitmap;
[0062] S303, judge whether the task to be detected is repeatedly marked;
[0063] If it is repeatedly marked, it is judged that the task to be detected has a deadlock, and the deadlock detection result is reported;
[0064] If it is initially marked, it is judged that the task to be detected has no deadlock, and then S4 is entered and executed.
[0065] In one embodiment, in the above step S4, the method for obtaining other tasks waiting for the same mutual exclusion semaphore as the task to be detected and detecting whether the other tasks have a deadlock comprises:
[0066] S401, extract the mutual exclusion semaphore waited by the task to be detected;
[0067] S402, extract other tasks waiting for the same mutual exclusion semaphore in the task bitmap;
[0068] S403, mark the other tasks in the blocking bitmap;
[0069] S404, judge whether the other tasks are repeatedly marked;
[0070] If they are repeatedly marked, it is judged that the other tasks have a deadlock, and the deadlock detection result is reported;
[0071] If they are initially marked, S5 is entered and executed.
[0072] In one embodiment, in the above step S5, the method for extracting the task holding the mutual exclusion semaphore and detecting whether the task has a deadlock comprises:
[0073] S501, extract the task holding the mutual exclusion semaphore;
[0074] S502, extracting and judging whether the task has a waiting mutex semaphore;
[0075] If the task does not have the waiting mutex semaphore, it is judged that the task does not have a deadlock, and S503 is entered and executed,
[0076] If the task has the waiting mutex semaphore, the task is taken as a new task to be detected, and S4 is entered and executed;
[0077] S503, deleting the task related to the mutex semaphore in the blocking bitmap.
[0078] In S301-S303 in S3, S401-S304 in S4, and S501-S502 in S5, each bit in the task bitmap is set to 0, and when a task to be detected is extracted and subsequent detection is performed, the task to be detected and the tasks related thereto in the task bitmap are marked as 1. When the blocking bitmap is initially defined, the blocking bitmap is empty and each bit is set to 0.
[0079] In one improved embodiment of the above embodiment, the waiting mutex semaphore state in S1 is a permanent waiting mutex semaphore state.
[0080] In another improved embodiment of the above embodiment, before all tasks of the operating system are acquired in S1, an empty task bitmap and an empty blocking bitmap are defined. The task bitmap stores tasks in the operating system in a waiting mutex semaphore state, and the blocking bitmap stores the task to be detected and the tasks related thereto.
[0081] The detection method for the mutex semaphore deadlock among multiple tasks designed in the present application can discover and feedback whether a deadlock phenomenon caused by two or more tasks waiting to acquire the mutex semaphore occurs as early as possible when the tasks in the operating system cannot be completed or are stalled, so that corresponding deadlock removal measures can be taken according to the feedback deadlock result (including the tasks having the deadlock and the related mutex semaphores), thereby ensuring the system to continue running.
[0082] The present specific embodiment further discloses a computer device, which comprises a memory, a processor, and a computer program stored in the memory and executable on the processor, and the processor implements the detection method for the mutex semaphore deadlock among multiple tasks when executing the computer program.
[0083] The present specific embodiment further discloses a computer readable storage medium, which stores a detection method for the mutex semaphore deadlock among multiple tasks.
[0084] The above merely describes preferred embodiments of the present application and is not used to limit the present application, and any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included in the protection scope of the present application.
[0085] In addition, it should be understood that, although the present specification is described in terms of embodiments, not every embodiment contains only one independent technical solution, and the description of the specification is only for the sake of clarity, and those skilled in the art should consider the specification as a whole, and the technical solutions in each embodiment can also be combined appropriately to form other embodiments that those skilled in the art can understand.
Claims
1. A method for detecting inter-task mutual exclusion semaphore deadlock, characterized by, The method comprises the following steps: S1, obtaining all tasks of an operating system; S2, extracting a task in a waiting mutual exclusion semaphore state and storing the task in a self-defined task bitmap; S3, detecting whether any one of the tasks in the task bitmap exists a deadlock; if the deadlock exists, reporting a deadlock detection result; if the deadlock does not exist, entering and executing S4; S4, obtaining other tasks waiting for the same mutual exclusion semaphore as the task to be detected, and detecting whether the other tasks exist a deadlock; if the deadlock exists, reporting a deadlock detection result; if the deadlock does not exist, entering and executing S5; S5, extracting a task holding the mutual exclusion semaphore, and detecting whether the task exists a deadlock; if the deadlock does not exist, deleting the task related to the mutual exclusion semaphore, and entering and executing S6; if the deadlock exists, taking the task as a new task to be detected, and entering and executing S4; S6, repeating steps S3-S5 until all tasks in the task bitmap are detected; In step S3, the method for detecting whether any one of the tasks in the task bitmap exists a deadlock comprises: S301, taking out any one of the tasks in the task bitmap to be detected; S302, marking the task to be detected in a self-defined blocking bitmap; S303, judging whether the task to be detected is repeatedly marked; if the task to be detected is repeatedly marked, judging that the task to be detected exists a deadlock, and reporting a deadlock detection result; if the task to be detected is initially marked, judging that the task to be detected does not exist a deadlock, and entering and executing S4; In step S4, the method for obtaining other tasks waiting for the same mutual exclusion semaphore as the task to be detected and detecting whether the other tasks exist a deadlock comprises: S401, extracting a mutual exclusion semaphore waited by the task to be detected; S402, extracting other tasks waiting for the mutual exclusion semaphore in the task bitmap; S403, marking the other tasks in the blocking bitmap; S404, judging whether the other tasks are repeatedly marked; if the other tasks are repeatedly marked, judging that the other tasks exist a deadlock, and reporting a deadlock detection result; if the other tasks are initially marked, entering and executing S5.
2. The method of claim 1, wherein: In step S5, the method for extracting a task holding the mutual exclusion semaphore and detecting whether the task exists a deadlock comprises: S501, extracting a task holding the mutual exclusion semaphore; S502, extracting and judging whether the task has a waiting mutual exclusion semaphore; if the task does not have the waiting mutual exclusion semaphore, judging that the task does not exist a deadlock, entering and executing S503, if the task has the waiting mutual exclusion semaphore, taking the task as a new task to be detected, and entering and executing S4; S503, deleting the task related to the mutual exclusion semaphore in the blocking bitmap.
3. The method of claim 1 or 2, wherein: In step S1, the waiting mutual exclusion semaphore state is a permanent waiting mutual exclusion semaphore state.
4. The method of claim 3, wherein: Before step S1, an empty task bitmap and an empty blocking bitmap are defined.
Citation Information
Patent Citations
Method and device for detecting deadlock in multiple-task operating system
CN103268256A
Data concurrency competition conflict detection analysis method and system
CN114035970A