Home >Operation and Maintenance >Linux Operation and Maintenance >What are parent process and child process
In the computer field, the parent process (English: Parent Process) refers to a process that has created one or more child processes.
process 0 (i.e. PID =0 swap process, all processes except Swapper Process) are created by other processes using the system call fork. Here, the process that calls fork to create a new process is the parent process, and the corresponding The created process is a child process, so processes other than process 0 have only one parent process, but a process can have multiple child processes.
The operating system kernel uses the process identifier (Process Identifier, PID) to identify the process. Process 0 is a special process created when the system boots. After it calls fork to create a child process (that is, process 1 with PID=1, also called init), process 0 becomes a swap process (sometimes also called Idle process), and process 1 (init process) is the ancestor of all other processes in the system.
exit, a fatal error occurs during running, or When a termination signal is received), the exit status (return value) of the child process will be reported to the operating system, and the system will use the SIGCHLD signal to notify the parent process of the termination of the child process. At this time, the process control block (PCB) of the child process ) still resides in memory. Generally speaking, after receiving SIGCHLD, the parent process will use the wait system call to obtain the exit status of the child process, and then the kernel can release the PCB of the ended child process from memory; if the parent process does not do this, the child process will The PCB of the process will always reside in the memory, which means it becomes a zombie process.
Orphan processes refer to child processes that are still running after the parent process ends. In UNIX-like systems, orphan processes are generally "adopted" by the init process and become init's child processes. In order to avoid the generation of zombie processes, the method generally adopted in practical applications is:fork## The product of #. In this case, the child process starts as a copy of the parent process, and after that, depending on specific needs, the child process can chain load another process with the help of
exec calls. A program.
When a child process ends, is interrupted, or resumes execution, the kernel will send the SIGCHLD signal to its parent process. By default, the parent process will ignore it with the SIG_IGN function.
"Orphan Process" and "Zombie Process"After a child process terminates execution, if its parent process does not call
wait in advance, the kernel will continue to retain the exit status and other information of the child process so that the parent process can wait
Get it. And because in this case, although the child process has been terminated, it is still consuming system resources, so it is also called a zombie process. wait
Often called in the SIGCHLD signal processing function. Solution and Prevention
In the POSIX.1-2001 standard, the parent process can set the SIGCHLD processing function to SIG_IGN (also the default setting), or to SIGCHLD Set the SA_NOCLDWAIT flag so that the kernel can automatically reclaim the resources of terminated child processes. Since Linux 2.6 and FreeBSD 5.0, both kernels support these two methods. However, when it comes to ignoring the SIGCHLD signal, due to the long-standing differences between System V and BSD, calling wait
is still the most convenient way to recycle the resources of the derived child process.
The above is the detailed content of What are parent process and child process. For more information, please follow other related articles on the PHP Chinese website!