Waitpid equivalent with timeout?

前端 未结 10 1261
小鲜肉
小鲜肉 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:45

    I thought that select will return EINTR when SIGCHLD signaled by on of the child. I belive this should work:

    while(1)
    {
      int retval = select(0, NULL, NULL, NULL, &tv, &mask);
      if (retval == -1 && errno == EINTR) // some signal
      { 
          pid_t pid = (waitpid(-1, &st, WNOHANG) == 0);
          if (pid != 0) // some child signaled
      }
      else if (retval == 0)
      {
          // timeout
          break;
      }
      else // error
    }
    

    Note: you can use pselect to override current sigmask and avoid interrupts from unneeded signals.

提交回复
热议问题