Waitpid equivalent with timeout?

前端 未结 10 1254
小鲜肉
小鲜肉 2020-11-27 12:10

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

10条回答
  •  情深已故
    2020-11-27 12:47

    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;
    

提交回复
热议问题