In linux, waitpid system calls-Linux general technology-Linux programming and kernel information. The following is a detailed description. Original: zhangzhiyin88
1. You can make a small summary of the life of a process using the image metaphor:
With a fork statement, a new process is born, but it is only 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 // Define the pid_t type
# Include
Pid_t waitpid (pid_tpid, int * status, intoptions );
In essence, the functions of the system call waitpid and wait are exactly the same, but the waitpid has two more user-controlled parameters pid and options, this provides us with a more flexible way of programming. The following two parameters are described in detail:
Pid
From the parameter name pid and type pid_t, we can see that what is needed here is a process ID. But when the pid gets different values, it has different meanings here.
1. When the pid is greater than 0, only wait for the child process whose process ID is equal to the pid, 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 is <-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.
Options
Options 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 you do not want to use them, you can set options to 0, for example:
Ret = waitpid (-1, NULL, 0 );
If the WNOHANG parameter is used to call waitpid, even if no sub-process exits, it will return immediately and will not wait forever like wait.
The WUNTRACED parameter is used for tracking and debugging and is rarely used.
Check the linux source code unistd. h and we will find that wait is the packaged waitpid:
The return value of waitpid is slightly more complex than that of wait. There are three cases:
1. When the returned result is normal, waitpid returns the ID of the collected sub-process;
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 during the call,-1 is returned. errno is set to a value to indicate the error;
When the sub-process indicated by the pid does not exist or the process exists but is not a sub-process that calls the process, waitpid will return an error, and errno is set to ECHILD.
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