WO2019006997A1 - Program deadlock detection method, storage medium, device and system - Google Patents

Program deadlock detection method, storage medium, device and system Download PDF

Info

Publication number
WO2019006997A1
WO2019006997A1 PCT/CN2017/117366 CN2017117366W WO2019006997A1 WO 2019006997 A1 WO2019006997 A1 WO 2019006997A1 CN 2017117366 W CN2017117366 W CN 2017117366W WO 2019006997 A1 WO2019006997 A1 WO 2019006997A1
Authority
WO
WIPO (PCT)
Prior art keywords
thread
function
creation function
program
creation
Prior art date
Application number
PCT/CN2017/117366
Other languages
French (fr)
Chinese (zh)
Inventor
杨亮
陈少杰
张文明
Original Assignee
武汉斗鱼网络科技有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 武汉斗鱼网络科技有限公司 filed Critical 武汉斗鱼网络科技有限公司
Publication of WO2019006997A1 publication Critical patent/WO2019006997A1/en

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/466Transaction processing
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/48Program initiating; Program switching, e.g. by interrupt

Definitions

  • the present invention relates to the field of operating systems, and in particular, to a program deadlock detection method, a storage medium, a device, and a system.
  • An application running in the Windows operating system usually creates multiple threads to share the logic operations of its own runtime, thereby effectively improving the efficiency of the application itself.
  • the Windows operating system provides a series of thread synchronization and mutual exclusion. Methods to solve the problem of conflicts between threads, for example, signal events, critical sections, mutexes, etc. can be used for thread synchronization and mutual exclusion, but with the use of thread synchronization and mutual exclusion methods, It is the deadlock problem of threads during the running of the application.
  • Deadlock refers to a blocking imagination caused by two or more threads in the process of execution, due to competition of resources or communication between each other. Without other external forces, the thread will remain in the wait state, and the program will be in a deadlock state. These threads that are always waiting for each other are called deadlock threads. If it is not known in time that the program has a deadlock, it is aimed at the program. The effective resolution of the deadlock problem will greatly affect the subsequent operation of the program.
  • the object of the present invention is to provide a program deadlock detection method, which can timely and effectively determine whether a program has a deadlock.
  • the present invention also provides a program deadlock detection system, which can The program's deadlock is effectively detected, so that the program can be judged in a timely and effective manner.
  • the technical solution adopted by the present invention includes:
  • the resources include a memory physical address, a disk sector, a CPU, and an OS resource of an operating system.
  • S2 is specifically:
  • the hook function returns the thread handle value returned when the thread is created, the current thread corresponding to the handle creation function, and the thread ID of the current thread.
  • the signal event creation function When the signal event creation function performs signal event creation, the signal event handle value returned by the signal event creation, the current thread corresponding to the signal event creation function, and the thread ID of the current thread are obtained by the hook function;
  • the hook function When the mutex creation function creates the mutex, the hook function returns the mutex handle value returned when the mutex is created, the current thread corresponding to the mutex creation function, and the thread ID of the current thread.
  • the hook function value returned when the critical section is created, the current thread corresponding to the critical section creation function, and the thread ID of the current thread are obtained by the hook function.
  • the hook function is also used to join the wait function
  • the hook function is used to get the resources that the thread is waiting for.
  • the present invention also provides a storage medium having stored thereon a computer program that implements a program deadlock detection method when the computer program is executed by a processor.
  • the invention also provides a program deadlock detecting device, comprising a memory and a processor, wherein the memory stores a computer program running on the processor, and the processor implements the deadlock detecting method when the computer program executes the computer program .
  • the invention also provides a program deadlock detection system, comprising:
  • An injection module for injecting a hook function into a thread's thread handle creation function, a signal event creation function, a mutex creation function, and a critical section creation function;
  • the obtaining module is configured to respectively use a hook function when the thread handle creation function, the signal event creation function, the mutex creation function, or the critical section creation function respectively perform thread, signal event, mutex or critical section creation Obtaining a thread handle creation function, a signal event creation function, a current thread corresponding to the cipher creation function and the critical section creation function, and a thread ID of the current thread, and the obtaining module is further configured to acquire a resource that the current thread waits for;
  • a storage module configured to store the thread and the thread ID acquired by the acquiring module in a global field, where the storage module is further configured to store the resource owned by the acquired thread in the global field;
  • a judging module configured to take two threads from the global field, if the resource waiting for the first thread is the resource owned by the second thread, and the resource waiting for the second thread is the first thread If the resource is owned, the program will be deadlocked. If not, the program will not be deadlocked.
  • the resources include a memory physical address, a disk sector, a CPU, and an OS resource of an operating system.
  • the injection module is further configured to inject a hook function into the waiting function of the system, and when the waiting function runs, acquire the resource waiting for the thread through the hook function.
  • the present invention has the advantages of: injecting a hook function in a thread handle creation function, a signal event creation function, a mutex creation function, and a critical section creation function, respectively acquiring a thread handle creation function by a hook function
  • the signal thread creation function, the mutex creation function, and the current thread corresponding to the critical section creation function and the thread ID of the current thread thereby quickly and conveniently acquiring the resources owned by the thread and the resources waiting, facilitating timely threading.
  • the judgment of the closed-loop dependency relationship can achieve the purpose of timely learning when the program has a deadlock, and it is convenient to process the deadlock program in time.
  • FIG. 1 is a flowchart of a method for detecting a program deadlock according to an embodiment of the present invention
  • FIG. 2 is a schematic structural diagram of a program deadlock detecting apparatus according to an embodiment of the present invention.
  • an embodiment of the present invention provides a program deadlock detection method, which is used to determine whether a program has a deadlock, a thread synchronization, and a mutual exclusion when a program uses a thread synchronization and mutual exclusion mechanism to perform multi-thread operation.
  • the mechanism includes a signal event, a critical section, a mutex, and a thread handle.
  • the program runs on the Windows operating system, and the program deadlock detection method in the embodiment of the present invention is completed based on the Windows operating system. For a program that has a deadlock, there must be a loop waiting condition. For example, thread P1 is waiting for the resource occupied by one thread P2, and thread P2 is waiting for the resource occupied by one thread P1. In this case, the deadlock of the program will occur. .
  • the signal event has two states, one is the signal state, the other is the no signal state, and the thread is waiting for a signal event with no signal state to be blocked.
  • a signal event is created by calling the system's signal event creation function CreateEvent. By waiting for a signal event by calling the wait function WaitForSingleObject, the resource waiting for the current thread can be known.
  • the mutex has two states, one is the idle state, and the other is the occupied state.
  • the mutex becomes occupied. But the thread will not be blocked, but when a thread waits for a mutex in an occupied state, the thread will be blocked.
  • For the current thread create a mutex by calling the mutex creation function, and wait for a mutex through the system call wait function WaitForSingleObject to know the resources waiting for the current thread.
  • the critical section has two states, one is the entry state, and the other is the non-entry state.
  • the critical section becomes the entry state, but the thread does not. It will be blocked, but the thread will be blocked when it waits for a critical section when entering the state.
  • a critical section is created by calling the critical section creation function of the system. By waiting for a critical section by calling the wait function WaitForSingleObject, the resource waiting for the current thread can be known.
  • a thread handle when a thread is created by calling the thread handle creation function of the system, a thread handle is obtained. Before the created thread is not exited, the thread handle is signalless. When the created thread finishes running, the thread handle becomes changed. It has a signal state.
  • Resources include memory physical addresses, disk sectors, CPUs, and OS (Operating System) resources of the operating system, that is, hardware resources and software resources to be used during the running of the program.
  • OS Operating System
  • the hook function returns the thread handle value returned when the thread is created, the current thread corresponding to the handle creation function, and the thread ID of the current thread. Because the thread handle creation function CreateThread becomes the CreateThreadHook function, when the program calls the CreateThread function, it will enter the CreateThreadHook function, so through the CreateThreadHook function to get the thread handle value returned when the thread is created, the current thread corresponding to the handle creation function, and The thread ID of the current thread.
  • the signal event creation function creates a signal event
  • the hook event value returned by the signal event creation, the current thread corresponding to the signal event creation function, and the thread ID of the current thread are obtained through the hook function.
  • the signal event creation function CreateEvent becomes the CreateEventHook function
  • the program calls the CreateEvent function it will enter the CreateEventHook function, so that the signal event handle value and the signal event creation function corresponding to the signal event creation are obtained through the CreateEventHook function.
  • the current thread and the thread ID of the current thread are obtained through the CreateEventHook function.
  • the hook function returns the mutex handle value returned when the mutex is created, the current thread corresponding to the mutex creation function, and the thread ID of the current thread. Because the mutex creation function CreateMutex becomes the CreateMutexHook function, when the program calls the CreateMutex function, it will enter the CreateMutexHook function, and the CreateMutexHook function can be used to obtain the mutex handle value and mutex returned when the mutex is created. Create the current thread corresponding to the function and the thread ID of the current thread.
  • the hook function value returned when the critical section is created, the current thread corresponding to the critical section creation function, and the thread ID of the current thread are obtained by the hook function.
  • the critical section creation function InitializeCriticalSection becomes the InitializeCriticalSectionHook function
  • the program calls the InitializeCriticalSection function it will enter the InitializeCriticalSectionHook function, thereby obtaining the critical section handle value and the critical section creation function returned when the critical section is created by the InitializeCriticalSectionHook function.
  • the current thread and the thread ID of the current thread are examples of the critical section creation function.
  • the hook function is also used to join the wait function WaitForSingleObject; when the wait function WaitForSingleObject is running, the hook function is used to get the resources waiting for the thread. At the same time, the information of the resources that the acquired thread waits for is stored in the global dictionary ResourceWaitDict.
  • the resources owned by the thread and the resources waiting for are respectively belonged to the global field and the global dictionary, and are separately saved, so as to facilitate subsequent judgment on whether the program has a deadlock.
  • thread A waits for a signal event created in thread B, and thread B depends on thread A to exit to set the signal event to a signal state, ie
  • the owner thread of the resource waiting for the thread A is the thread B
  • the owner thread of the resource waiting for the thread BA is the thread A. Therefore, the closed loop dependency between the threads is detected, and the program is deadlocked.
  • the principle of a program deadlock detection method of the present invention lies in the method of injecting a hook function in a thread handle creation function, a signal event creation function, a mutex creation function, and a critical section creation function, when a thread handle creates a function, a signal
  • the event creation function, the mutex creation function, and the critical section creation function respectively create threads, signal events, mutexes, and critical sections, respectively, the thread handle creation function, signal event creation function, and mutex creation are respectively obtained by the hook function.
  • the current thread corresponding to the function and the critical section creation function and the thread ID of the current thread thereby quickly and conveniently acquiring the resources owned by the thread and the resources waiting, facilitating the timely judgment of the closed-loop dependency relationship between the threads, thereby obtaining the timely Know whether the program has a deadlock phenomenon, timely judgment, timely processing, and effectively ensure the orderly operation of the program.
  • the present invention further provides a storage medium having a computer program stored thereon, and when the computer program is executed by the processor, the steps of the program deadlock detection method described in the above embodiments are implemented.
  • the storage medium includes a U disk, a mobile hard disk, a ROM (Read-Only Memory), a RAM (Random Access Memory), a disk or an optical disk, and the like. The medium of the code.
  • the present invention further provides a program deadlock detection apparatus, including a memory and a processor, wherein the memory stores a computer program running on the processor, and the processor executes the computer program.
  • a program deadlock detection apparatus including a memory and a processor, wherein the memory stores a computer program running on the processor, and the processor executes the computer program.
  • the program deadlock detection step of each of the above embodiments is implemented.
  • the invention also discloses a program deadlock detection system based on the program deadlock detection method, which comprises an injection module, an acquisition module, a storage module and a determination module.
  • the injection module is used to inject a hook function into the thread's thread handle creation function, the signal event creation function, the mutex creation function, and the critical section creation function;
  • the acquisition module is used when the thread handle creation function, the signal event creation function, and the mutual
  • the repulsion creation function or the critical section creation function respectively creates threads, signal events, mutexes, or critical sections, respectively, the thread handle creation function, the signal event creation function, the mutexe creation function, and the criticality are respectively obtained by the hook function.
  • the current thread corresponding to the area creation function and the thread ID of the current thread is further configured to acquire the resource that the current thread waits;
  • the storage module is configured to store the thread and the thread ID acquired by the acquiring module in the global field,
  • the storage module is further configured to store the resources owned by the acquired thread in the global field;
  • the determining module is configured to take two threads from the global field, if the resource waiting for the first thread is the second thread The resources owned, and the resource waiting for the second thread is the resource owned by the first thread, then Sequence deadlock occurs, and if not, then the deadlock does not occur.
  • the resources include the physical address of the memory, the disk sector, the CPU, and the OS resources of the operating system.
  • the injection module is also used to inject a hook function into the system's wait function WaitForSingleObject.
  • WaitForSingleObject When the wait function WaitForSingleObject is running, the hook function acquires the resources that the thread waits for.
  • the principle of a program deadlock detection system of the present invention is that the injection module injects a hook function into a thread handle creation function, a signal event creation function, a mutex creation function, and a critical section creation function, when a thread handle creation function,
  • the signal event creation function, the mutex creation function, and the critical section creation function respectively acquire the thread handle creation function, the signal event creation function, and the mutex by the hook function when the thread, the signal event, the mutex, and the critical section are respectively created.
  • Create the function and the current thread corresponding to the critical section creation function and the thread ID of the current thread thereby quickly and conveniently obtaining the resources owned by the thread and the resources waiting, so that the judgment module can timely judge the closed-loop dependency relationship between the threads. Therefore, it is timely to know whether the program has a deadlock imagination, and to obtain time for the subsequent program deadlock processing, and effectively ensure the orderly operation of the program.

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Debugging And Monitoring (AREA)

Abstract

The present invention relates to the field of operating systems, and provides a program deadlock detection method, a storage medium, a device, and a system. The method comprises: S1: adding a hook function into a thread handle creation function, a signal event creation function, a mutex creation function, and a critical region creation function of a thread; S2: obtaining a current thread and a thread ID of the current thread corresponding to the thread handle creation function, the signal event creation function, the mutex creation function, and the critical region creation function by means of the hook function, respectively; S3: storing the obtained thread and thread ID, and resources owned by the obtained thread into a global field; S4: according to the thread ID in the global field, invoking a waiting function of a system to obtain the resources that each thread is waiting for in the global field; S5: selecting any two threads from the global field for deadlock determination. According to the present invention, the deadlock of a program can be effectively detected in a timely manner.

Description

程序死锁检测方法、存储介质、设备及系统Program deadlock detection method, storage medium, device and system 技术领域Technical field
本发明涉及操作系统领域,具体涉及一种程序死锁检测方法、存储介质、设备及系统。The present invention relates to the field of operating systems, and in particular, to a program deadlock detection method, a storage medium, a device, and a system.
背景技术Background technique
Windows操作系统中运行的应用程序,通常会创建多个线程来分担自身运行时的逻辑运算,从而有效提高应用程序自身的运行效率。但是,在多线程的运算处理环境下,在对某些全局数据进行读写时极易出现线程间冲突的现象,针对线程间冲突的问题,Windows操作系统提供了一系列的线程同步与互斥方法来解决线程间冲突问题,例如,信号事件、临界区、互斥体等方法都可以用来进行线程的同步和互斥,但随着线程同步与互斥方法的使用,随之带来的便是应用程序运行过程中线程的死锁问题,死锁是指两个或两个以上的线程在执行过程中,彼此之间由于资源的竞争或彼此间的通信造成的一种阻塞想象,若无其它外力作用,线程将一直处于等待状态,此时程序便处于死锁状态,这些永远处于互相等待状态的线程称为死锁线程,若无法及时得知程序发生了死锁,从而针对程序的死锁问题进行有效解决,将极大地影响程序的后续运行。An application running in the Windows operating system usually creates multiple threads to share the logic operations of its own runtime, thereby effectively improving the efficiency of the application itself. However, in the multi-threaded computing environment, it is very easy to conflict between threads when reading and writing some global data. For the conflict between threads, the Windows operating system provides a series of thread synchronization and mutual exclusion. Methods to solve the problem of conflicts between threads, for example, signal events, critical sections, mutexes, etc. can be used for thread synchronization and mutual exclusion, but with the use of thread synchronization and mutual exclusion methods, It is the deadlock problem of threads during the running of the application. Deadlock refers to a blocking imagination caused by two or more threads in the process of execution, due to competition of resources or communication between each other. Without other external forces, the thread will remain in the wait state, and the program will be in a deadlock state. These threads that are always waiting for each other are called deadlock threads. If it is not known in time that the program has a deadlock, it is aimed at the program. The effective resolution of the deadlock problem will greatly affect the subsequent operation of the program.
发明内容Summary of the invention
针对现有技术中存在的缺陷,本发明的目的在于提供一种程序死锁检测方法,能够对程序是否发生死锁进行及时有效的判断,本发明还提供一种程序死锁检测系统,能够对程序的死锁进行有效检测,从 而及时而有效的对程序是否发生死锁进行判断。Aiming at the defects existing in the prior art, the object of the present invention is to provide a program deadlock detection method, which can timely and effectively determine whether a program has a deadlock. The present invention also provides a program deadlock detection system, which can The program's deadlock is effectively detected, so that the program can be judged in a timely and effective manner.
为达到以上目的,本发明采取的技术方案是,包括:In order to achieve the above object, the technical solution adopted by the present invention includes:
S1:在线程的线程句柄创建函数、信号事件创建函数、互斥体创建函数和临界区创建函数中均加入一hook函数;S1: adding a hook function in the thread handle creation function, the signal event creation function, the mutex creation function, and the critical section creation function of the thread;
S2:当线程句柄创建函数、信号事件创建函数、互斥体创建函数或临界区创建函数分别相应地进行线程、信号事件、互斥体或临界区的创建时,通过hook函数分别获取线程句柄创建函数、信号事件创建函数、互斥体创建函数和临界区创建函数所对应的当前线程以及当前线程的线程ID;S2: When the thread handle creation function, the signal event creation function, the mutex creation function, or the critical section creation function respectively perform thread, signal event, mutex or critical section creation, respectively, the thread function is respectively obtained by the hook function. The current thread corresponding to the function, the signal event creation function, the mutex creation function, and the critical section creation function, and the thread ID of the current thread;
S3:将获取的线程、线程ID以及获取的线程所拥有的资源存储于全局字段中;S3: storing the acquired thread, the thread ID, and the resource owned by the acquired thread in the global field;
S4:根据全局字段中的线程ID,调用系统的等待函数获取全局字段中每个线程所等待的资源;S4: According to the thread ID in the global field, the calling function of the system is called to obtain the resource waiting for each thread in the global field;
S5:从全局字段中任取两个线程,若其中第一个线程所等待的资源为第二个线程所拥有的资源,且第二个线程所等待的资源为第一个线程所拥有的资源,则程序发生死锁,若否,则程序未发生死锁。S5: take two threads from the global field, if the resource waiting for the first thread is the resource owned by the second thread, and the resource waiting for the second thread is the resource owned by the first thread , the program has a deadlock, if not, the program does not deadlock.
在上述技术方案的基础上,所述资源包括内存物理地址、磁盘扇区、cpu和操作系统的OS资源。Based on the foregoing technical solutions, the resources include a memory physical address, a disk sector, a CPU, and an OS resource of an operating system.
在上述技术方案的基础上,S2具体为:Based on the above technical solutions, S2 is specifically:
当线程句柄创建函数进行线程的创建时,通过hook函数获得创建线程时返回的线程句柄值、句柄创建函数对应的当前线程以及当前线程的线程ID;When the thread handle creates a function to create a thread, the hook function returns the thread handle value returned when the thread is created, the current thread corresponding to the handle creation function, and the thread ID of the current thread.
当信号事件创建函数进行信号事件的创建时,通过hook函数获得信号事件创建时返回的信号事件句柄值、信号事件创建函数对应的当前线程以及当前线程的线程ID;When the signal event creation function performs signal event creation, the signal event handle value returned by the signal event creation, the current thread corresponding to the signal event creation function, and the thread ID of the current thread are obtained by the hook function;
当互斥体创建函数进行互斥体的创建时,通过hook函数获得创建互斥体时返回的互斥体句柄值、互斥体创建函数对应的当前线程以及当前线程的线程ID;When the mutex creation function creates the mutex, the hook function returns the mutex handle value returned when the mutex is created, the current thread corresponding to the mutex creation function, and the thread ID of the current thread.
当临界区创建函数进行临界区的创建时,通过hook函数获得创建临界区时返回的临界区句柄值、临界区创建函数对应的当前线程以及当前线程的线程ID。When the critical section creation function creates the critical section, the hook function value returned when the critical section is created, the current thread corresponding to the critical section creation function, and the thread ID of the current thread are obtained by the hook function.
在上述技术方案的基础上,Based on the above technical solutions,
所述hook函数还用于加入等待函数中;The hook function is also used to join the wait function;
当等待函数运行时,通过hook函数获取线程所等待的资源。When waiting for the function to run, the hook function is used to get the resources that the thread is waiting for.
在上述技术方案的基础上,S4中,获取的线程所等待的资源的信息存储于全局字典中。Based on the above technical solution, in S4, the information of the resources that the acquired thread waits for is stored in the global dictionary.
本发明还提供一种存储介质,该存储介质上存储有计算机程序,所述计算机程序被处理器执行时所述实现程序死锁检测方法。The present invention also provides a storage medium having stored thereon a computer program that implements a program deadlock detection method when the computer program is executed by a processor.
本发明还提供一种程序死锁检测设备,包括存储器和处理器,存储器上储存有在处理器上运行的计算机程序,所述处理器执行所述计算机程序时实现所述实现程序死锁检测方法。The invention also provides a program deadlock detecting device, comprising a memory and a processor, wherein the memory stores a computer program running on the processor, and the processor implements the deadlock detecting method when the computer program executes the computer program .
本发明还提供一种程序死锁检测系统,包括:The invention also provides a program deadlock detection system, comprising:
注入模块,其用于向线程的线程句柄创建函数、信号事件创建函数、互斥体创建函数和临界区创建函数中均注入一hook函数;An injection module for injecting a hook function into a thread's thread handle creation function, a signal event creation function, a mutex creation function, and a critical section creation function;
获取模块,其用于当线程句柄创建函数、信号事件创建函数、互斥体创建函数或临界区创建函数分别相应地进行线程、信号事件、互斥体或临界区的创建时,通过hook函数分别获取线程句柄创建函数、信号事件创建函数、互斥体创建函数和临界区创建函数所对应的当前线程以及当前线程的线程ID,所述获取模块还用于获取当前线程所等待的资源;The obtaining module is configured to respectively use a hook function when the thread handle creation function, the signal event creation function, the mutex creation function, or the critical section creation function respectively perform thread, signal event, mutex or critical section creation Obtaining a thread handle creation function, a signal event creation function, a current thread corresponding to the cipher creation function and the critical section creation function, and a thread ID of the current thread, and the obtaining module is further configured to acquire a resource that the current thread waits for;
存储模块,其用于将获取模块获取的线程和线程ID存储于全局字段中,所述存储模块还用于将获取的线程所拥有的资源存储于全局字段中;a storage module, configured to store the thread and the thread ID acquired by the acquiring module in a global field, where the storage module is further configured to store the resource owned by the acquired thread in the global field;
判断模块,其用于从全局字段中任取两个线程,若其中第一个线程所等待的资源为第二个线程所拥有的资源,且第二个线程所等待的资源为第一个线程所拥有的资源,则程序发生死锁,若否,则程序未发生死锁。a judging module, configured to take two threads from the global field, if the resource waiting for the first thread is the resource owned by the second thread, and the resource waiting for the second thread is the first thread If the resource is owned, the program will be deadlocked. If not, the program will not be deadlocked.
在上述技术方案的基础上,所述资源包括内存物理地址、磁盘扇区、cpu和操作系统的OS资源。Based on the foregoing technical solutions, the resources include a memory physical address, a disk sector, a CPU, and an OS resource of an operating system.
在上述技术方案的基础上,所述注入模块还用于向系统的等待函数中注入hook函数,当等待函数运行时,通过hook函数获取线程所等待的资源。On the basis of the above technical solution, the injection module is further configured to inject a hook function into the waiting function of the system, and when the waiting function runs, acquire the resource waiting for the thread through the hook function.
与现有技术相比,本发明的优点在于:在线程句柄创建函数、信号事件创建函数、互斥体创建函数和临界区创建函数中注入hook函数的方式,通过hook函数分别获取线程句柄创建函数、信号事件创建函数、互斥体创建函数和临界区创建函数所对应的当前线程以及当前线程的线程ID,从而快速而便捷的获取线程所拥有的资源以及所等待的资源,便于及时的对线程间闭环依赖关系的判断,达到当程序出现死锁时,能够及时得知的目的,便于及时对死锁程序进行处理。Compared with the prior art, the present invention has the advantages of: injecting a hook function in a thread handle creation function, a signal event creation function, a mutex creation function, and a critical section creation function, respectively acquiring a thread handle creation function by a hook function The signal thread creation function, the mutex creation function, and the current thread corresponding to the critical section creation function and the thread ID of the current thread, thereby quickly and conveniently acquiring the resources owned by the thread and the resources waiting, facilitating timely threading. The judgment of the closed-loop dependency relationship can achieve the purpose of timely learning when the program has a deadlock, and it is convenient to process the deadlock program in time.
附图说明DRAWINGS
图1为本发明实施例中一种程序死锁检测方法的流程图;1 is a flowchart of a method for detecting a program deadlock according to an embodiment of the present invention;
图2为本发明实施例中一种程序死锁检测设备的结构示意图。FIG. 2 is a schematic structural diagram of a program deadlock detecting apparatus according to an embodiment of the present invention.
具体实施方式Detailed ways
以下结合附图及实施例对本发明作进一步详细说明。The present invention will be further described in detail below with reference to the accompanying drawings and embodiments.
参见图1所示,本发明实施例提供一种程序死锁检测方法,用于当程序采用线程的同步和互斥机制进行多线程运行时,判断程序是否发生死锁,线程的同步和互斥机制包括信号事件、临界区、互斥体、以及线程句柄,程序运行于Windows操作系统上,且本发明实施例的程序死锁检测方法基于Windows操作系统完成。对于发生死锁的程序,必然存在一个环路等待条件,如线程P1正在等待一个线程P2占用的资源,而线程P2正在等待一个线程P1占用的资源,此种情况下便会出现程序的死锁。Referring to FIG. 1 , an embodiment of the present invention provides a program deadlock detection method, which is used to determine whether a program has a deadlock, a thread synchronization, and a mutual exclusion when a program uses a thread synchronization and mutual exclusion mechanism to perform multi-thread operation. The mechanism includes a signal event, a critical section, a mutex, and a thread handle. The program runs on the Windows operating system, and the program deadlock detection method in the embodiment of the present invention is completed based on the Windows operating system. For a program that has a deadlock, there must be a loop waiting condition. For example, thread P1 is waiting for the resource occupied by one thread P2, and thread P2 is waiting for the resource occupied by one thread P1. In this case, the deadlock of the program will occur. .
对于信号事件机制,信号事件有两种状态,一种是有信号状态,另一种是无信号状态,线程在等待一个无信号状态的信号事件是会出现阻塞的情况。对于当前的线程,通过调用系统的信号事件创建函数CreateEvent来创建一个信号事件,通过调用等待函数WaitForSingleObject来等待一个信号事件,即可获知当前线程所等待的资源。For the signal event mechanism, the signal event has two states, one is the signal state, the other is the no signal state, and the thread is waiting for a signal event with no signal state to be blocked. For the current thread, a signal event is created by calling the system's signal event creation function CreateEvent. By waiting for a signal event by calling the wait function WaitForSingleObject, the resource waiting for the current thread can be known.
对于互斥体机制,互斥体有两种状态,一种是空闲状态,另一种是被占用状态,线程在等待一个空闲状态的互斥体时,该互斥体变成被占用状态,但该线程不会被阻塞,但当一个线程等待一个被占用状态的互斥体时,该线程就会被阻塞。对于当前的线程,通过调用互斥体创建函数来创建一个互斥体,通过系统的调用等待函数WaitForSingleObject来等待一个互斥体,即可获知当前线程所等待的资源。For the mutex mechanism, the mutex has two states, one is the idle state, and the other is the occupied state. When the thread waits for a mutex of an idle state, the mutex becomes occupied. But the thread will not be blocked, but when a thread waits for a mutex in an occupied state, the thread will be blocked. For the current thread, create a mutex by calling the mutex creation function, and wait for a mutex through the system call wait function WaitForSingleObject to know the resources waiting for the current thread.
对于临界区机制,临界区有两种状态,一种是进入状态,另一种是未进入状态,线程在等待一个未进入状态的临界区时,该临界区变成进入状态,但是该线程不会被阻塞,但是线程等待一个进入状态时的临界区时,该线程就会被阻塞。对于当前的线程,通过调用系统的 临界区创建函数来创建一个临界区,通过调用等待函数WaitForSingleObject来等待一个临界区,即可获知当前线程所等待的资源。For the critical section mechanism, the critical section has two states, one is the entry state, and the other is the non-entry state. When the thread waits for a critical section that has not entered the state, the critical section becomes the entry state, but the thread does not. It will be blocked, but the thread will be blocked when it waits for a critical section when entering the state. For the current thread, a critical section is created by calling the critical section creation function of the system. By waiting for a critical section by calling the wait function WaitForSingleObject, the resource waiting for the current thread can be known.
对于线程句柄机制,通过调用系统的线程句柄创建函数创建一个线程时,会得到一个线程句柄,在创建的线程未退出之前,线程句柄是无信号状态,当创建的线程运行结束后,线程句柄变成有信号状态。For the thread handle mechanism, when a thread is created by calling the thread handle creation function of the system, a thread handle is obtained. Before the created thread is not exited, the thread handle is signalless. When the created thread finishes running, the thread handle becomes changed. It has a signal state.
资源包括内存物理地址、磁盘扇区、cpu和操作系统的OS(Operating System,操作系统)资源,即程序在运行的过程中所要运用到的硬件资源和软件资源。Resources include memory physical addresses, disk sectors, CPUs, and OS (Operating System) resources of the operating system, that is, hardware resources and software resources to be used during the running of the program.
本发明实施例的一种程序死锁检测方法具体包括:A program deadlock detection method according to an embodiment of the present invention specifically includes:
S1:在线程的线程句柄创建函数、信号事件创建函数、互斥体创建函数和临界区创建函数中均加入一hook函数(钩子函数),从而使线程句柄创建函数CreateThread变成CreateThreadHook函数,信号事件创建函数CreateEvent变成CreateEventHook函数,互斥体创建函数CreateMutex变成CreateMutexHook函数,临界区创建函数InitializeCriticalSection变成InitializeCriticalSectionHook函数。S1: Add a hook function (hook function) in the thread handle creation function, the signal event creation function, the mutex creation function and the critical section creation function of the thread, so that the thread handle creation function CreateThread becomes the CreateThreadHook function, the signal event The create function CreateEvent becomes the CreateEventHook function, the mutex creation function CreateMutex becomes the CreateMutexHook function, and the critical section creation function InitializeCriticalSection becomes the InitializeCriticalSectionHook function.
S2:当线程句柄创建函数、信号事件创建函数、互斥体创建函数或临界区创建函数分别相应地进行线程、信号事件、互斥体或临界区的创建时,通过hook函数分别获取线程句柄创建函数、信号事件创建函数、互斥体创建函数和临界区创建函数所对应的当前线程以及当前线程的线程ID。S2: When the thread handle creation function, the signal event creation function, the mutex creation function, or the critical section creation function respectively perform thread, signal event, mutex or critical section creation, respectively, the thread function is respectively obtained by the hook function. The current thread corresponding to the function, signal event creation function, mutex creation function, and critical section creation function, and the thread ID of the current thread.
具体的:specific:
当线程句柄创建函数进行线程的创建时,通过hook函数获得创建线程时返回的线程句柄值、句柄创建函数对应的当前线程以及当前线程的线程ID。因线程句柄创建函数CreateThread变成 CreateThreadHook函数,因此当程序在调用CreateThread函数时,便会进入到CreateThreadHook函数中,从而通过CreateThreadHook函数获取创建线程时返回的线程句柄值、句柄创建函数对应的当前线程以及当前线程的线程ID。When the thread handle creates a function to create a thread, the hook function returns the thread handle value returned when the thread is created, the current thread corresponding to the handle creation function, and the thread ID of the current thread. Because the thread handle creation function CreateThread becomes the CreateThreadHook function, when the program calls the CreateThread function, it will enter the CreateThreadHook function, so through the CreateThreadHook function to get the thread handle value returned when the thread is created, the current thread corresponding to the handle creation function, and The thread ID of the current thread.
当信号事件创建函数进行信号事件的创建时,通过hook函数获得信号事件创建时返回的信号事件句柄值、信号事件创建函数对应的当前线程以及当前线程的线程ID。因信号事件创建函数CreateEvent变成CreateEventHook函数,因此当程序在调用CreateEvent函数时,便会进入到CreateEventHook函数中,从而通过CreateEventHook函数获得信号事件创建时返回的信号事件句柄值、信号事件创建函数对应的当前线程以及当前线程的线程ID。When the signal event creation function creates a signal event, the hook event value returned by the signal event creation, the current thread corresponding to the signal event creation function, and the thread ID of the current thread are obtained through the hook function. Because the signal event creation function CreateEvent becomes the CreateEventHook function, when the program calls the CreateEvent function, it will enter the CreateEventHook function, so that the signal event handle value and the signal event creation function corresponding to the signal event creation are obtained through the CreateEventHook function. The current thread and the thread ID of the current thread.
当互斥体创建函数进行互斥体的创建时,通过hook函数获得创建互斥体时返回的互斥体句柄值、互斥体创建函数对应的当前线程以及当前线程的线程ID。因互斥体创建函数CreateMutex变成CreateMutexHook函数,因此当程序在调用CreateMutex函数时,便会进入到CreateMutexHook函数中,从而通过CreateMutexHook函数获取创建互斥体时返回的互斥体句柄值、互斥体创建函数对应的当前线程以及当前线程的线程ID。When the mutex creation function creates a mutex, the hook function returns the mutex handle value returned when the mutex is created, the current thread corresponding to the mutex creation function, and the thread ID of the current thread. Because the mutex creation function CreateMutex becomes the CreateMutexHook function, when the program calls the CreateMutex function, it will enter the CreateMutexHook function, and the CreateMutexHook function can be used to obtain the mutex handle value and mutex returned when the mutex is created. Create the current thread corresponding to the function and the thread ID of the current thread.
当临界区创建函数进行临界区的创建时,通过hook函数获得创建临界区时返回的临界区句柄值、临界区创建函数对应的当前线程以及当前线程的线程ID。因临界区创建函数InitializeCriticalSection变成InitializeCriticalSectionHook函数,因此当程序在调用InitializeCriticalSection函数时,便会进入到InitializeCriticalSectionHook函数中,从而通过InitializeCriticalSectionHook函数获得创建临界区时返回的临界区句 柄值、临界区创建函数对应的当前线程以及当前线程的线程ID。When the critical section creation function creates the critical section, the hook function value returned when the critical section is created, the current thread corresponding to the critical section creation function, and the thread ID of the current thread are obtained by the hook function. Because the critical section creation function InitializeCriticalSection becomes the InitializeCriticalSectionHook function, when the program calls the InitializeCriticalSection function, it will enter the InitializeCriticalSectionHook function, thereby obtaining the critical section handle value and the critical section creation function returned when the critical section is created by the InitializeCriticalSectionHook function. The current thread and the thread ID of the current thread.
S3:将获取的线程、线程ID以及获取的线程所拥有的资源存储于全局字段ResourctDict中,线程所拥有的资源为线程在运行时的当前所占有的资源;S3: storing the acquired thread, the thread ID, and the resource owned by the acquired thread in the global field ResourctDict, and the resource owned by the thread is the current resource occupied by the thread at runtime;
S4:根据全局字段中的线程ID,调用系统的等待函数WaitForSingleObject获取全局字段中每个线程所等待的资源,线程所等待的资源为线程接下来继续运行时所要使用到的资源。具体的:S4: According to the thread ID in the global field, the system wait function WaitForSingleObject is called to obtain the resource waiting for each thread in the global field, and the resource waiting for the thread is the resource to be used when the thread continues to run. specific:
hook函数还用于加入等待函数WaitForSingleObject中;当等待函数WaitForSingleObject运行时,通过hook函数获取线程所等待的资源。同时,获取的线程所等待的资源的信息存储于全局字典ResourceWaitDict中。The hook function is also used to join the wait function WaitForSingleObject; when the wait function WaitForSingleObject is running, the hook function is used to get the resources waiting for the thread. At the same time, the information of the resources that the acquired thread waits for is stored in the global dictionary ResourceWaitDict.
至此,线程所拥有的资源和所等待的资源分别分属于全局字段和全局字典中,分开保存,便于后续的对程序是否发生死锁进行判断。At this point, the resources owned by the thread and the resources waiting for are respectively belonged to the global field and the global dictionary, and are separately saved, so as to facilitate subsequent judgment on whether the program has a deadlock.
S5:从全局字段中任取两个线程,若其中第一个线程所等待的资源为第二个线程所拥有的资源,且第二个线程所等待的资源为第一个线程所拥有的资源,则程序发生死锁,有效而及时的对程序死锁现象的出现进行检测,从而便于后续的及时处理,若否,则程序未发生死锁。即任意两个线程中,第一个线程所等待的资源为第二个线程所拥有的资源,且第二个线程所等待的资源为第一个线程所拥有的资源,则程序发生死锁,若任意两线程间不存在此种关系,则程序未发生死锁。S5: take two threads from the global field, if the resource waiting for the first thread is the resource owned by the second thread, and the resource waiting for the second thread is the resource owned by the first thread Then, the program is deadlocked, and the occurrence of the program deadlock phenomenon is detected effectively and timely, thereby facilitating subsequent timely processing. If not, the program does not have a deadlock. That is, in any two threads, the resource waiting for the first thread is the resource owned by the second thread, and the resource waiting for the second thread is the resource owned by the first thread, and the program is deadlocked. If there is no such relationship between any two threads, the program does not have a deadlock.
具体举例说明,对于全局字段中的线程A和线程B,线程A等待线程B中创建的信号事件,而线程B又依赖于线程A退出才将信号事件设置为有信号状态,即For example, for thread A and thread B in the global field, thread A waits for a signal event created in thread B, and thread B depends on thread A to exit to set the signal event to a signal state, ie
ResourceDict[hThreadHandleA]=AResourceDict[hThreadHandleA]=A
ResourceDict[hEvent]=BResourceDict[hEvent]=B
并且有And there is
ResourceWaitDict[A]=hEventResourceWaitDict[A]=hEvent
ResourceWaitDict[B]=hThreadHandleAResourceWaitDict[B]=hThreadHandleA
因此可得到线程A所等待的资源的拥有者线程为线程B,线程BA所等待的资源的拥有者线程为线程A,因此便检测到线程间出现闭环的依赖关系,从而程序发生死锁现象。Therefore, the owner thread of the resource waiting for the thread A is the thread B, and the owner thread of the resource waiting for the thread BA is the thread A. Therefore, the closed loop dependency between the threads is detected, and the program is deadlocked.
本发明的一种程序死锁检测方法的原理在于,通过在线程句柄创建函数、信号事件创建函数、互斥体创建函数和临界区创建函数中注入hook函数的方式,当线程句柄创建函数、信号事件创建函数、互斥体创建函数和临界区创建函数分别进行线程、信号事件、互斥体和临界区的创建时,通过hook函数分别获取线程句柄创建函数、信号事件创建函数、互斥体创建函数和临界区创建函数所对应的当前线程以及当前线程的线程ID,从而快速而便捷的获取线程所拥有的资源以及所等待的资源,便于及时的对线程间闭环依赖关系的判断,从而及时得知程序是否出现死锁现象,及时判断,及时处理,有效保证程序的有序运行。The principle of a program deadlock detection method of the present invention lies in the method of injecting a hook function in a thread handle creation function, a signal event creation function, a mutex creation function, and a critical section creation function, when a thread handle creates a function, a signal When the event creation function, the mutex creation function, and the critical section creation function respectively create threads, signal events, mutexes, and critical sections, respectively, the thread handle creation function, signal event creation function, and mutex creation are respectively obtained by the hook function. The current thread corresponding to the function and the critical section creation function and the thread ID of the current thread, thereby quickly and conveniently acquiring the resources owned by the thread and the resources waiting, facilitating the timely judgment of the closed-loop dependency relationship between the threads, thereby obtaining the timely Know whether the program has a deadlock phenomenon, timely judgment, timely processing, and effectively ensure the orderly operation of the program.
另外,对应上述程序死锁检测方法,本发明还提供一种存储介质,存储介质上存储有计算机程序,计算机程序被处理器执行时实现上述各实施例所述的程序死锁检测方法的步骤。需要说明的是,所述存储介质包括U盘、移动硬盘、ROM(Read-Only Memory,只读存储器)、RAM(Random Access Memory,随机存取存储器)、磁碟或者光盘等各种可以存储程序代码的介质。In addition, in accordance with the above-described program deadlock detection method, the present invention further provides a storage medium having a computer program stored thereon, and when the computer program is executed by the processor, the steps of the program deadlock detection method described in the above embodiments are implemented. It should be noted that the storage medium includes a U disk, a mobile hard disk, a ROM (Read-Only Memory), a RAM (Random Access Memory), a disk or an optical disk, and the like. The medium of the code.
参见图2所示,对应上述程序死锁检测方法,本发明还提供一种程序死锁检测设备,包括存储器和处理器,存储器上储存有在处理器上运行的计算机程序,处理器执行计算机程序时实现上述各实施例的程序死锁检测步骤。Referring to FIG. 2, in accordance with the above program deadlock detection method, the present invention further provides a program deadlock detection apparatus, including a memory and a processor, wherein the memory stores a computer program running on the processor, and the processor executes the computer program. The program deadlock detection step of each of the above embodiments is implemented.
本发明还公开了一种基于上述程序死锁检测方法的程序死锁检测系统,包括注入模块、获取模块、存储模块和判断模块。The invention also discloses a program deadlock detection system based on the program deadlock detection method, which comprises an injection module, an acquisition module, a storage module and a determination module.
注入模块用于向线程的线程句柄创建函数、信号事件创建函数、互斥体创建函数和临界区创建函数中均注入一hook函数;获取模块用于当线程句柄创建函数、信号事件创建函数、互斥体创建函数或临界区创建函数分别相应地进行线程、信号事件、互斥体或临界区的创建时,通过hook函数分别获取线程句柄创建函数、信号事件创建函数、互斥体创建函数和临界区创建函数所对应的当前线程以及当前线程的线程ID,所述获取模块还用于获取当前线程所等待的资源;存储模块用于将获取模块获取的线程和线程ID存储于全局字段中,所述存储模块还用于将获取的线程所拥有的资源存储于全局字段中;判断模块用于从全局字段中任取两个线程,若其中第一个线程所等待的资源为第二个线程所拥有的资源,且第二个线程所等待的资源为第一个线程所拥有的资源,则程序发生死锁,若否,则程序未发生死锁。The injection module is used to inject a hook function into the thread's thread handle creation function, the signal event creation function, the mutex creation function, and the critical section creation function; the acquisition module is used when the thread handle creation function, the signal event creation function, and the mutual When the repulsion creation function or the critical section creation function respectively creates threads, signal events, mutexes, or critical sections, respectively, the thread handle creation function, the signal event creation function, the mutexe creation function, and the criticality are respectively obtained by the hook function. The current thread corresponding to the area creation function and the thread ID of the current thread, the obtaining module is further configured to acquire the resource that the current thread waits; the storage module is configured to store the thread and the thread ID acquired by the acquiring module in the global field, The storage module is further configured to store the resources owned by the acquired thread in the global field; the determining module is configured to take two threads from the global field, if the resource waiting for the first thread is the second thread The resources owned, and the resource waiting for the second thread is the resource owned by the first thread, then Sequence deadlock occurs, and if not, then the deadlock does not occur.
其中,资源包括内存物理地址、磁盘扇区、cpu和操作系统的OS资源。同时,注入模块还用于向系统的等待函数WaitForSingleObject中注入hook函数,当等待函数WaitForSingleObject运行时,通过hook函数获取线程所等待的资源。The resources include the physical address of the memory, the disk sector, the CPU, and the OS resources of the operating system. At the same time, the injection module is also used to inject a hook function into the system's wait function WaitForSingleObject. When the wait function WaitForSingleObject is running, the hook function acquires the resources that the thread waits for.
本发明的一种程序死锁检测系统的原理在于,注入模块向线程句柄创建函数、信号事件创建函数、互斥体创建函数和临界区创建函数中注入hook函数的方式,当线程句柄创建函数、信号事件创建函数、互斥体创建函数和临界区创建函数分别进行线程、信号事件、互斥体和临界区的创建时,通过hook函数分别获取线程句柄创建函数、信号事件创建函数、互斥体创建函数和临界区创建函数所对应的当前线程以及当前线程的线程ID,从而快速而便捷的获取线程所拥有的资 源以及所等待的资源,便于判断模块及时的对线程间闭环依赖关系的判断,从而及时得知程序是否出现死锁想象,给后续的程序死锁处理争取时间,有效保证程序的有序运行。The principle of a program deadlock detection system of the present invention is that the injection module injects a hook function into a thread handle creation function, a signal event creation function, a mutex creation function, and a critical section creation function, when a thread handle creation function, The signal event creation function, the mutex creation function, and the critical section creation function respectively acquire the thread handle creation function, the signal event creation function, and the mutex by the hook function when the thread, the signal event, the mutex, and the critical section are respectively created. Create the function and the current thread corresponding to the critical section creation function and the thread ID of the current thread, thereby quickly and conveniently obtaining the resources owned by the thread and the resources waiting, so that the judgment module can timely judge the closed-loop dependency relationship between the threads. Therefore, it is timely to know whether the program has a deadlock imagination, and to obtain time for the subsequent program deadlock processing, and effectively ensure the orderly operation of the program.
本发明不局限于上述实施方式,对于本技术领域的普通技术人员来说,在不脱离本发明原理的前提下,还可以做出若干改进和润饰,这些改进和润饰也视为本发明的保护范围之内。本说明书中未作详细描述的内容属于本领域专业技术人员公知的现有技术。The present invention is not limited to the above embodiments, and those skilled in the art can also make several improvements and retouchings without departing from the principles of the present invention. These improvements and retouchings are also considered as protection of the present invention. Within the scope. The contents not described in detail in the present specification belong to the prior art well known to those skilled in the art.

Claims (10)

  1. 一种程序死锁检测方法,用于当程序采用线程的同步和互斥机制进行多线程运行时,判断程序是否发生死锁,所述线程的同步和互斥机制包括信号事件、临界区、互斥体、以及线程句柄,所述程序运行于Windows操作系统上,其特征在于,包括:A program deadlock detection method is used for judging whether a program has a deadlock when a program uses a thread synchronization and mutual exclusion mechanism for multi-thread running, and the thread synchronization and mutual exclusion mechanism includes signal events, critical sections, and mutual The repulsion, and the thread handle, the program runs on the Windows operating system and is characterized by:
    S1:在线程的线程句柄创建函数、信号事件创建函数、互斥体创建函数和临界区创建函数中均加入一hook函数;S1: adding a hook function in the thread handle creation function, the signal event creation function, the mutex creation function, and the critical section creation function of the thread;
    S2:当线程句柄创建函数、信号事件创建函数、互斥体创建函数或临界区创建函数分别相应地进行线程、信号事件、互斥体或临界区的创建时,通过hook函数分别获取线程句柄创建函数、信号事件创建函数、互斥体创建函数和临界区创建函数所对应的当前线程以及当前线程的线程ID;S2: When the thread handle creation function, the signal event creation function, the mutex creation function, or the critical section creation function respectively perform thread, signal event, mutex or critical section creation, respectively, the thread function is respectively obtained by the hook function. The current thread corresponding to the function, the signal event creation function, the mutex creation function, and the critical section creation function, and the thread ID of the current thread;
    S3:将获取的线程、线程ID以及获取的线程所拥有的资源存储于全局字段中;S3: storing the acquired thread, the thread ID, and the resource owned by the acquired thread in the global field;
    S4:根据全局字段中的线程ID,调用系统的等待函数获取全局字段中每个线程所等待的资源;S4: According to the thread ID in the global field, the calling function of the system is called to obtain the resource waiting for each thread in the global field;
    S5:从全局字段中任取两个线程,若其中第一个线程所等待的资源为第二个线程所拥有的资源,且第二个线程所等待的资源为第一个线程所拥有的资源,则程序发生死锁,若否,则程序未发生死锁。S5: take two threads from the global field, if the resource waiting for the first thread is the resource owned by the second thread, and the resource waiting for the second thread is the resource owned by the first thread , the program has a deadlock, if not, the program does not deadlock.
  2. 如权利要求1所述的一种程序死锁检测方法,其特征在于:所述资源包括内存物理地址、磁盘扇区、cpu和操作系统的OS资源。The program deadlock detection method according to claim 1, wherein the resources include a memory physical address, a disk sector, a cpu, and an OS resource of an operating system.
  3. 如权利要求1所述的一种程序死锁检测方法,其特征在于,S2具体为:A program deadlock detection method according to claim 1, wherein S2 is specifically:
    当线程句柄创建函数进行线程的创建时,通过hook函数获得创建线程时返回的线程句柄值、句柄创建函数对应的当前线程以及当前 线程的线程ID;When the thread handle creates a function to create a thread, the hook function returns the thread handle value returned when the thread is created, the current thread corresponding to the handle creation function, and the thread ID of the current thread.
    当信号事件创建函数进行信号事件的创建时,通过hook函数获得信号事件创建时返回的信号事件句柄值、信号事件创建函数对应的当前线程以及当前线程的线程ID;When the signal event creation function performs signal event creation, the signal event handle value returned by the signal event creation, the current thread corresponding to the signal event creation function, and the thread ID of the current thread are obtained by the hook function;
    当互斥体创建函数进行互斥体的创建时,通过hook函数获得创建互斥体时返回的互斥体句柄值、互斥体创建函数对应的当前线程以及当前线程的线程ID;When the mutex creation function creates the mutex, the hook function returns the mutex handle value returned when the mutex is created, the current thread corresponding to the mutex creation function, and the thread ID of the current thread.
    当临界区创建函数进行临界区的创建时,通过hook函数获得创建临界区时返回的临界区句柄值、临界区创建函数对应的当前线程以及当前线程的线程ID。When the critical section creation function creates the critical section, the hook function value returned when the critical section is created, the current thread corresponding to the critical section creation function, and the thread ID of the current thread are obtained by the hook function.
  4. 如权利要求1所述的一种程序死锁检测方法,其特征在于:A program deadlock detection method according to claim 1, wherein:
    所述hook函数还用于加入等待函数中;The hook function is also used to join the wait function;
    当等待函数运行时,通过hook函数获取线程所等待的资源。When waiting for the function to run, the hook function is used to get the resources that the thread is waiting for.
  5. 如权利要求1所述的一种程序死锁检测方法,其特征在于:S4中,获取的线程所等待的资源的信息存储于全局字典中。The program deadlock detection method according to claim 1, wherein in S4, the information of the resource waiting for the acquired thread is stored in the global dictionary.
  6. 一种存储介质,该存储介质上存储有计算机程序,其特征在于:所述计算机程序被处理器执行时实现权利要求1至5任一项所述的方法。A storage medium having stored thereon a computer program, wherein the computer program is executed by a processor to implement the method of any one of claims 1 to 5.
  7. 一种程序死锁检测设备,包括存储器和处理器,存储器上储存有在处理器上运行的计算机程序,其特征在于:所述处理器执行所述计算机程序时实现权利要求1至5任一项所述的方法。A program deadlock detecting apparatus comprising a memory and a processor, wherein the memory stores a computer program running on the processor, wherein the processor executes the computer program to implement any one of claims 1 to 5 Said method.
  8. 一种基于权利要求1所述方法的程序死锁检测系统,其特征在于,包括:A program deadlock detection system based on the method of claim 1, comprising:
    注入模块,其用于向线程的线程句柄创建函数、信号事件创建函数、互斥体创建函数和临界区创建函数中均注入一hook函数;An injection module for injecting a hook function into a thread's thread handle creation function, a signal event creation function, a mutex creation function, and a critical section creation function;
    获取模块,其用于当线程句柄创建函数、信号事件创建函数、互斥体创建函数或临界区创建函数分别相应地进行线程、信号事件、互斥体或临界区的创建时,通过hook函数分别获取线程句柄创建函数、信号事件创建函数、互斥体创建函数和临界区创建函数所对应的当前线程以及当前线程的线程ID,所述获取模块还用于获取当前线程所等待的资源;The obtaining module is configured to respectively use a hook function when the thread handle creation function, the signal event creation function, the mutex creation function, or the critical section creation function respectively perform thread, signal event, mutex or critical section creation Obtaining a thread handle creation function, a signal event creation function, a current thread corresponding to the cipher creation function and the critical section creation function, and a thread ID of the current thread, and the obtaining module is further configured to acquire a resource that the current thread waits for;
    存储模块,其用于将获取模块获取的线程和线程ID存储于全局字段中,所述存储模块还用于将获取的线程所拥有的资源存储于全局字段中;a storage module, configured to store the thread and the thread ID acquired by the acquiring module in a global field, where the storage module is further configured to store the resource owned by the acquired thread in the global field;
    判断模块,其用于从全局字段中任取两个线程,若其中第一个线程所等待的资源为第二个线程所拥有的资源,且第二个线程所等待的资源为第一个线程所拥有的资源,则程序发生死锁,若否,则程序未发生死锁。a judging module, configured to take two threads from the global field, if the resource waiting for the first thread is the resource owned by the second thread, and the resource waiting for the second thread is the first thread If the resource is owned, the program will be deadlocked. If not, the program will not be deadlocked.
  9. 如权利要求8所述的一种程序死锁检测系统,其特征在于:所述资源包括内存物理地址、磁盘扇区、cpu和操作系统的OS资源。A program deadlock detection system according to claim 8, wherein said resources include memory physical addresses, disk sectors, cpus, and OS resources of the operating system.
  10. 如权利要求8所述的一种程序死锁检测系统,其特征在于:所述注入模块还用于向系统的等待函数中注入hook函数,当等待函数运行时,通过hook函数获取线程所等待的资源。A program deadlock detection system according to claim 8, wherein said injection module is further configured to inject a hook function into a wait function of the system, and when the wait function is running, acquire a wait by the hook function. Resources.
PCT/CN2017/117366 2017-07-01 2017-12-20 Program deadlock detection method, storage medium, device and system WO2019006997A1 (en)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN201710528372.5 2017-07-01
CN201710528372.5A CN109213576B (en) 2017-07-01 2017-07-01 Program deadlock detection method, storage medium, device and system

Publications (1)

Publication Number Publication Date
WO2019006997A1 true WO2019006997A1 (en) 2019-01-10

Family

ID=64950581

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2017/117366 WO2019006997A1 (en) 2017-07-01 2017-12-20 Program deadlock detection method, storage medium, device and system

Country Status (2)

Country Link
CN (1) CN109213576B (en)
WO (1) WO2019006997A1 (en)

Cited By (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111767155A (en) * 2020-08-06 2020-10-13 腾讯科技(深圳)有限公司 Deadlock detection method, device, equipment and computer readable storage medium
CN111858075A (en) * 2020-06-30 2020-10-30 广东浪潮大数据研究有限公司 Deadlock detection method, system, equipment and readable storage medium
CN112199203A (en) * 2020-11-16 2021-01-08 奥士康科技股份有限公司 Method and system for solving deadlock caused by multi-table operation
CN114840847A (en) * 2021-02-02 2022-08-02 武汉斗鱼鱼乐网络科技有限公司 Method, device, medium and equipment for safely creating thread in target process

Families Citing this family (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110837465A (en) * 2019-10-15 2020-02-25 珠海金山网络游戏科技有限公司 Android-based handle leakage detection method and system
CN111831557B (en) * 2020-06-19 2023-10-20 北京华三通信技术有限公司 Deadlock detection method and device
CN114035970B (en) * 2022-01-10 2022-04-22 南京云信达科技有限公司 Data concurrent competition conflict detection analysis method and system

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103761182A (en) * 2013-12-26 2014-04-30 上海华为技术有限公司 Method and device for deadlock detection
JP2014119964A (en) * 2012-12-17 2014-06-30 Hitachi Ltd Computer system and program
CN106681811A (en) * 2016-12-08 2017-05-17 腾讯科技(深圳)有限公司 Multi-thread scheduling method and device based on thread pool

Family Cites Families (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN102222015B (en) * 2010-04-13 2016-04-27 三星电子(中国)研发中心 Detect the method and system of the deadlock in multithread programs
CN102073588B (en) * 2010-12-28 2013-11-20 北京邮电大学 Code static analysis based multithread deadlock detection method and system
CN102567096B (en) * 2011-12-30 2017-02-08 中国科学院软件研究所 Mutual-exclusion semaphore management method for preventing deadlock under multi-task environment
CN103246552B (en) * 2012-02-14 2018-03-09 腾讯科技(深圳)有限公司 Prevent thread from the method and apparatus blocked occur
US9477576B2 (en) * 2013-04-11 2016-10-25 International Business Machines Corporation Using application state data and additional code to resolve deadlocks
CN105511969B (en) * 2015-11-25 2020-05-19 中国船舶工业系统工程研究院 Method for mutual exclusion between cross-process threads

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
JP2014119964A (en) * 2012-12-17 2014-06-30 Hitachi Ltd Computer system and program
CN103761182A (en) * 2013-12-26 2014-04-30 上海华为技术有限公司 Method and device for deadlock detection
CN106681811A (en) * 2016-12-08 2017-05-17 腾讯科技(深圳)有限公司 Multi-thread scheduling method and device based on thread pool

Cited By (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111858075A (en) * 2020-06-30 2020-10-30 广东浪潮大数据研究有限公司 Deadlock detection method, system, equipment and readable storage medium
CN111767155A (en) * 2020-08-06 2020-10-13 腾讯科技(深圳)有限公司 Deadlock detection method, device, equipment and computer readable storage medium
CN112199203A (en) * 2020-11-16 2021-01-08 奥士康科技股份有限公司 Method and system for solving deadlock caused by multi-table operation
CN114840847A (en) * 2021-02-02 2022-08-02 武汉斗鱼鱼乐网络科技有限公司 Method, device, medium and equipment for safely creating thread in target process

Also Published As

Publication number Publication date
CN109213576A (en) 2019-01-15
CN109213576B (en) 2022-04-08

Similar Documents

Publication Publication Date Title
WO2019006997A1 (en) Program deadlock detection method, storage medium, device and system
US8176489B2 (en) Use of rollback RCU with read-side modifications to RCU-protected data structures
KR101835250B1 (en) Detection of unauthorized memory modification and access using transactional memory
EP2972885B1 (en) Memory object reference count management with improved scalability
US8924655B2 (en) In-kernel SRCU implementation with reduced OS jitter
US10915424B2 (en) Defeating deadlocks in production software
US8495638B2 (en) Component-specific disclaimable locks
KR101970390B1 (en) Lock elision with binary translation based processors
US9501237B2 (en) Automatic mutual exclusion
US8769546B2 (en) Busy-wait time for threads
RU2746155C2 (en) Destroying an object based on the sequence of actions performed
US9207967B2 (en) Using nonspeculative operations for lock elision
US8495642B2 (en) Mechanism for priority inheritance for read/write locks
US8135690B2 (en) Concurrency object classification
US8893137B2 (en) Transaction-based shared memory protection for high availability environments
US10346196B2 (en) Techniques for enhancing progress for hardware transactional memory
US10983839B2 (en) Method, apparatus, and electronic device for improving CPU performance
WO2019047376A1 (en) Interface task calling method and device, electronic device and readable storage medium
US20160320984A1 (en) Information processing device, parallel processing program and method for accessing shared memory
US9507817B2 (en) Method for synchronizing access to shared resources of a computing system and detecting and eliminating deadlocks using lock files
KR20200077297A (en) Method and Apparatus for detecting atomicity violation for shared memory used in multi-process/multi-thread
US9928072B1 (en) Detecting and recording atomic execution
de Oliveira et al. Nested locks in the lock implementation: The real-time read-write semaphores on linux
KR101012669B1 (en) Malicious program detector for scanning a illegal memory access and method thereof
Attiya et al. Highly-concurrent multi-word synchronization

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 17917062

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 17917062

Country of ref document: EP

Kind code of ref document: A1