We can see the heartbeat protection function of the application from the csdn development expert magazine. It is useful. Currently, you have tried the project. In the trial process, I also have a deeper understanding of this function. Here, I will record my understanding of this Part and hope to help later users.
Functions and usage of the heartbeat Protection Program
Functions of the heartbeat Protection ProgramThe heartbeat protection program is used to implement self-maintenance of unattended programs, so that the protected programs can be automatically restarted when they crash due to an exception, meeting the unattended environment requirements. Heartbeat protection is applicable to scenarios that require unattended and long-term operation, but may crash due to uncertainties. Note: This program is not used to protect against poor code quality.
Composition of Heartbeat Protection ProgramThe heartbeat Protection Program consists of two parts. They are used for heart beat and recovery respectively. The first part is the heartbeat counting module, which needs to be implanted into the protection program and serves as the heart of the program. When the program runs normally, the heartbeat count is also normal. This module is released as a dynamic library. Provides two interfaces: Heartbeat start and heartbeat stop. The second part is the heartbeat recovery module. Started by the heartbeat counting module, this module monitors the running status of the protected program. When an exception occurs to the protected program, it deletes the existing process, restarts the protected program, and continues the heartbeat. This module exists as an application.
Principle of Heartbeat Protection ProgramThe core of the heartbeat Protection Program is to create a semaphore between the heart beat and recovery modules. The heartbeats module is responsible for adding value to semaphores, and the cardiac recovery module is responsible for reducing semaphores. Increase or decrease values use the same frequency (once per second ). When the heart beats normally, the semaphore value remains unchanged. If the heart beats abnormally (such as program crashes), the semaphore does not increase. Through impairment, the cardiac recovery module can detect that the semaphore is 0. In this case, it is deemed that the protected program has encountered a problem. Delete the existing process of the protected program (by the process name) and restart the new program. In this way, you can maintain the continuous operation of the protected program.
Use of Heartbeat Protection ProgramThe heartbeat count module needs to be introduced into the protected program. The heartbeat. dll module is a dynamic library. Heartbeat. Lib must be included in the protected program and heartbeat. h file must be introduced. This dynamic library contains two interface functions: Start heartbeat function: startheartbeat (char * pszprogamname, int ntimeout) This function is used to start heartbeat count. It must be called when the protection program starts. Generally, it can be in the oninitinstance function. The first parameter is the full path of the protected program, which can be obtained using the getmodulefilename function. The second parameter is the timeout time, that is, the time when the protected program does not count. this parameter is considered abnormal, in seconds. Terminate heartbeat function: stopheartbeat ()
This function is used to terminate the protected program normally and can be called in the onexitinstance function. During normal termination, the heartbeat recovery process is terminated.