1 method of Process termination:
<1> Return of the entry point function of the main thread (preferably using this method)
A thread in the <2> process calls the EXITPROCESSS function (this method should be avoided).
<3> a thread in another process calls the TerminateProcess function (this method should be avoided).
All threads in the <4> process are self-terminating (this situation almost never occurs).
1.1 Main thread entry point function return
The application should always be designed so that its process terminates only when the entry point function of the main thread returns. This is the only way to ensure that all thread resources are properly purged.
<1> C + + objects will be able to be freed using their destructors
<2> the operating system can properly release the stack memory used by the thread
The <3> system sets the exit code of the process to the return value of the entry point function
The <4> system subtracts the count of kernel objects by 1
1.2 Calling the ExitProcess function
Voidexitprocess (Uexitcode)
Compared to the main thread's entry point function return, it is less secure and does not guarantee that resources will be cleaned up.
By calling the ExitProcess function, the C++/C run-time startup code ensures that the process terminates when the main thread returns from its entry point function, regardless of whether there are other threads running in the process.
If you call ExitThread in the Enter point function, the main thread will stop running, but the process will not terminate if there are other threads in the process.
ExitProcess and ExitThread can ensure that the operating system resources are purged during function calls, but there is no guarantee that C + + runtime resources are properly purged, so it is best not to invoke these resources.
When the entry point function of the main thread is returned, the C + + runtime calls the Exit function of the runtime, and the Exit function completes the following operation
<1> Call the _onexit function to register the person with any function
<2> call destructors for all global and static C + + objects
<3> calls the system's ExitProcess function and passes the nmainretval to it. Allows the operating system to undo the process and set its exit code.
1.3 Calling the TerminateProcess function
BOOL terminateprocess (HANDLE hprocess,uintnexitcode)
This function notifies all threads in the process to terminate, and when all threads terminate, the process terminates, but it does not tell the process that the associated DLL is going to be terminated. The things it does:
<1> all open handles are closed
<2> All threads will be terminated
<3> the state of the process object becomes terminated, which satisfies all threads waiting for the process to end
The state of all thread objects in the <4> process becomes terminated, satisfying all threads that are waiting for the thread to end
The terminating state of the <5> process is changed from still_active to the return code of the process
This function is asynchronous, it tells the operating system that you want to terminate a process, but when the function returns, you cannot guarantee that the process has been killed, if you want to know exactly whether the process is killed, you can use the WaitForSingleObject function.
2 situation at the end of the process
<1> all remaining threads in the process will be terminated
<2> the user object specified in the process, the GDI object is freed, and the kernel object is closed
<3> status programming of kernel objects receive notification status
Exit code for the <4> process is changed from still_active to ExitProcess or terminateprocess
<5> process Kernel object count minus 1
The exit code for the process can be found through the function boolgetexitcodeprocess (HANDLE handle,lpdword lpexitcode), if the code is still_active indicates that the process has not been terminated, if not, It indicates that the process has been terminated.
3 Sub-processes
There are 3 ways to implement a function in a program:
<1> calling functions
<2> Open New Threads
<3> opening up new processes
3.1 Calling functions
It is very common to call a function, but its disadvantage is that it must wait for the function to complete before executing the following code because it is in the same thread
3.2 Opening new threads
This makes it possible to execute the code of other threads while the new thread executes, but the disadvantage is that there is a synchronization problem when there is a need to communicate data between different threads.
3.3 Pioneering the sub-process
This is a bit of protecting the data, executing synchronously, or waiting for the new process to execute and execute the code of the other process. The disadvantage is to open up new processes, which can result in wasted address space.