It is the signal handler signchld()
that interrupts usleep()
in the parent, as we can prove by substituting
usleep(20000000);
with
if (usleep(20000000) < 0) {
if (errno == EINTR)
fprintf (stderr, "[PX] usleep() interrupted by a signal\n");
}
(and of course including errno.h
). This will print
[P1] 0
[C1] 0
[X] child exited
[PX] usleep() interrupted by a signal
[P2] 0
[P1] 1
and so on.
On my system (GNU/Linux) the man page for usleep()
reads:
4.3BSD, POSIX.1-2001. POSIX.1-2001 declares this function obsolete; use nanosleep(2) instead. POSIX.1-2008 removes the specification of usleep().
Only the EINVAL error return is documented by SUSv2 and POSIX.1-2001.