Blame man2/getitimer.2

Packit 7cfc04
.\" Copyright 7/93 by Darren Senn <sinster@scintilla.santa-clara.ca.us>
Packit 7cfc04
.\" and Copyright (C) 2016, Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\" Based on a similar page Copyright 1992 by Rick Faith
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(FREELY_REDISTRIBUTABLE)
Packit 7cfc04
.\" May be freely distributed and modified
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" Modified Tue Oct 22 00:22:35 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
Packit 7cfc04
.\" 2005-04-06 mtk, Matthias Lang <matthias@corelatus.se>
Packit 7cfc04
.\" 	Noted MAX_SEC_IN_JIFFIES ceiling
Packit 7cfc04
.\"
Packit 7cfc04
.TH GETITIMER 2 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
getitimer, setitimer \- get or set value of an interval timer
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <sys/time.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int getitimer(int " which ", struct itimerval *" curr_value );
Packit 7cfc04
.BI "int setitimer(int " which ", const struct itimerval *" new_value ,
Packit 7cfc04
.BI "              struct itimerval *" old_value );
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
These system calls provide access to interval timers, that is,
Packit 7cfc04
timers that initially expire at some point in the future,
Packit 7cfc04
and (optionally) at regular intervals after that.
Packit 7cfc04
When a timer expires, a signal is generated for the calling process,
Packit 7cfc04
and the timer is reset to the specified interval
Packit 7cfc04
(if the interval is nonzero).
Packit 7cfc04
.PP
Packit 7cfc04
Three types of timers\(emspecified via the
Packit 7cfc04
.IR which
Packit 7cfc04
argument\(emare provided,
Packit 7cfc04
each of which counts against a different clock and
Packit 7cfc04
generates a different signal on timer expiration:
Packit 7cfc04
.TP 1.5i
Packit 7cfc04
.B ITIMER_REAL
Packit 7cfc04
This timer counts down in real (i.e., wall clock) time.
Packit 7cfc04
At each expiration, a
Packit 7cfc04
.B SIGALRM
Packit 7cfc04
signal is generated.
Packit 7cfc04
.TP
Packit 7cfc04
.B ITIMER_VIRTUAL
Packit 7cfc04
This timer counts down against the user-mode CPU time consumed by the process.
Packit 7cfc04
(The measurement includes CPU time consumed by all threads in the process.)
Packit 7cfc04
At each expiration, a
Packit 7cfc04
.B SIGVTALRM
Packit 7cfc04
signal is generated.
Packit 7cfc04
.TP
Packit 7cfc04
.B ITIMER_PROF
Packit 7cfc04
This timer counts down against the total (i.e., both user and system)
Packit 7cfc04
CPU time consumed by the process.
Packit 7cfc04
(The measurement includes CPU time consumed by all threads in the process.)
Packit 7cfc04
At each expiration, a
Packit 7cfc04
.B SIGPROF
Packit 7cfc04
signal is generated.
Packit 7cfc04
.IP
Packit 7cfc04
In conjunction with
Packit 7cfc04
.BR ITIMER_VIRTUAL ,
Packit 7cfc04
this timer can be used to profile user and system CPU time
Packit 7cfc04
consumed by the process.
Packit 7cfc04
.PP
Packit 7cfc04
A process has only one of each of the three types of timers.
Packit 7cfc04
.PP
Packit 7cfc04
Timer values are defined by the following structures:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct itimerval {
Packit 7cfc04
    struct timeval it_interval; /* Interval for periodic timer */
Packit 7cfc04
    struct timeval it_value;    /* Time until next expiration */
Packit 7cfc04
};
Packit 7cfc04
Packit 7cfc04
struct timeval {
Packit 7cfc04
    time_t      tv_sec;         /* seconds */
Packit 7cfc04
    suseconds_t tv_usec;        /* microseconds */
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.\"
Packit 7cfc04
.SS getitimer()
Packit 7cfc04
The function
Packit 7cfc04
.BR getitimer ()
Packit 7cfc04
places the current value of the timer specified by
Packit 7cfc04
.IR which
Packit 7cfc04
in the buffer pointed to by
Packit 7cfc04
.IR curr_value .
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.IR it_value
Packit 7cfc04
substructure is populated with the amount of time remaining until
Packit 7cfc04
the next expiration of the specified timer.
Packit 7cfc04
This value changes as the timer counts down, and will be reset to
Packit 7cfc04
.IR it_interval
Packit 7cfc04
when the timer expires.
Packit 7cfc04
If both fields of
Packit 7cfc04
.IR it_value
Packit 7cfc04
are zero, then this timer is currently disarmed (inactive).
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.IR it_interval
Packit 7cfc04
substructure is populated with the timer interval.
Packit 7cfc04
If both fields of
Packit 7cfc04
.IR it_interval
Packit 7cfc04
are zero, then this is a single-shot timer (i.e., it expires just once).
Packit 7cfc04
.SS setitimer()
Packit 7cfc04
The function
Packit 7cfc04
.BR setitimer ()
Packit 7cfc04
arms or disarms the timer specified by
Packit 7cfc04
.IR which ,
Packit 7cfc04
by setting the timer to the value specified by
Packit 7cfc04
.IR new_value .
Packit 7cfc04
If
Packit 7cfc04
.I old_value
Packit 7cfc04
is non-NULL,
Packit 7cfc04
the buffer it points to is used to return the previous value of the timer
Packit 7cfc04
(i.e., the same information that is returned by
Packit 7cfc04
.BR getitimer ()).
Packit 7cfc04
.PP
Packit 7cfc04
If either field in
Packit 7cfc04
.IR new_value.it_value
Packit 7cfc04
is nonzero,
Packit 7cfc04
then the timer is armed to initially expire at the specified time.
Packit 7cfc04
If both fields in
Packit 7cfc04
.IR new_value.it_value
Packit 7cfc04
are zero, then the timer is disarmed.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.IR new_value.it_interval
Packit 7cfc04
field specifies the new interval for the timer;
Packit 7cfc04
if both of its subfields are zero, the timer is single-shot.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success, zero is returned.
Packit 7cfc04
On error, \-1 is returned, and
Packit 7cfc04
.I errno
Packit 7cfc04
is set appropriately.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
.TP
Packit 7cfc04
.B EFAULT
Packit 7cfc04
.IR new_value ,
Packit 7cfc04
.IR old_value ,
Packit 7cfc04
or
Packit 7cfc04
.I curr_value
Packit 7cfc04
is not valid a pointer.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
.I which
Packit 7cfc04
is not one of
Packit 7cfc04
.BR ITIMER_REAL ,
Packit 7cfc04
.BR ITIMER_VIRTUAL ,
Packit 7cfc04
or
Packit 7cfc04
.BR ITIMER_PROF ;
Packit 7cfc04
or (since Linux 2.6.22) one of the
Packit 7cfc04
.I tv_usec
Packit 7cfc04
fields in the structure pointed to by
Packit 7cfc04
.I new_value
Packit 7cfc04
contains a value outside the range 0 to 999999.
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
POSIX.1-2001, SVr4, 4.4BSD (this call first appeared in 4.2BSD).
Packit 7cfc04
POSIX.1-2008 marks
Packit 7cfc04
.BR getitimer ()
Packit 7cfc04
and
Packit 7cfc04
.BR setitimer ()
Packit 7cfc04
obsolete, recommending the use of the POSIX timers API
Packit 7cfc04
.RB ( timer_gettime (2),
Packit 7cfc04
.BR timer_settime (2),
Packit 7cfc04
etc.) instead.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
Timers will never expire before the requested time,
Packit 7cfc04
but may expire some (short) time afterward, which depends
Packit 7cfc04
on the system timer resolution and on the system load; see
Packit 7cfc04
.BR time (7).
Packit 7cfc04
(But see BUGS below.)
Packit 7cfc04
If the timer expires while the process is active (always true for
Packit 7cfc04
.BR ITIMER_VIRTUAL ),
Packit 7cfc04
the signal will be delivered immediately when generated.
Packit 7cfc04
.PP
Packit 7cfc04
A child created via
Packit 7cfc04
.BR fork (2)
Packit 7cfc04
does not inherit its parent's interval timers.
Packit 7cfc04
Interval timers are preserved across an
Packit 7cfc04
.BR execve (2).
Packit 7cfc04
.PP
Packit 7cfc04
POSIX.1 leaves the
Packit 7cfc04
interaction between
Packit 7cfc04
.BR setitimer ()
Packit 7cfc04
and the three interfaces
Packit 7cfc04
.BR alarm (2),
Packit 7cfc04
.BR sleep (3),
Packit 7cfc04
and
Packit 7cfc04
.BR usleep (3)
Packit 7cfc04
unspecified.
Packit 7cfc04
.PP
Packit 7cfc04
The standards are silent on the meaning of the call:
Packit 7cfc04
.PP
Packit 7cfc04
    setitimer(which, NULL, &old_value);
Packit 7cfc04
.PP
Packit 7cfc04
Many systems (Solaris, the BSDs, and perhaps others)
Packit 7cfc04
treat this as equivalent to:
Packit 7cfc04
.PP
Packit 7cfc04
    getitimer(which, &old_value);
Packit 7cfc04
.PP
Packit 7cfc04
In Linux, this is treated as being equivalent to a call in which the
Packit 7cfc04
.I new_value
Packit 7cfc04
fields are zero; that is, the timer is disabled.
Packit 7cfc04
.IR "Don't use this Linux misfeature" :
Packit 7cfc04
it is nonportable and unnecessary.
Packit 7cfc04
.SH BUGS
Packit 7cfc04
The generation and delivery of a signal are distinct, and
Packit 7cfc04
only one instance of each of the signals listed above may be pending
Packit 7cfc04
for a process.
Packit 7cfc04
Under very heavy loading, an
Packit 7cfc04
.B ITIMER_REAL
Packit 7cfc04
timer may expire before the signal from a previous expiration
Packit 7cfc04
has been delivered.
Packit 7cfc04
The second signal in such an event will be lost.
Packit 7cfc04
.PP
Packit 7cfc04
On Linux kernels before 2.6.16, timer values are represented in jiffies.
Packit 7cfc04
If a request is made set a timer with a value whose jiffies
Packit 7cfc04
representation exceeds
Packit 7cfc04
.B MAX_SEC_IN_JIFFIES
Packit 7cfc04
(defined in
Packit 7cfc04
.IR include/linux/jiffies.h ),
Packit 7cfc04
then the timer is silently truncated to this ceiling value.
Packit 7cfc04
On Linux/i386 (where, since Linux 2.6.13,
Packit 7cfc04
the default jiffy is 0.004 seconds),
Packit 7cfc04
this means that the ceiling value for a timer is
Packit 7cfc04
approximately 99.42 days.
Packit 7cfc04
Since Linux 2.6.16,
Packit 7cfc04
the kernel uses a different internal representation for times,
Packit 7cfc04
and this ceiling is removed.
Packit 7cfc04
.PP
Packit 7cfc04
On certain systems (including i386),
Packit 7cfc04
Linux kernels before version 2.6.12 have a bug which will produce
Packit 7cfc04
premature timer expirations of up to one jiffy under some circumstances.
Packit 7cfc04
This bug is fixed in kernel 2.6.12.
Packit 7cfc04
.\" 4 Jul 2005: It looks like this bug may remain in 2.4.x.
Packit 7cfc04
.\"	http://lkml.org/lkml/2005/7/1/165
Packit 7cfc04
.PP
Packit 7cfc04
POSIX.1-2001 says that
Packit 7cfc04
.BR setitimer ()
Packit 7cfc04
should fail if a
Packit 7cfc04
.I tv_usec
Packit 7cfc04
value is specified that is outside of the range 0 to 999999.
Packit 7cfc04
However, in kernels up to and including 2.6.21,
Packit 7cfc04
Linux does not give an error, but instead silently
Packit 7cfc04
adjusts the corresponding seconds value for the timer.
Packit 7cfc04
From kernel 2.6.22 onward,
Packit 7cfc04
this nonconformance has been repaired:
Packit 7cfc04
an improper
Packit 7cfc04
.I tv_usec
Packit 7cfc04
value results in an
Packit 7cfc04
.B EINVAL
Packit 7cfc04
error.
Packit 7cfc04
.\" Bugzilla report 25 Apr 2006:
Packit 7cfc04
.\" http://bugzilla.kernel.org/show_bug.cgi?id=6443
Packit 7cfc04
.\" "setitimer() should reject noncanonical arguments"
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR gettimeofday (2),
Packit 7cfc04
.BR sigaction (2),
Packit 7cfc04
.BR signal (2),
Packit 7cfc04
.BR timer_create (2),
Packit 7cfc04
.BR timerfd_create (2),
Packit 7cfc04
.BR time (7)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.