Search code examples
csignalswaitpidsetitimer

Prevent SIGALRM from interrupting waitpid()


I'm trying to make my process waitpid() for a child process, but also print something every interval amount of time.

My current plan is to schedule an itimer, waitpid(), handle the SIGALRM printing, and stopping the timer when the waitpid() finishes.

The only part I can't figure out is preventing the SIGALRM from interrupting the waitpid().

I've looked in the man pages and don't see any flags for this.

Thoughts?


Solution

  • If waitpid() returned -1 and errno equals EINTR just start over calling waitpid().

    pid_t pid;
    
    while (((pid_t) -1) == (pid = waitpid(...)))
    {
      if (EINTR == errno)
      {
        continue;
      }
    
      ...
    }
    

    Alternatively (at least for Linux) the return of waitpid() on reception of a signal could be avoided via the SA_RESTART flag set on installing signal handlers. For more on this please see here: http://man7.org/linux/man-pages/man7/signal.7.html