Linux Process (fork, waitpid)

Source: Internet
Author: User
For the life of a process, we can make a small summary using the image metaphor: with a fork, a new process is born, but it is just a clone of the old process. Then, with exec, the new process was reborn and independent from home, and started a career serving the people. The process is the same if a person is dead, but the process is also the same. It can be the last "}" that runs to the main function, leaving us with ease. It can also be suicide, there are two ways to commit suicide. One is to call the exit function, and the other is to use return in the main function. Either way, it can leave a suicide note, stored in the return value; it can even be murdered and ended by other processes in other ways. After the process dies, a zombie will be left behind. Wait and waitpid act as the zombie and push the zombie to be credened, making it invisible. In Linux, wait system calling introduces one of the zombie wait systems. The following describes another waitpid, which seems more complex. Waitpid function prototype: # include <sys/types. h>/* provide the definition of type pid_t */# include <sys/Wait. h> pid_twaitpid (pid_tpid, int * status, intoptions); essentially, the function of the system to call waitpid is the same as that of wait, however, waitpid has two more user-controlled parameters PID and options, thus providing another more flexible way for programming. Here we will introduce these two parameters in detail: PID from the parameter name PID and type pid_t, we can see that here we need a process ID. But when the PID gets different values, it has different meanings here. 1. when the PID is greater than 0, only the child process whose process ID is equal to the PID is waiting. No matter how many other child processes have ended and exited, as long as the specified child process has not ended, waitpid will keep waiting. 2. When pid =-1, wait for any sub-process to exit without any restrictions. At this time, waitpid and wait play the same role. 3. When pid = 0, wait for any sub-process in the same process group. If the sub-process has already been added to another process group, waitpid will not ignore it. 4. When PID <-1, wait for any sub-process in a specified process group. the ID of this process group is equal to the absolute value of PID. Optionsoptions provides some additional options to control waitpid. Currently, only the wnohang and wuntraced options are supported in Linux. These two constants can be connected using the '|' operator, for example: ret = waitpid (-1, null, wnohang | wuntraced); if we don't want to use them, we can also set options to 0, for example: ret = waitpid (-1, null, 0); If the wnohang parameter is used to call waitpid, it will return immediately even if no sub-process exits and will not wait forever like wait. The wuntraced parameter is used for tracking and debugging and is rarely used. View Linux Source Code Unistd. H we will find that wait is actually the packaged waitpid: staticinlinepid_twait (int * wait_stat) {returnwaitpid (-1, wait_stat, 0);} The waitpid return value is slightly more complex than wait, there are three cases: 1. when the returned result is normal, waitpid returns the ID of the sub-process collected; 2. if the wnohang option is set, and waitpid in the call finds that no child process has exited to collect data, 0 is returned; 3. if an error occurs in the call,-1 is returned, and errno is set to the corresponding value to indicate the error. When the sub-process indicated by the PID does not exist, or the process exists, but not the sub-process that calls the process, waitpid will return an error, and errno will be set to echild. The following is a simple example: Download: waitpid. c/* waitpid. C */# include <sys/types. h> # include <sys/W AIT. h> # include <unistd. h> # include <stdio. h> int main () {pid_t PC, PR; Pc = fork (); If (Pc <0) /* fork Error */{printf ("fork error \ n"); exit (1);} else if (Pc = 0) /* The wnohang parameter is used in the sub-process */{sleep (10); exit (0);} else {do, waitpid will not wait here */PR = waitpid (PC, null, wnohang); If (Pr = 0) {printf ("No child exit \ n "); sleep (1) ;}}while (Pr = 0); If (Pr = pc) printf ("successfully get child % d \ n", Pr ); elseprintf ("Wait child error \ n");} return 0;} compile Run: $ gcc-O waitpid. C $. /waitpidno child exitno child exitsuccessfully get child 4607 after 10 failed attempts by the parent process, finally, the sub-processes that exit are collected. The parent process and child process sleep for 10 seconds and 1 second respectively, which means they work for 10 seconds and 1 second respectively. Both parent and child processes have work to do. The parent process uses the short interval of work to check whether the child process exits. If the child process exits, it is collected.

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.