The arms race – arming and disarming a POSIX timer

As mentioned previously, we use the timer_settime(2) system call to either arm (start) or disarm (stop) a timer:

#include <time.h>int timer_settime(timer_t timerid, int flags,                    const struct itimerspec *new_value,                    struct itimerspec *old_value);Link with -lrt.

As one can have multiple concurrent POSIX timers running simultaneously, thus we need to specify exactly which timer we are referring to; this is done via the first parameter timer_id, which is the timer's ID, and the effective return of the previously seen timer_create(2) system call.

The important data structure employed here is the itimerspec; its definition is as follows:

struct timespec {    time_t tv_sec; /* Seconds */ long tv_nsec; ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.