Add "watchdog" to the application

zhaozj2021-02-16  59

I believe most of the programmers or users, seeing the kind and warm prompt information similar to the following in Windows, will not feel unfamiliar:

"XXX executes illegal operations will be closed. To terminate the program, please click

To debug the program, please click

. "Or," Do you send an error report to Microsoft?

,

. "

If this program is running in an unattended, it is necessary to maintain a continuous working state, and the bug is difficult to exclude, it is necessary to take emergency measures, eliminate or reduce the impact of the program error. This article discusses the solution to this problem.

People who have done a certain hardware have known that bad work environments, with defective hardware design, imperfect algorithms, etc., can cause "running", so they are specially installed with a "watchdog", Responsible for monitoring the main body, if necessary, generate a reset interrupt, effectively avoiding the device.

The idea of ​​"watchdog" can be used in advanced language programming. The basic practice is to design a simple monitoring program as the main process, the original work program as the child process, by the main process promoter process and monitors the operating state of the child process. The child process does not pop up the dialog described at the beginning of this article when a serious error occurs, but is quietly exited. After the main process finds the child process, restart the child process. Such repeated.

In the specific implementation, the following is an example of VC:

Set the child process is "silent mode"

Call the API function setErrorMode in the system initialization section (beginning in CWINAPP or Main)

SETERRORMODE (SEM_NOGPFAULTERRORBOX);

Ensure that the program does not pop up the dialog box when the serious error occurs, no need to manually intervene, and exit itself.

Promotion sub-process

In the main process, create a child process and run it. The executable of the sub-process is Work.exe, and the schematic code is as follows.

Startupinfo Si;

Process_information pi;

ZeromeMory (& Pi, SizeOf (PI));

ZeromeMory (& Si, SIZEOF (Si));

Si.cb = sizeof (si);

// Start the child Process

IF (CreateProcess ("Work.exe", Null, Null, False, 0, Null, Null, & Si, & Pi)

{

// SUCCESS

...

}

CreateProcess has 10 parameters that look scary, it is not complicated, it is easy to understand. The last parameter returns information of the ID and handle of the child process, followed by monitoring the process ID or handle.

Monitor process

The timing check childhood is operating normally. There are several APIs that can be used to monitor the process of the specified ID, like getProcessVersion, getProcesstimes, getProcessiocounters, etc., where getProcessVersion is the easiest, only one parameter:

DWORD getProcessVersion (DWORD Processid);

This function returns 0 when the child process has exited.

More "professional" function is getExitcodeProcess, which can even tell us why the child process exits:

Bool getExitcodeProcess

Handle HProcess, // Handle To The Process

LPDWORD LPEXITCODE // Termination Status;

Further consideration

In order to enhance the reliability of the system, install "watchdog" to the work program, which is not a feasible technical solution. But if there is two sets of user interface, it looks a bit less professional. The child process can be designed as a Console-based application, without a user interface, all information is output through the main process window. The sixth parameters of the main process CreateProcess need to join the CREATE_NO_WINDOW item and hide the child process. This looks like a user's point of view, like only one application exists.

Another problem is that if the user closes the main process, how do I close the child process? The TerminateProcess function can end the child process, but may cause new problems such as memory leakage. Preferably, the main process emits a message to the child process and synchronizes, so that the child process can exit.

Extended, a primary process can manage multiple child processes at the same time. Typical examples use multi-blocks to capture, analyze, and process the system, which will bind each block application with a child process, while the main process is responsible for monitoring all the works of all childhoods.

The discussion above involves the process of inter-process communication (IPC) problem. There are many ways to solve, like File Mapping, Mailslot, Pipe, DDE, COM, RPC, Clipboard, Socket, WM_CopyData, etc., can be used according to personal preferences and specific conditions.

[related resources]

Kernel Studio: www.kernelstudio.com

Release Date: 2004-04-27

转载请注明原文地址:https://www.9cbs.com/read-23656.html

New Post(0)