2

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?

alk
  • 69,737
  • 10
  • 105
  • 255
Matt Goodrich
  • 4,875
  • 5
  • 25
  • 38

2 Answers2

1

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

alk
  • 69,737
  • 10
  • 105
  • 255
0

Either you have to mask the signal before calling waitpid(), but then your alarm will be delayed until unblocked, or you have to check waitpid() return value and errno for EINTR

Stian Skjelstad
  • 2,277
  • 1
  • 9
  • 19