#include #include #include #include #include #include static void check_for_SIGALRM (int sig) { if (sig != SIGALRM) _exit (1); } int main (void) { static struct sigaction act; struct timespec forever, remaining; time_t time_t_max = (1ull << (sizeof time_t_max * CHAR_BIT - 1)) - 1; act.sa_handler = check_for_SIGALRM; sigemptyset (&act.sa_mask); sigaction (SIGALRM, &act, NULL); forever.tv_sec = time_t_max; forever.tv_nsec = 999999999; printf ("Setting alarm for 1 second from now ...\n"); alarm (1); printf ("Sleeping for %lld.%09ld seconds...\n", (long long) forever.tv_sec, forever.tv_nsec); if (nanosleep (&forever, &remaining) == 0) return 2; if (errno != EINTR) return 3; if (remaining.tv_sec < time_t_max - 10) { printf ("After alarm sent off, remaining time is %lld.%09ld seconds;\n", (long long) remaining.tv_sec, remaining.tv_nsec); printf ("i.e., nanosleep claimed that it slept for about %f years.\n", (forever.tv_sec - remaining.tv_sec) / (24 * 60 * 60 * 364.2425)); return 4; } printf ("ok\n"); return 0; }