[C/C ++] multi-process: botnets and botnets
A zombie process is generated when the parent process calls fork to create a child process, and the child process runs until it is terminated. After a process is terminated, some information is useful to the parent process and kernel, such as the ID of the process, the exit status of the process, and the CPU time of the process. Therefore, after a process is terminated, the system will recycle all the memory allocated to it by the kernel, close the files opened by the process, and keep the above little information for the parent process to use, the system sends a message to the parent process.SIGCHLD
Signal, the parent process should be called in timewait
Function to collect dead parts for sub-processes and make some final work. However, if the parent process is not called in timewait
Function, the sub-process state becomesZOMBIE
, That isBotnets
.Botnets
Will occupy memory together and cannot be usedkill
Kill cleanup, unless the parent process then callswait
The function or parent process also ends, or the parent process is killed and terminated by other factors,Botnets
Will be recycled.
In the previous article [C/C ++] multi-process: The sample code used by the parent process to listen to the sub-process status wait () is modified to demonstrate zombie processes:
// If (count> CHILD_COUNT) {// change the original CHILD_COUNT to 10 if (count> 10) {FILE * fMain = freopen ("/Users/sodino/workspace/xcode/Define/main.txt", "w", stdout ); // After redirecting the standard output stream of the parent Process, add the following latency code int count = 0; while (1) {printTime (); printf ("Main Process sleep10, count = % d \ n ", count); sleep (10); if (count> 100) {break ;}}
After compilation, run. The effect is as follows:
First, you can directly view the process status during the sub-process running. You can see twoa.out
Process.
After the sub-process prints 10 lines of logs, check the Process status.zombie
Status, that isBotnets
.
Use commandskill
Drop the parent process, or wait for the parent process to executewait
Function, you can find that the sub-process has been recycled by the system.
As mentioned at the beginning of this article, the system will sendSIGCHLD
Signal, then the next article will talk about how to register the signal listening and processing functions: multi-process: semaphore listening and processing functions.