Parent and child processes

Source: Internet
Author: User
Tags posix

Parent Process

In the computer realm, the parent process ( English:parentprocesses) refers to the process in which one or more child processes have been created.

UNIX

In UNIX , all processes except process 0(that is, the pid=0 exchange process,swapperprocess) are created by other processes using the system call fork. The process that calls fork to create a new process is the parent process, and the process that is created for it is a child process, so that only processes other than process 0 have one parent process, but a process can have more than one child process.

The operating system kernel identifies the process with a process identifier (Identifier, or PID). Process 0 is a special process created at system boot time, when it calls fork to create a child process (that is, pid=1 process 1, also known as Init), process 0 becomes the exchange process (sometimes called the Idle process ), and Process 1 (the INIT process) is the ancestor of all the other processes in the system.

Zombie process with orphan process

When a child process finishes running (typically called exit, a fatal error at run time, or a terminating signal is received), the exit status (return value) of the child process is returned to the operating system, and the system notifies the parent process of the event that the child process was closed with a sigchld signal. The Process Control block (PCB) of the child process still resides in memory at this time. In general, when SIGCHLD is received, the parent process uses the wait system call to get the exit status of the child process, and then the kernel can release the PCB of the finished child process from memory, and if the parent process does not do so, the child process's PCB will always reside in memory and become a zombie process.

The orphan process is a child process that is still running after the parent process is finished. In Unix-like systems, orphan processes are generally "adopted" by the Init process and become the children of the INIT process.

In order to avoid a zombie process, the actual application generally takes the following approach:

    1. Sets the handler function for the SIGCHLD signal in the parent process to sig_ign (ignoring the signal);

    2. Fork two times and kill a sub-process, making the two-level subprocess an orphan process and being "adopted" and cleaned up by Init.

Linux

In the Linux kernel, there is a very small difference between a process and a POSIX thread, and the definition of a parent process is different from UNIX. Linux has two parent processes, called (form) the parent process and the actual parent process, and 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 normal processes, the parent process is the same process as the actual parent process, but for a POSIX thread that exists as a process, the parent process and the actual parent process may be different.

Child Process

In the computer realm, a child process is a process created by another process, which corresponds to a process called a parent. The child process inherits most of the properties of the parent process, such as the file descriptor.

Produce

In Unix, a child process is usually fork the product of a system call. In this case, the child process starts out as a copy of the parent process, and after that, the child process can chain through the exec call to load the other program, depending on the needs.

relationship to the parent process

A process may subordinate more than one child process, but there can be at most 1 parent processes, 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, a process with a process ID of 1 (that is, the Init process) is created directly by the kernel during the boot phase of the system and does not terminate execution while the system is running (see the Linux boot process ), and for other processes that have no parent process, It is possible to perform various background tasks in the user space.

When a child process finishes, breaks, or resumes execution, the kernel sends a SIGCHLD signal to its parent process. By default, the parent process is ignored by the Sig_ign function.

"Orphan process" and "Zombie process "

After the corresponding parent process finishes executing, the process becomes an orphan process, but it is then "adopted" by the Init process as its child process.

After a child process terminates execution, the kernel persists information such as the exit state of the child process if its parent process has not been called in advance, so that the wait parent process can wait obtain it. And because, in this case, the child process is terminated but still consumes system resources, it is also known as a zombie process. wait is often called in the handler function of the SIGCHLD signal.

Solutions and Prevention

In the POSIX.1-2001 standard specification, the parent process can set SIGCHLD's handler function to Sig_ign (also default), or set the sa_nocldwait tag for sigchld so that the kernel can automatically reclaim the resources of the terminated child process. Both of these are supported by both kernels since Linux 2.6 and FreeBSD 5.0. However, with the long-standing difference between System V and BSD, the invocation is wait still the most convenient way to reclaim the resources of the derived child processes, ignoring the SIGCHLD signal.

Our public number

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/99/8E/wKiom1lJzODRKTp0AACeGDQ6kF0241.jpg "title=" Wg2weima.jpg "alt=" Wkiom1ljzodrktp0aacegdq6kf0241.jpg "/>

Parent and child processes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.