wait(2) System Calls Manual wait(2) wait waitpid waitid - (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); /* This is the glibc and POSIX interface; see VERSIONS for information on the raw system call. */ 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 . : . "" ( ). . ( SA_RESTART sigaction(2)). waitable. wait() waitpid() wait() . wait(&wstatus) : waitpid(-1, &wstatus, 0); waitpid() pid . waitpid() options . pid: < -1 pid. -1 . 0 waitpid(). > 0 pid. options OR : WNOHANG . WUNTRACED ( ptrace(2)). . WCONTINUED ( Linux 2.6.10) SIGCONT. ( Linux .) wstatus NULL wait() waitpid() int . ( wait() waitpid()!): WIFEXITED(wstatus) true exit(3) _exit(2) main(). WEXITSTATUS(wstatus) . 8 status exit(3) _exit(2) return main(). WIFEXITED true. WIFSIGNALED(wstatus) . WTERMSIG(wstatus) . WIFSIGNALED . WCOREDUMP(wstatus) // POSIX.1-2024 ( core(5)). WIFSIGNALED . WIFSTOPPED(wstatus) // POSIX.1-2024 WUNTRACED ( ptrace(2)). WSTOPSIG(wstatus) . WIFSTOPPED . WIFCONTINUED(wstatus) ( 2.6.10) SIGCONT. waitid() waitid() ( 2.6.9) . idtype id ( ) : idtype == P_PID id. idtype == P_PIDFD ( 5.4) PID id. ( pidfd_open(2) PID.) idtype == P_PGID id. 5.4 id . idtype == P_ALL id. OR options: WEXITED . WSTOPPED . WCONTINUED ( ) SIGCONT. OR options: WNOHANG waitpid(). WNOWAIT . waitid() siginfo_t infop: si_pid (PID) . si_uid . ( .) 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). WNOHANG options waitid() 0 siginfo_t infop . ( ) si_pid . POSIX.1-2008 1 (2013) WNOHANG options waitid() si_pid si_signo . si_pid waitid(). POSIX.1 . wait(): -1. waitpid(): WNOHANG pid 0. -1. waitid(): 0 WNOHANG id -1. errno . EAGAIN PID id . ECHILD ( wait()) . ECHILD ( waitpid() waitid()) pid (waitpid()) idtype id (waitid()) . ( SIGCHLD SIG_IGN. .) EINTR WNOHANG SIGCHLD signal(7). EINVAL options . ESRCH ( wait() waitpid()) pid INT_MIN. C wait() ( glibc) wait4(2). waitpid() C wait4(2). waitid() struct rusage *. NULL wait4(2). getrusage(2) . POSIX.1-2024. SVr4 4.3BSD POSIX.1-2001. "". (PID ) . . "" ( ) init(1) ( "subreaper" prctl(2) PR_SET_CHILD_SUBREAPER) init(1) . POSIX.1-2001 SIGCHLD SIG_IGN SA_NOCLDWAIT SIGCHLD ( sigaction(2)) wait() waitpid() errno ECHILD. ( POSIX SIGCHLD SIG_IGN . SIGCHLD "" SIG_IGN .) 2.6 POSIX. 2.4 ( ) : wait() waitpid() SIGCHLD SIGCHLD . . clone(2) pthread_create(3) clone(2). 2.4 . POSIX 2.4 . options clone(2) 4.7 waitid(): __WCLONE "clone" . "non-clone" . ( "clone" SIGCHLD .) __WALL . __WALL ( 2.4) ( ). __WNOTHREAD ( 2.4) . 2.4. 4.7 __WALL (ptraced). POSIX.1-2008 waitid() infop siginfo_t ( ). infop (NULL) waitid() . . 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 #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) # 3 . . : . 6.18 8 2026 wait(2)