Home  >  Article  >  Operation and Maintenance  >  What are parent process and child process

What are parent process and child process

PHP中文网
PHP中文网Original
2017-06-22 15:41:218351browse

Parent Process

In the computer field, the parent process (English: Parent Process) refers to a process that has created one or more child processes.

UNIX


##In UNIX, except for

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.

Zombie process and orphan process


When a child process ends running (usually calling

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:

  1. Set the SIGCHLD signal processing function in the parent process to SIG_IGN (ignore the signal) ;

  2. Fork twice and kill the first-level child process, making the second-level child process an orphan process and "adopted" and cleaned up by init.

Linux

In the Linux kernel, there is a very slight difference between a process and a POSIX thread, and the definition of a parent process is also different from that of UNIX . Linux has two kinds of parent processes, namely (formal) parent process and actual parent process. For a child process, its parent process is the process that receives the SIGCHLD signal when the child process ends, and the actual parent process is the process that receives the SIGCHLD signal when the child process ends. The process that actually creates the child process in the thread environment. For a normal process, the parent process and the actual parent process are the same process, but for a POSIX thread that exists in the form of a process, the parent process and the actual parent process may be different.

Child process


#In the computer field, a child process is created by another process (correspondingly called the parent process) created process. The child process inherits most of the attributes of the parent process, such as file descriptors.

produces


In Unix, the child process is usually the system call

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.

Relationship with the parent process

A process may have multiple child processes, but there can only be one at most. Parent process, and if a process does not have a parent process, it can be seen that the process is likely to be directly generated by the kernel. In Unix and Unix-like systems, the process with process ID 1 (i.e. init process) is directly created by the kernel during the system boot phase and will not terminate execution during the system running (see
Linux startup process) ; For other processes that have no parent process, they may be executed to complete various background tasks in user space.

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 the corresponding parent process ends execution, the process will become an orphan process, but it will Immediately "adopted" by the init process as its child 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 waitGet 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. waitOften 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn