timer_settime(2)

๐Ÿ’– timer_settime(2) โ€” bestie edition ๐Ÿ’–

okay so this is timer_settime(2) from section 2 (system calls, the โœจrealโœจ ones), and honestly? it's giving "I control time now". let's go ๐Ÿ•ฐ๏ธโœจ


๐ŸŒธ NAME ๐ŸŒธ

timer_settime, timer_gettime โ€” arm/disarm and fetch state of POSIX per-process timer ๐Ÿ’…

like. you can literally arm a timer. and disarm it. and peek at what it's doing. iconic.


๐ŸŽ€ LIBRARY ๐ŸŽ€

Real-time library (librt, -lrt) ๐Ÿซถ


โœจ SYNOPSIS โœจ

#include <time.h>

int timer_gettime(timer_t timerid, struct itimerspec *curr_value);
int timer_settime(timer_t timerid, int flags,
                  const struct itimerspec *restrict new_value,
                  struct itimerspec *_Nullable restrict old_value);

๐Ÿงš Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

timer_settime(), timer_gettime():
    _POSIX_C_SOURCE >= 199309L

๐Ÿ’œ DESCRIPTION ๐Ÿ’œ

timer_settime() arms or disarms the timer identified by timerid. no cap, that's the whole job.

the new_value argument is a pointer to an itimerspec structure that specifies the new initial value and the new interval for the timer. that itimerspec structure is described over in itimerspec(3type) if you wanna go read it, bestie, I'm not gonna steal its whole moment.

each of the substructures of the itimerspec structure is a timespec(3) structure that lets a time value be specified in seconds and nanoseconds. these time values are measured according to the clock that was specified when the timer was created by timer_create(2). so like, the clock choice was made AGES ago, at timer-birth. ๐Ÿผ

if new_value->it_value specifies a nonzero value (i.e., either subfield is nonzero), then timer_settime() arms (starts) the timer, setting it to initially expire at the given time. and if the timer was already armed? yeah, the previous settings are just overwritten. sorry not sorry ๐Ÿ’…

if new_value->it_value specifies a zero value (i.e., both subfields are zero), then the timer is disarmed. nap time ๐Ÿ˜ด

the new_value->it_interval field specifies the period of the timer, in seconds and nanoseconds. if this field is nonzero, then each time that an armed timer expires, the timer is reloaded from the value specified in new_value->it_interval. it just keeps going. main character energy, it never stops. ๐Ÿ”

if new_value->it_interval specifies a zero value, then the timer expires just once, at the time specified by it_value. one and done, a little one-shot moment. ๐ŸŽฏ

by default, the initial expiration time specified in new_value->it_value is interpreted relative to the current time on the timer's clock at the time of the call. but you can modify that by specifying TIMER_ABSTIME in flags, in which case new_value->it_value is interpreted as an absolute value as measured on the timer's clock; that is, the timer will expire when the clock value reaches the value specified by new_value->it_value. if the specified absolute time has already passed, then the timer expires immediately, and the overrun count (see timer_getoverrun(2)) will be set correctly. yes bestie, it handles the "oops I'm late" case for you ๐Ÿ˜ญโœจ

if the value of the CLOCK_REALTIME clock is adjusted while an absolute timer based on that clock is armed, then the expiration of the timer will be appropriately adjusted. adjustments to the CLOCK_REALTIME clock have no effect on relative timers based on that clock. fr fr, absolute timers move when the clock moves, relative ones do not. ๐Ÿ“Œ

if old_value is not NULL, then it points to a buffer that is used to return the previous interval of the timer (in old_value->it_interval) and the amount of time until the timer would previously have next expired (in old_value->it_value). it's like receipts for the old settings ๐Ÿงพ

timer_gettime() returns the time until next expiration, and the interval, for the timer specified by timerid, in the buffer pointed to by curr_value. the time remaining until the next timer expiration is returned in curr_value->it_value; this is always a relative value, regardless of whether the TIMER_ABSTIME flag was used when arming the timer. always relative, no exceptions, periodt. if the value returned in curr_value->it_value is zero, then the timer is currently disarmed ๐Ÿ’ค

the timer interval is returned in curr_value->it_interval. if the value returned in curr_value->it_interval is zero, then this is a "one-shot" timer. ๐ŸŽฏ


๐ŸŒˆ RETURN VALUE ๐ŸŒˆ

on success, timer_settime() and timer_gettime() return 0. ๐Ÿฅณ

on error, -1 is returned, and errno is set to indicate the error. ๐Ÿ’€


๐Ÿ’… ERRORS ๐Ÿ’…

these functions may fail with the following errors:

  • EFAULT โ€” new_value, old_value, or curr_value is not a valid pointer. ๐Ÿซ 
  • EINVAL โ€” timerid is invalid. ๐Ÿšซ

timer_settime() may fail with the following errors:

  • EINVAL โ€” new_value.it_value is negative; or new_value.it_value.tv_nsec is negative or greater than 999,999,999.
lowkey pay attention to that second one bestie, 999,999,999 nanoseconds is the ceiling. you cannot sneak in a full second inside the nanosecond field. the kernel WILL clock it. ๐Ÿ™ˆ

๐Ÿ‘‘ STANDARDS ๐Ÿ‘‘

POSIX.1-2024.


๐Ÿฆ‹ HISTORY ๐Ÿฆ‹

Linux 2.6. POSIX.1-2001.


๐Ÿ“ EXAMPLES ๐Ÿ“

See timer_create(2).

(yeah it's a short one, the real demo lives over there. go look, it's cute.)


โญ SEE ALSO โญ

timer_create(2), timer_getoverrun(2), timespec(3), time(7)


and that's the whole page, bestie!! this manual page came from Linux man-pages 6.20, dated 2025-10-29 โ€” shoutout to the maintainers for keeping the timer lore alive ๐Ÿซถโœจ now go arm something. responsibly. ๐Ÿ’–

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms