Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Parent and child processes

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Parent process

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

UNIX

In UNIX, all processes except process 0 (the exchange process of PID=0, 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 process created for it is the child process, so all processes except process 0 have only one parent process, but a process can have multiple child processes.

The operating system kernel identifies processes by process identifiers (Process Identifier, or PID). Process 0 is a special process created when the system boots. After it calls fork to create a child process (process 1 of PID=1, also known as init), process 0 becomes an exchange process (sometimes 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 caused by calling exit, a fatal error at runtime, or receiving a termination signal), the exit status (return value) of the child process is returned to the operating system, and the SIGCHLD signal is used to inform the parent process that the child process is terminated, and the process control block (PCB) of the child process still resides in memory. Generally speaking, after receiving the 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 terminated child process from memory. If the parent process does not do so, the PCB of the child process will always reside in memory, that is, the zombie process.

An orphan process is a child process that is still running after the end of the parent process. In UNIX-like systems, orphan processes are generally "adopted" by init processes and become child processes of init.

In order to avoid the generation of zombie processes, the general methods adopted in practical applications are:

Set the processing function of the SIGCHLD signal in the parent process to SIG_IGN (ignore signal)

Fork killed the first-level child process twice, so that the second-level child process became an orphan process and was "adopted" and cleaned up by init.

Linux

In the Linux kernel, there is a fairly slight difference between a process and a POSIX thread, and the definition of a parent process is not the same as that of UNIX. Linux has two kinds of parent processes, which are called (formal) parent process and actual parent process. For a child process, the parent process is the process that receives the SIGCHLD signal at the end of the child process, while the actual parent process is the process that actually creates the child process in a multithreaded environment. For a normal process, the parent process and the actual parent process are the same process, but for a POSIX thread that exists as a process, the parent process and the actual parent process may not be the same.

Child process

In the computer world, a child process is a process created by another process (corresponding to a parent process). The child process inherits most of the properties of the parent process, such as file descriptors.

Produce

In Unix, a child process is usually the product of a system call to fork. In this case, the child process is initially a copy of the parent process, and after that, according to the specific needs, the child process can chain load another program with the help of exec call.

Relationship with the parent process

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

When a child process ends, interrupts, or resumes execution, the kernel sends SIGCHLD signals to its parent process. By default, the parent process ignores it with the SIG_IGN function.

"orphan process" and "zombie process"

After the corresponding parent process finishes execution, the process becomes an orphan process, but is immediately "adopted" by the init process as its child process.

After a child process terminates execution, if the parent process does not call wait in advance, the kernel will keep the exit status of the child process and other information, so that the parent process can obtain it by wait. 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 is often called in the handlers of SIGCHLD signals.

Solution and prevention

In the POSIX.1-2001 standard, the parent process can set the handler function of SIGCHLD to SIG_IGN (which is also the default setting), or set the SA_NOCLDWAIT flag for SIGCHLD so that the kernel can automatically reclaim the resources of terminated child processes. Since Linux 2.6 and FreeBSD 5.0, both kernels have supported both approaches. However, on the problem of ignoring SIGCHLD signals, because of the long-standing difference between System V and BSD, calling wait is still the most convenient way to recover the resources of derived child processes.

Our public account

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report