timers/t_clock_nanosleep.cThis is timers/t_clock_nanosleep.c, an example to accompany the book, The Linux Programming Interface. This file is not printed in the book; it is the solution to Exercise 23-2 (page 512). The source code file is copyright 2024, Michael Kerrisk, and is licensed under the GNU General Public License, version 3. In the listing below, the names of Linux system calls and C library functions are hyperlinked to manual pages from the Linux man-pages project, and the names of functions implemented in the book are hyperlinked to the implementations of those functions. |
/* t_clock_nanosleep.c Demonstrate the use of clock_nanosleep() to sleep for an interval specified in nanoseconds. See also t_nanosleep.c. Linux supports clock_nanosleep() since kernel 2.6. */ #if ! defined(_XOPEN_SOURCE) || _XOPEN_SOURCE < 600 #define _XOPEN_SOURCE 600 #endif #include <sys/time.h> #include <time.h> #include <signal.h> #include "tlpi_hdr.h"
static void sigintHandler(int sig) { return; /* Just interrupt clock_nanosleep() */ }
int main(int argc, char *argv[]) { if (argc < 3 || strcmp(argv[1], "--help") == 0) usageErr("%s secs nanosecs [a]\n", argv[0]); /* Allow SIGINT handler to interrupt clock_nanosleep() */ struct sigaction sa; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = sigintHandler; if (sigaction(SIGINT, &sa, NULL) == -1) errExit("sigaction"); /* If more than three command-line arguments, use TIMER_ABSTIME flag */ int flags = (argc > 3) ? TIMER_ABSTIME : 0; struct timespec request; if (flags == TIMER_ABSTIME) { if (clock_gettime(CLOCK_REALTIME, &request) == -1) errExit("clock_gettime"); printf("Initial CLOCK_REALTIME value: %ld.%09ld\n", (long) request.tv_sec, request.tv_nsec); request.tv_sec += getLong(argv[1], 0, "secs"); request.tv_nsec += getLong(argv[2], 0, "nanosecs"); if (request.tv_nsec >= 1000000000) { request.tv_sec += request.tv_nsec / 1000000000; request.tv_nsec %= 1000000000; } } else { /* Relative sleep */ request.tv_sec = getLong(argv[1], 0, "secs"); request.tv_nsec = getLong(argv[2], 0, "nanosecs"); } struct timeval start, finish; if (gettimeofday(&start, NULL) == -1) errExit("gettimeofday"); for (;;) { struct timespec remain; int s = clock_nanosleep(CLOCK_REALTIME, flags, &request, &remain); if (s != 0 && s != EINTR) errExitEN(s, "clock_nanosleep"); if (s == EINTR) printf("Interrupted... "); if (gettimeofday(&finish, NULL) == -1) errExit("gettimeofday"); printf("Slept: %.6f secs", finish.tv_sec - start.tv_sec + (finish.tv_usec - start.tv_usec) / 1000000.0); if (s == 0) break; /* sleep completed */ if (flags != TIMER_ABSTIME) { printf("... Remaining: %ld.%09ld", (long) remain.tv_sec, remain.tv_nsec); request = remain; } printf("... Restarting\n"); } printf("\nSleep complete\n"); exit(EXIT_SUCCESS); }
Note that, in most cases, the programs rendered in these web pages are not free standing: you'll typically also need a few other source files (mostly in the lib/ subdirectory) as well. Generally, it's easier to just download the entire source tarball and build the programs with make(1). By hovering your mouse over the various hyperlinked include files and function calls above, you can see which other source files this file depends on.