GETITIMER(2)
π GETITIMER(2) π
okay so bestie, we're in the System Calls Manual today and the vibe is timers. not the cute countdown-to-your-birthday kind (well, actually, kind of). we're talking interval timers, the little ticking clocks your process can set up so the kernel yells at it later. grab a snack π, this one's actually fun.
β¨ NAME β¨
getitimer, setitimer β get/set value of interval timer
π LIBRARY π
Standard C Library (libc, -lc)
π SYNOPSIS π
#include <sys/time.h>
#define ITIMER_REAL 0
#define ITIMER_VIRTUAL 1
#define ITIMER_PROF 2
int
getitimer(int which, struct itimerval *value);
int
setitimer(int which, const struct itimerval *value,
struct itimerval *ovalue);
π¦ DESCRIPTION π¦
so. every process gets three interval timers, and they're all defined in <sys/time.h>. that's the whole personality of this page.
getitimer()β hands you back the current value of whichever timer you asked for inwhich, and drops it into the structure atvalue.setitimer()β sets a timer to the value you give it, and ifovalueisn't a null pointer it also gives you the previous value of that timer. two-for-one, we love to see it π
and a timer value? that's an itimerval structure, babes:
struct itimerval {
struct timeval it_interval; /* timer interval */
struct timeval it_value; /* current value */
};
okay let's decode this icon:
- if
it_valueis non-zero, that's the time until the next timer expiration. it's the "when do I go off" field. - if
it_intervalis non-zero, that's the value used to reloadit_valuewhen the timer expires. so it's the "and then do it again every ___" field. - setting
it_valueto0disables the timer. doesn't matter whatit_intervalsays β zeroit_valuemeans off, periodt. - setting
it_intervalto0means the timer gets disabled after its next expiration (assumingit_valueis non-zero, obviously). one last hurrah, then it's done.
β° heads up: time values smaller than the resolution of the system clock get rounded up to that resolution. typically that's 10 milliseconds. so don't come crying to me about sub-10ms precision, it's giving "the clock said no."
now the three timers, main character by main character:
ITIMER_REAL β this one decrements in real time. when it expires, a SIGALRM signal gets delivered. wall-clock baby, it does not care what your process is doing.
ITIMER_VIRTUAL β this one decrements in process virtual time. it only runs while the process is actually executing. when it expires, you get a SIGVTALRM. it's the "only count my time, not the world's" timer β¨
ITIMER_PROF β this one decrements in process virtual time and while the system is running on behalf of the process. it's designed for interpreters doing statistical profiling of interpreted programs. every time it expires, SIGPROF gets delivered.
π and becauseSIGPROFcan interrupt in-progress system calls, any program usingITIMER_PROFmust be prepared to restart interrupted system calls. this is not optional, bestie, this is the price of admission.
one more number for you: the maximum number of seconds allowed for it_interval and it_value in setitimer() is 100000000. that's the ceiling. don't push it. π«Ά
π NOTES π
three macros for messing with time values live in <sys/time.h> too:
timerclear()β sets a time value to zero.timerisset()β tests if a time value is non-zero.timercmp()β compares two time values.
that's the whole trio. useful little guys. πΈ
π RETURN VALUES π
if everything went great? you get 0. iconic.
if it didn't? you get -1, and the global variable errno gets set to tell you what went wrong. (rude, but informative.)
π ERRORS π
both getitimer() and setitimer() will fail if:
[EFAULT]β thevalueargument specified a bad address. π[EINVAL]β thevalueargument specified a time that was too large to be handled.
π SEE ALSO π
gettimeofday(2), select(2), sigaction(2), clocks(7)
π STANDARDS π
getitimer() and setitimer() conform to IEEE Std 1003.1-2001 ("POSIX.1"). β¨
β οΈ but plot twist: the later IEEE Std 1003.1-2008 ("POSIX.1") revision marked both functions as obsolescent, and it recommends usingtimer_gettime(2)andtimer_settime(2)instead. so likeβ¦ they still work, but the standards girlies are side-eyeing them. iykyk.
π°οΈ HISTORY π°οΈ
the getitimer() system call appeared in 4.2BSD. an elder. a legend. π
and that's the tea, bestie π this page is from the Debian crew and it's dated May 1, 2020 β so go forth, set some timers, and remember to restart those interrupted system calls. slay πβ¨
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1983, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms