/* * “oversleeping” problem is particularly marked for a process that uses a loop to re start * a sleep that is interrupted by a signal handler. If signals are delivered at a high rate * , then a relative sleep (of the type performed by nanosleep()) can lead to large inaccuracies * in the time a process spends sleeping. We can avoid the oversleeping problem by making an * initial call to clock_gettime() to retrieve the time, adding the desired amount to that time, * and then calling clock_nanosleep() with the TIMER_ABSTIME flag (and restarting the system * call if it is interrupted by a signal handler). */ struct timespec request; /* Retrieve current value of CLOCK_REALTIME clock */ if (clock_gettime(CLOCK_REALTIME, &request) == -1) errExit("clock_gettime"); request.tv_sec += 20; /* Sleep for 20 seconds from now */ s = clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &request, NULL); if (s != 0) { if (s == EINTR) printf("Interrupted by signal handler\n"); else errExitEN(s, "clock_nanosleep"); }