timer_create − create a POSIX per-process timer


#include <signal.h>
#include <time.h>

int timer_create(clockid_t clockid, struct sigevent *sevp,
timer_t *

Link with −lrt.

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

timer_create(): _POSIX_C_SOURCE >= 199309L


timer_create() creates a new per-process interval timer. The ID of the new timer is returned in the buffer pointed to by timerid, which must be a non-null pointer. This ID is unique within the process, until the timer is deleted. The new timer is initially disarmed.

The clockid argument specifies the clock that the new timer uses to measure time. It can be specified as one of the following values:

A settable system-wide real-time clock.


A nonsettable monotonically increasing clock that measures time from some unspecified point in the past that does not change after system startup.

CLOCK_PROCESS_CPUTIME_ID (since Linux 2.6.12)

A clock that measures (user and system) CPU time consumed by (all of the threads in) the calling process.

CLOCK_THREAD_CPUTIME_ID (since Linux 2.6.12)

A clock that measures (user and system) CPU time consumed by the calling thread.

As well as the above values, clockid can be specified as the clockid returned by a call to clock_getcpuclockid(3) or pthread_getcpuclockid(3).

The sevp argument points to a sigevent structure that specifies how the caller should be notified when the timer expires. For the definition and general details of this structure, see sigevent(7).

The sevp.sigev_notify field can have the following values:

Don’t asynchronously notify when the timer expires. Progress of the timer can be monitored using timer_gettime(2).


Upon timer expiration, generate the signal sigev_signo for the process. See sigevent(7) for general details. The si_code field of the siginfo_t structure will be set to SI_TIMER. At any point in time, at most one signal is queued to the process for a given timer; see timer_getoverrun(2) for more details.


Upon timer expiration, invoke sigev_notify_function as if it were the start function of a new thread. See sigevent(7) for details.

SIGEV_THREAD_ID (Linux-specific)

As for SIGEV_SIGNAL, but the signal is targeted at the thread whose ID is given in sigev_notify_thread_id, which must be a thread in the same process as the caller. The sigev_notify_thread_id field specifies a kernel thread ID, that is, the value returned by clone(2) or gettid(2). This flag is intended only for use by threading libraries.

Specifying sevp as NULL is equivalent to specifying a pointer to a sigevent structure in which sigev_notify is SIGEV_SIGNAL, sigev_signo is SIGALRM, and sigev_value.sival_int is the timer ID.


On success, timer_create() returns 0, and the ID of the new timer is placed in *timerid. On failure, −1 is returned, and errno is set to indicate the error.



Temporary error during kernel allocation of timer structures.


Clock ID, sigev_notify, sigev_signo, or sigev_notify_thread_id is invalid.


Could not allocate memory.


This system call is available since Linux 2.6.




A program may create multiple interval timers using timer_create().

Timers are not inherited by the child of a fork(2), and are disarmed and deleted during an execve(2).

The kernel preallocates a "queued real-time signal" for each timer created using timer_create(). Consequently, the number of timers is limited by the RLIMIT_SIGPENDING resource limit (see setrlimit(2)).

The timers created by timer_create() are commonly known as "POSIX (interval) timers". The POSIX timers API consists of the following interfaces:


timer_create(): Create a timer.


timer_settime(2): Arm (start) or disarm (stop) a timer.


timer_gettime(2): Fetch the time remaining until the next expiration of a timer, along with the interval setting of the timer.


timer_getoverrun(2): Return the overrun count for the last timer expiration.


timer_delete(2): Disarm and delete a timer.

Part of the implementation of the POSIX timers API is provided by glibc. In particular:


The functionality for SIGEV_THREAD is implemented within glibc, rather than the kernel.


The timer IDs presented at user level are maintained by glibc, which maps these IDs to the timer IDs employed by the kernel.

The POSIX timers system calls first appeared in Linux 2.6. Prior to this, glibc provided an incomplete user-space implementation (CLOCK_REALTIME timers only) using POSIX threads, and current glibc falls back to this implementation on systems running pre-2.6 Linux kernels.

Since Linux 3.10, the /proc/[pid]/timers file can be used to list the POSIX timers for the process with PID pid. See proc(5) for further information.


The program below takes two arguments: a sleep period in seconds, and a timer frequency in nanoseconds. The program establishes a handler for the signal it uses for the timer, blocks that signal, creates and arms a timer that expires with the given frequency, sleeps for the specified number of seconds, and then unblocks the timer signal. Assuming that the timer expired at least once while the program slept, the signal handler will be invoked, and the handler displays some information about the timer notification. The program terminates after one invocation of the signal handler.

In the following example run, the program sleeps for 1 second, after creating a timer that has a frequency of 100 nanoseconds. By the time the signal is unblocked and delivered, there have been around ten million overruns.

$ ./a.out 1 100
Establishing handler for signal 34
Blocking signal 34
timer ID is 0x804c008
Sleeping for 1 seconds
Unblocking signal 34
Caught signal 34
sival_ptr = 0xbfb174f4; *sival_ptr = 0x804c008
overrun count = 10004886

Program source
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>


#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
} while (0)

static void
print_siginfo(siginfo_t *si)
timer_t *tidp;
int or;

tidp = si−>si_value.sival_ptr;

printf(" sival_ptr = %p; ", si−>si_value.sival_ptr);
printf(" *sival_ptr = 0x%lx\n", (long) *tidp);

or = timer_getoverrun(*tidp);
if (or == −1)
printf(" overrun count = %d\n", or);

static void
handler(int sig, siginfo_t *si, void *uc)
/* Note: calling printf() from a signal handler is not
strictly correct, since printf() is not async−signal−safe;
see signal(2) */

printf("Caught signal %d\n", sig);
signal(sig, SIG_IGN);

main(int argc, char *argv[])
timer_t timerid;
struct sigevent sev;
struct itimerspec its;
long long freq_nanosecs;
sigset_t mask;
struct sigaction sa;

if (argc != 3) {
fprintf(stderr, "Usage: %s <sleep−secs> <freq−nanosecs>\n",

/* Establish handler for timer signal */

printf("Establishing handler for signal %d\n", SIG);
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handler;
if (sigaction(SIG, &sa, NULL) == −1)

/* Block timer signal temporarily */

printf("Blocking signal %d\n", SIG);
sigaddset(&mask, SIG);
if (sigprocmask(SIG_SETMASK, &mask, NULL) == −1)

/* Create the timer */

sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIG;
sev.sigev_value.sival_ptr = &timerid;
if (timer_create(CLOCKID, &sev, &timerid) == −1)

printf("timer ID is 0x%lx\n", (long) timerid);

/* Start the timer */

freq_nanosecs = atoll(argv[2]);
its.it_value.tv_sec = freq_nanosecs / 1000000000;
its.it_value.tv_nsec = freq_nanosecs % 1000000000;
its.it_interval.tv_sec = its.it_value.tv_sec;
its.it_interval.tv_nsec = its.it_value.tv_nsec;

if (timer_settime(timerid, 0, &its, NULL) == −1)

/* Sleep for a while; meanwhile, the timer may expire
multiple times */

printf("Sleeping for %d seconds\n", atoi(argv[1]));

/* Unlock the timer signal, so that timer notification
can be delivered */

printf("Unblocking signal %d\n", SIG);
if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == −1)



clock_gettime(2), setitimer(2), timer_delete(2), timer_getoverrun(2), timer_settime(2), timerfd_create(2), clock_getcpuclockid(3), pthread_getcpuclockid(3), pthreads(7), sigevent(7), signal(7), time(7)


This page is part of release 3.69 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at http://www.kernel.org/doc/man−pages/.

More Linux Commands

snmpget(1) - communicates with a network entity using SNMP G
snmpget is an SNMP application that uses the SNMP GET request to query for information on a network entity. One or more object identifiers (OIDs) may be given a

gnutls_pkcs7_set_crt_raw(3) - API function - Linux man page
This function will add a certificate to the PKCS7 or RFC2630 certificate set. RETURNS On success, GNUTLS_E_SUCCESS (0) is returned, otherwise a negative error v

Tcl_LimitReady(3) - manage and check resource limits on inte
Tcls interpreter resource limit subsystem allows for close control over how much computation time a script may use, and is useful for cases where a program is d

pam_misc_drop_env(3) - liberating a locally saved environmen
This function is defined to complement the pam_getenvlist(3) function. It liberates the memory associated with env, overwriting with 0 all memory before free()i

gnutls_x509_crt_set_activation_time(3) - API function.......
This function will set the time this Certificate was or will be activated. RETURNS On success, GNUTLS_E_SUCCESS (0) is returned, otherwise a negative error valu

ypdomainname(8) - set or display name of current NIS(YP) dom
Without an argument, domainname displays the name of the current NIS domainname of the system. domainname uses the getdomainname(2) function for getting the dom

crl2pkcs7(1ssl) - Create a PKCS#7 structure from a CRL and c
The crl2pkcs7 command takes an optional CRL and one or more certificates and converts them into a PKCS#7 degenerate certificates only structure. COMMAND OPTIONS

showkey(1) - examine the codes sent by the keyboard.........
showkey prints to standard output either the scan codes or the keycode or the ascii code of each key pressed. In the first two modes the program runs until 10 s

TAP::Formatter::Color(3pm) - Run Perl test scripts with colo
Note that this harness is experimental. You may not like the colors Ive chosen and I havent yet provided an easy way to override them. This test harness is the

pam_systemd(8) - Register user sessions in the systemd contr
pam_systemd registers user sessions with the systemd login manager systemd-logind.service(8), and hence the systemd control group hierarchy. On login, this modu

mvwinnstr(3ncurses) - get a string of characters from a curs
These routines return a string of characters in str, extracted starting at the current cursor position in the named window. Attributes are stripped from the cha

asn1_expand_octet_string(3) - Expand "OCTET STRING" fields i
Expands an OCTET STRING element of a structure created from a DER decoding process (the asn1_der_decoding() function). The type used for expansion is the first

We can't live, work or learn in freedom unless the software we use is free.