Parent process and child process, parent process child process

Source: Internet
Author: User

Parent process and child process, parent process child process
A Parent Process is in the computer field. A Parent Process (Parent Process) refers to a Process that has created one or more child processes. UNIX

In UNIX, except for the process 0 (that is, the PID = 0 exchange process,Swapper ProcessAll other processes 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, the process created for it is a child process. Therefore, all processes except process 0 have only one parent process, but one 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 during system boot. After it calls fork to create a sub-process (process 1 with PID = 1, also known as init, process 0 is converted to the exchange process (sometimes called the idle process), and process 1 (init process) is the ancestor of all other processes in the system.

Zombie and orphan Processes

When a sub-process stops running (usually caused by calling exit, a fatal error or receiving a termination signal), the exit status (return value) of the sub-process is returned to the operating system, the system sends a SIGCHLD signal to the parent process to notify the child process of the ending event. At this time, the process control block (PCB) of the child process remains in the memory. Generally, after receiving SIGCHLD, the parent process uses the wait system call to obtain the exit status of the child process, and then the kernel can release the pcb of the child process from the memory; if the parent process does not, the child process's PCB will remain in the memory, that is, it will become a zombie process.

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

To avoid zombie processes, the following methods are generally used in actual applications:

Linux

In the Linux kernel, the process and POSIX Threads are quite slightly different, and the definition of the parent process is also different from that of UNIX. Linux has two parent processes, namely the (form) parent process and the actual parent process. For a child process, the parent process receives the SIGCHLD signal at the end of the Child process, the actual parent process is the process that actually creates the child process in the multi-threaded environment. For a common process, the parent process and the actual parent process are the same process, but for a POSIX thread in the form of a process, the parent process and the actual parent process may be different.

 

In the computer field, a child process is created by another process (corresponding to the parent process. Child processes inherit most of the attributes of the parent process, such as file descriptors. Generate

In Unix, sub-processes are generally called by the system.fork. In this case, the child process is a copy of the parent process at the beginning.ExecCall to chain and load another program.

Relationship with parent process

A process may have multiple sub-processes, but only one parent process can exist. If a process does not have a parent process, it is likely that the process is directly generated by the kernel. In Unix and Unix-like systems, processes with a process ID of 1 (that is, the init process) are directly created by the kernel in the system boot phase, the execution will not be terminated during system running (see the Linux Startup Process). Other processes without a parent process may be executed to complete various background tasks in the user space.

When a sub-process ends, interrupts, or resumes execution, the kernel sends a SIGCHLD signal to its parent process. By default, the parent process is ignored using the SIG_IGN function.

Orphan process and zombie Process"

After the execution of the corresponding parent process ends, the process will become an orphan process, but then the init process will immediately "adopt" as its child process.

After a sub-process is terminated, if its parent process does not callwaitThe kernel keeps the information such as the exit status of the sub-process so that the parent process canwait. In this case, the sub-process is terminated but still consumes system resources. Therefore, it is also called a zombie process.WaitIt is often called in the processing function of SIGCHLD signal.

Solution and Prevention

In the POSIX.1-2001 standard, the parent process can set SIGCHLD's handler to SIG_IGN (also the default) or set SA_NOCLDWAIT tag for SIGCHLD, so that the kernel can automatically Recycle resources of terminated sub-processes. Since Linux 2.6 and FreeBSD 5.0, both kernels support these two methods. However, to ignore the SIGCHLD signal, because of the long-standing difference between System V and BSD, to reclaim the resources of the derived sub-process, callwaitIt is still the most convenient method.

 

Our public account

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.