For child processes, the wait()
and waitpid()
functions can be used to suspends execution of the current process until a child has exited. But t
Nothing equivalent to wait()
. The usual practice is to poll using kill(pid, 0)
and looking for return value -1 and errno
of ESRCH
to indicate that the process is gone.
Update: Since linux kernel 5.3 there is a pidfd_open syscall, which creates an fd for a given pid, which can be polled to get notification when pid has exited.
None I am aware of. Apart from the solution from chaos, you can use semaphores if you can change the program you want to wait for.
The library functions are sem_open(3)
, sem_init(3),
sem_wait(3),
...
sem_wait(3)
performs a wait, so you don´t have to do busy waiting as in chaos´ solution. Of course, using semaphores makes your programs more complex and it may not be worth the trouble.
On BSDs and OS X, you can use kqueue with EVFILT_PROC+NOTE_EXIT to do exactly that. No polling required. Unfortunately there's no Linux equivalent.