#include <sys/types.h>
#include <sys/wait.h>
pid_t waitpid(pid_t pid, int* status, int options);
pid < -1 wait for any process with group id value = -pid;
pid = -1 same as wait();
pid = 0 wait for any subprocess of this process;
pid > 0 wait for process with it's id = pid;
options = 0 don't use options;
WNOHANG : just return if no subprocess finished.
WUNTRACED: just return if subprocess is hanged up now.
status the return status of subprocess is stored in this int value.
to make sure what happed, we can use following macro:
WIFEXITED(status) 如果子进程正常结束则为非0值。
WEXITSTATUS(status) 取得子进程exit()返回的结束代码,一般会先用WIFEXITED来判断是否正常结束才能使用此宏。
WIFSIGNALED(status) 如果子进程是因为信号而结束则此宏值为真
WTERMSIG(status) 取得子进程因信号而中止的信号代码,一般会先用WIFSIGNALED来判断后才使用此宏。
WIFSTOPPED(status) 如果子进程处于暂停执行情况则此宏值为真。一般只有使用WUNTRACED时才会有此情况。
WSTOPSIG(status) 取得引发子进程暂停的信号代码,一般会先用WIFSTOPPED来判断后才使用此宏。
return pid the subprocess id, else -1 for error, and errno is set.