Imagine I have a process that starts several child processes. The parent needs to know when a child exits.
I can use waitpid
, but then if/when the paren
If your program runs only on contemporary Linux kernels (5.3 or later), the preferred way is to use pidfd_open
(https://lwn.net/Articles/789023/ https://man7.org/linux/man-pages/man2/pidfd_open.2.html).
This system call returns a file descriptor representing a process, and then you can select
, poll
or epoll
it, the same way you wait on other types of file descriptors.
For example,
int fd = pidfd_open(pid, 0);
struct pollfd pfd = {fd, POLLIN, 0};
poll(&pfd, 1, 1000) == 1;