Automatic resurrection of abnormal death process
First, the problem is produced
We have such experiences, and applications running on Windows often terminate, and it is necessary to start from manual re-start. If the computer is unmanned, the abnormality terminated process cannot be started in real time, it may cause loss to production.
When I developed the GPS Global Satellite Positioning System Control Center program, I encountered an abnormality of the control center program to terminate death, thereby identifying a method of automatically resurrecting a process for reference.
Second, related knowledge
Typically, a process for one running instance of an application is called, which can also contain multiple possible paths in one process, each execution path called a thread, and one process contains at least one main thread. The main thread is responsible for performing the startup code. Alternatively, a process can create a number of sub-processes. When the process is created, the system automatically generates the main thread, the main thread can then create more threads.
We can write a program that makes it created, promoting the child process, and monitors the operation of the process, and then recreate the child process immediately when it appears anomalous termination.
Third, related functions
1. Create a sub-process function:
Bool createprocess
LPCTSTR LPAPPLICATIONNAME,
LPTSTR LPCOMMANDLINE,
LPSecurity_attributes LPPROCESSATTRIBUTES,
LPSecurity_attributes LPTHREADATIADATTRIBUTES,
Bool binherithandles,
DWORD DWCREATIONFLAGS,
LPVOID LPENVIRONMENT,
LPCTSTR LPCURRENTDIRECTORY,
LPStartupinfo LPstartupinfo,
LPPROCESS_INFORMATION LPPROCESSINFORMATION
);
Parameter Description:
LPApplicationName: The new process will use the name of the executable of the executable, must contain the extension.
LPCommandline: The command line of the new process. If lpapplicationname is null, the first parameter of LPCommandline is the name of the executable executable of the new process, which does not include the extension, and the system assumes an exe file.
LPProcessAttributes and LPTHREADATTRIBUTES: Safety properties specified by the process object and thread objects, respectively.
BinheritHandles: Specifies whether the process inherits the handle in its parent process.
DWCREATIONFLAGS: Specifies the logo of the new process generation method, which can be connected with the logical operator OR.
LpenVironment: Points a piece of memory that contains the environment block string to be used with the new process, typically null, which makes a set of environment blocks in the child process.
LpCurrentDirectory: Sets the current drives and working directories of the sub-process, for null, child process inherits the current drive and work directory of the Parent process.
LPSTARTUPINFO: Points to STARTUPINFO. Generally, the child process uses the default value. However, to initialize all members in this structure to 0, and set CB as a structure size.
The STARTUPINFO structure is as follows:
Typedef struct _startupinfo {
DWORD CB;
LPTSTR LPRESERVED;
LPTSTR LPDESKTOP;
LPTSTR LPTITLE;
DWORD DWX;
DWORD DWY;
DWORD DWXSIZE;
DWORD DWYSIZE;
DWORD DWXCOUNTCHARS;
DWORD DWYCOUNTCHARS;
DWORD DWFILLATTRIBUTE;
DWORD DWFLAGS; Word WshowWindow;
Word cbreserved2;
LPBYTE LPRESERVED2;
Handle hstdinput;
Handle hstdoutput;
Handle hstderror;
} Startupinfo, * lpstartupinfo;
The LPProcessInformation parameter points to the LPPROCESS_INFORMATION structure, CreateProcess fills in the information of the sub-process before returning, and the parent process is using this information to monitor the child process. This structure is as follows:
Typedef struct _process_information {
Handle HPROCESS;
Handle hthread;
DWORD DWPROCESSID;
DWORD DWTHREADID;
} Process_information;
HProcess and Hthread are the handles of the sub-process's handle and sub-process's main thread, DWProcessID and DWTHREADID are identification numbers of identification number of child process identification numbers and sub-process.
2, child process termination detection function
GetExitcodeProcess (Handle Hprocess, LPDWORD LPEXITCODE);
HProcess: process handle, LPEXITCODE: Exit code when the process terminates.
If a process is not terminated, the return value of LPEXITCODE is STILL_ACTIVE, otherwise it returns other values.
Fourth, the method of Delphi5 language implementation
1. Create a new project Project1
Select File, New Application. Place a MEMO component on form FORM1, a OK button component, change the CATION property of the OK button component to CreateProcess. Place another TIMER component. Set the interval value of the Timer component to 1000, check whether the process is terminated every second.
2, define a process after the type of the unit1 USE section
PROCEDURE ESTABLISHPROCESS;
Define a variable after Var of Unit1 Use:
PiProcinfogps: Process_information;
3. The implementation code for writing the ESTABLISHPROCESS process in the Unit1 Implementation section is as follows:
PROCEDURE ESTABLISHPROCESS;
VAR
Sistartupinfo: startupinfo;
Saprocess, Sathread: Security_Attribute;
Fsuccess: boolean;
Begin
Fsuccess: = FALSE;
ZeromeMory (@ sistartupinfo, sizeof (sistartupinfo);
Sistartupinfo.cb: = sizeof (sistartupinfo);
Saprocess.nlength: = sizeof (saprocess);
Saprocess.lpsecurityDescriptor: = pchar (nil);
Saprocess.binherithandle: = true;
SATHREAD.NLENGTH: = SIZEOF (Sathread);
Sathread.lpsecurityDescriptor: = pchar (nil);
Sathread.binherithandle: = true;
Fsuccess: = CreateProcess (Pchar (NIL), 'C: / SR350 / SR350Buff', @ saprocess, @ Sathread, False,
CREATE_DEFAULT_ERROR_MODE, PCHAR (NIL), PCHAR (NIL), SISTARTUPINFO, PIPROCICINFOGPS; IF (Not Fsuccess) THEN
Form1.Memo1.Lines.Add ('Create Process SR350Buff Fail.')
Else
Form1.Memo1.Lines.Add ('Create Process SR350Buff Success.')
END;
4, call the process in the onClick event of the CreateProcess button
ESTABLISHPROCESS;
5. Write code for Timer1's Ontimer event:
Procedure TFORM1.TIMER1TIMER (Sender: TOBJECT);
VAR
DWEXITCODE: DWORD;
FPROCESSEXIT: BOOLEAN;
Begin
DWEXITCODE: = 0;
FPROCESSEXIT: = FALSE;
FPROCESSEXIT: = GETEXITCODEPROCESS (Piprocinfogps.hprocess, dwexitcode);
IF (fProcessExit and (dwexitcode <> still_active) ") THEN
Begin
Memo1.Lines.Add ('sr350buff.exe process termination ");
CloseHandle (piprocinfogps.hthread);
CloseHandle (piprocinfogps.hprocess);
ESTABLISHPROCESS;
END;
END;
6. Set the executable file name C: /SR350/sr350buff.exe, so the C: disk / sr350 directory requires a SR350Buff.exe file.
7, compile join, run Project1, click CreateProcess, see c: /sr350/sr350buff.exe startup. Turn off the SR350Buff.exe process, see SR350Buff.exe automatically starts.