The difference between wait and waitpid in Linux

Source: Internet
Author: User
Tags exit continue

Zombie does not take up memory and does not occupy the CPU, on the surface we can not care about their existence, however, in fact, the UNIX system limits the maximum number of processes that can exist at one time at a time. If the program does not clean up the zombie in time, it will end up with too many processes, and there will be an error when the new process needs to be generated again.

For the above reasons, we need to call wait or waipid in the parent after the child process calls exit

#include <sys/types.h>

#include <sys/wait.h>

pid_t Wait (int &statloc);

pid_t waitpid (pid_t pid,int *statloc, int options);

Both return:process ID if ok,-1 on Error

They are called by the parent process to get the child process end information and clear the zombie. When the parent calls the two functions

A block (if its child process is still running)

b immediately returns the child process end information (if a child process has ended and waits for the parent process to get information)

C Returns an error (if no child processes exist)

The difference between two functions is that wait will block the caller until a child process terminates and waitpid can be set to non-blocking by setting an option, and the waitpid is not waiting for the first process to end, but for the PID-specified process in the parameter.

The variable statloc in two functions is a pointer to int type data. If this variable is not NULL, then the termination status of the end process is saved in the area of the memory that the Statiloc points to, and if we do not care about termination status, the Statloc can be set to null.

In the traditional implementation, the specific bits in the integers returned by the two functions are given a specific meaning. POSIX.1 specifies some of the termination status contained in header files <sys/wait.h> macros to view these

Macro Description

wifexited (status) If the status is generated by a normally terminated process, the value is true, and we can continue to use the macro Wexitstatus (status) to get the exit or _exit parameters

wifsignaled (status) If the status is generated by a process that ends with an exception (receiving a signal), the value is true, and the macro Wtermsig (status) is used to get the number of signals.

wifstopped (status) If the status is generated by a process that receives a signal that is currently terminated, you can continue to invoke macro Wstopsig (status) to see which signal is causing the process to terminate.

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.