wait(2) System Calls Manual wait(2) wait, waitpid, waitid - LIBRARY Standard C library (libc, -lc) #include pid_t wait(int *_Nullable wstatus); pid_t waitpid(pid_t pid, int *_Nullable wstatus, int options); int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options); /* glibc POSIX; . */ glibc (. feature_test_macros(7)): waitid(): Since glibc 2.26: _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200809L glibc 2.25 and earlier: _XOPEN_SOURCE || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L || /* glibc <= 2.19: */ _BSD_SOURCE - , . : , , . , ; , " (zombie)" (. ). , . , , (, - SA_RESTART sigaction(2)). , , (waitable). wait() waitpid() wait() , . wait(&wstatus) : waitpid(-1, &wstatus, 0); waitpid() , , pid. waitpid() , options . pid : < -1 , , pid. -1 , . 0 meaning wait for any child process whose process group ID is equal to that of the calling process at the time of the call to waitpid(). > 0 , , pid. options : WNOHANG , . WUNTRACED , ( ptrace(2)). . WCONTINUED ( Linux 2.6.10) , - SIGCONT. (, Linux, . .) wstatus NULL, wait() waitpid() int, wstatus. ( , wait() waitpid()!): WIFEXITED(wstatus) , , exit(3) _exit(2), main(). WEXITSTATUS(wstatus) . status, exit(3) _exit(2) return main(). , WIFEXITED . WIFSIGNALED(wstatus) , - . WTERMSIG(wstatus) , . , WIFSIGNALED . WCOREDUMP(wstatus) , ( core(5)). , WIFSIGNALED . POSIX.1-2001 UNIX (, AIX, SunOS). #ifdef WCOREDUMP ... #endif. WIFSTOPPED(wstatus) , ; , WUNTRACED (. ptrace(2)). WSTOPSIG(wstatus) , - . , WIFSTOPPED . WIFCONTINUED(wstatus) ( Linux 2.6.10) , , SIGCONT. waitid() waitid() (, Linux 2.6.9) , . idtype id () : idtype == P_PID , ID id. idtype == P_PIDFD ( Linux 5.4) Wait for the child referred to by the PID file descriptor specified in id. (See pidfd_open(2) for further information on PID file descriptors.) idtype == P_PGID Wait for any child whose process group ID matches id. Since Linux 5.4, if id is zero, then wait for any child that is in the same process group as the caller's process group at the time of the call. idtype == P_ALL ; id . options ( OR): WEXITED . WSTOPPED , . WCONTINUED ( ) SIGCONT. OR options : WNOHANG waitpid(). WNOWAIT ; wait . , waitid() siginfo_t, infop: si_pid ID . si_uid ID . ( .) si_signo SIGCHLD. si_status , _exit(2) ( exit(3)), , , . si_code. si_code : CLD_EXITED ( _exit(2)); CLD_KILLED ( ); CLD_DUMPED ( ); CLD_STOPPED ( ); CLD_TRAPPED ( ); CLD_CONTINUED ( SIGCONT). options WNOHANG , waitid() 0, siginfo_t, infop, . () , , si_pid . POSIX.1-2008 Technical Corrigendum 1 (2013) , WNOHANG options , waitid() si_pid si_signo. Linux , si_pid waitid(). POSIX.1. wait(): on success, returns the process ID of the terminated child; on failure, -1 is returned. waitpid(): on success, returns the process ID of the child whose state has changed; if WNOHANG was specified and one or more child(ren) specified by pid exist, but have not yet changed state, then 0 is returned. On failure, -1 is returned. waitid(): returns 0 on success or if WNOHANG was specified and no child(ren) specified by id has yet changed state; on failure, -1 is returned. On failure, each of these calls sets errno to indicate the error. EAGAIN The PID file descriptor specified in id is nonblocking and the process that it refers to has not terminated. ECHILD ( wait()) . ECHILD ( waitpid() waitid()) , pid (waitpid()) idtype id (waitid()), . ( , SIGCHLD SIG_IGN. . Linux Notes .) EINTR WNOHANG SIGCHLD; . signal(7). EINVAL options. ESRCH (for wait() or waitpid()) pid is equal to INT_MIN. C , wait() -- , ( glibc) wait4(2). waitpid(); , C, wait4(2). waitid() struct rusage *. NULL, , wait4(2). getrusage(2). POSIX.1-2008. SVr4, 4.3BSD, POSIX.1-2001. , , , <<>> (zombie). (PID, , ), . , (wait), (slot) , , . , <<>> ( ) init(1) ( <<>>, prctl(2) PR_SET_CHILD_SUBREAPER); init(1) . POSIX.1-2001 , SIGCHLD SIG_IGN SA_NOCLDWAIT ( sigaction(2)), , wait() waitpid() , , errno, ECHILD ( POSIX SIGCHLD SIG_IGN . , SIGCHLD <<>>, SIG_IGN ). Linux 2.6 . , Linux 2.4 ( ) : wait() waitpid() SIGCHLD, SIGCHLD , , , ID . , Linux Linux , , . -- , ( Linux) clone(2); , pthread_create(3), clone(2). Linux 2.4, , , , , . , POSIX , , Linux 2.4, , . options, Linux, , clone(2); Linux 4.7, waitid(): __WCLONE << (clone)>> . , << >> (<<>> , , , SIGCHLD, ). , __WALL. __WALL ( Linux 2.4) ("" ""). __WNOTHREAD ( Linux 2.4) . Linux 2.4. Linux 4.7, , ptrace, __WALL . POSIX.1-2008, , waitid(), , infop siginfo_t (. ., null). Linux, infop NULL, waitid() ID . , . fork(2) waitpid(). . , pause(2), . , , , . , waitpid(), W*(), , . : $ ./a.out & Child PID is 32360 [1] 32359 $ kill -STOP 32360 stopped by signal 19 $ kill -CONT 32360 continued $ kill -TERM 32360 killed by signal 15 [1]+ Done ./a.out $ #include #include #include #include #include int main(int argc, char *argv[]) { int wstatus; pid_t cpid, w; cpid = fork(); if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); } if (cpid == 0) { /* Code executed by child */ printf("Child PID is %jd\n", (intmax_t) getpid()); if (argc == 1) pause(); /* Wait for signals */ _exit(atoi(argv[1])); } else { /* Code executed by parent */ do { w = waitpid(cpid, &wstatus, WUNTRACED | WCONTINUED); if (w == -1) { perror("waitpid"); exit(EXIT_FAILURE); } if (WIFEXITED(wstatus)) { printf("exited, status=%d\n", WEXITSTATUS(wstatus)); } else if (WIFSIGNALED(wstatus)) { printf("killed by signal %d\n", WTERMSIG(wstatus)); } else if (WIFSTOPPED(wstatus)) { printf("stopped by signal %d\n", WSTOPSIG(wstatus)); } else if (WIFCONTINUED(wstatus)) { printf("continued\n"); } } while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus)); exit(EXIT_SUCCESS); } } . _exit(2), clone(2), fork(2), kill(2), ptrace(2), sigaction(2), signal(2), wait4(2), pthread_create(3), core(5), credentials(7), signal(7) Azamat Hackimov Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . wait(2)