---
name: "timer_create"
section: "2"
platform: "linux"
package: "man-pages"
version: "6.19-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux", "freebsd"]
url: "https://new.carta.tech/timer_create"
---

# timer\_create(2)

create a POSIX per-process timer

## NAME

timer\_create - create a POSIX per-process timer

## LIBRARY

Real-time library (*librt*, *-lrt*)

## SYNOPSIS

```
#include <signal.h>           /* Definition of SIGEV_* constants */
#include <time.h>
```

**int timer\_create(clockid\_t** *clockid***,**                  **struct sigevent \*\_Nullable restrict** *sevp***,**                  **timer\_t \*restrict** *timerid***);**

Feature Test Macro Requirements for glibc (see [**feature\_test\_macros**(7)](/feature_test_macros/7.md)):

**timer\_create**():

```
    _POSIX_C_SOURCE >= 199309L
```

## DESCRIPTION

**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:

- **CLOCK\_REALTIME**

  A settable system-wide real-time clock.

- **CLOCK\_MONOTONIC**

  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.

- **CLOCK\_BOOTTIME** (Since Linux 2.6.39)

  Like **CLOCK\_MONOTONIC**, this is a monotonically increasing clock. However, whereas the **CLOCK\_MONOTONIC** clock does not measure the time while a system is suspended, the **CLOCK\_BOOTTIME** clock does include the time during which the system is suspended. This is useful for applications that need to be suspend-aware. **CLOCK\_REALTIME** is not suitable for such applications, since that clock is affected by discontinuous changes to the system clock.

- **CLOCK\_REALTIME\_ALARM** (since Linux 3.0)

  This clock is like **CLOCK\_REALTIME**, but will wake the system if it is suspended. The caller must have the **CAP\_WAKE\_ALARM** capability in order to set a timer against this clock.

- **CLOCK\_BOOTTIME\_ALARM** (since Linux 3.0)

  This clock is like **CLOCK\_BOOTTIME**, but will wake the system if it is suspended. The caller must have the **CAP\_WAKE\_ALARM** capability in order to set a timer against this clock.

- **CLOCK\_TAI** (since Linux 3.10)

  A system-wide clock derived from wall-clock time but counting leap seconds.

See [**clock\_getres**(2)](/clock_getres/2.md) for some further details on the above clocks.

As well as the above values, *clockid* can be specified as the *clockid* returned by a call to [**clock\_getcpuclockid**(3)](/clock_getcpuclockid/3.md) or [**pthread\_getcpuclockid**(3)](/pthread_getcpuclockid/3.md).

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**(3type)](/sigevent/3type.md).

The *sevp.sigev\_notify* field can have the following values:

- **SIGEV\_NONE**

  Don't asynchronously notify when the timer expires. Progress of the timer can be monitored using [**timer\_gettime**(2)](/timer_gettime/2.md).

- **SIGEV\_SIGNAL**

  Upon timer expiration, generate the signal *sigev\_signo* for the process. See [**sigevent**(3type)](/sigevent/3type.md) 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)](/timer_getoverrun/2.md) for more details.

- **SIGEV\_THREAD**

  Upon timer expiration, invoke *sigev\_notify\_function* as if it were the start function of a new thread. See [**sigevent**(3type)](/sigevent/3type.md) 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)](/clone/2.md) or [**gettid**(2)](/gettid/2.md). 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.

## RETURN VALUE

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.

## ERRORS

- **EAGAIN**

  Temporary error during kernel allocation of timer structures.

- **EINVAL**

  Clock ID, *sigev\_notify*, *sigev\_signo*, or *sigev\_notify\_thread\_id* is invalid.

- **ENOMEM**

  Could not allocate memory.

- **ENOTSUP**

  The kernel does not support creating a timer against this *clockid*.

- **EPERM**

  *clockid* was **CLOCK\_REALTIME\_ALARM** or **CLOCK\_BOOTTIME\_ALARM** but the caller did not have the **CAP\_WAKE\_ALARM** capability.

## VERSIONS

### C library/kernel differences

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

- •

  Much of the functionality for **SIGEV\_THREAD** is implemented within glibc, rather than the kernel. (This is necessarily so, since the thread involved in handling the notification is one that must be managed by the C library POSIX threads implementation.) Although the notification delivered to the process is via a thread, internally the NPTL implementation uses a *sigev\_notify* value of **SIGEV\_THREAD\_ID** along with a real-time signal that is reserved by the implementation (see [**nptl**(7)](/nptl/7.md)).

- •

  The implementation of the default case where *evp* is NULL is handled inside glibc, which invokes the underlying system call with a suitably populated *sigevent* structure.

- •

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

## STANDARDS

POSIX.1-2024.

## HISTORY

Linux 2.6. POSIX.1-2001.

Prior to Linux 2.6, glibc provided an incomplete user-space implementation (**CLOCK\_REALTIME** timers only) using POSIX threads, and before glibc 2.17, the implementation falls back to this technique on systems running kernels older than Linux 2.6.

## NOTES

A program may create multiple interval timers using **timer\_create**().

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

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)](/setrlimit/2.md)).

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)](/timer_settime/2.md)

  Arm (start) or disarm (stop) a timer.

- [**timer\_gettime**(2)](/timer_gettime/2.md)

  Fetch the time remaining until the next expiration of a timer, along with the interval setting of the timer.

- [**timer\_getoverrun**(2)](/timer_getoverrun/2.md)

  Return the overrun count for the last timer expiration.

- [**timer\_delete**(2)](/timer_delete/2.md)

  Disarm and delete a timer.

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)](/proc/5.md) for further information.

Since Linux 4.10, support for POSIX timers is a configurable option that is enabled by default. Kernel support can be disabled via the **CONFIG\_POSIX\_TIMERS** option.

## EXAMPLES

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 <err.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define CLOCKID CLOCK_REALTIME
#define SIG SIGRTMIN
static void
print_siginfo(siginfo_t *si)
{
    int      or;
    timer_t  *tidp;
    tidp = si->si_value.sival_ptr;
    printf("    sival_ptr = %p; ", si->si_value.sival_ptr);
    printf("    *sival_ptr = %#jx\n", (uintmax_t) *tidp);
    or = timer_getoverrun(*tidp);
    if (or == -1)
        err(EXIT_FAILURE, "timer_getoverrun");
    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 safe
       (and should not be done in production programs), since
       printf() is not async-signal-safe; see signal-safety(7).
       Nevertheless, we use printf() here as a simple way of
       showing that the handler was called.  */
    printf("Caught signal %d\n", sig);
    print_siginfo(si);
    signal(sig, SIG_IGN);
}
int
main(int argc, char *argv[])
{
    timer_t            timerid;
    sigset_t           mask;
    long long          freq_nanosecs;
    struct sigevent    sev;
    struct sigaction   sa;
    struct itimerspec  its;
    if (argc != 3) {
        fprintf(stderr, "Usage: %s <sleep-secs> <freq-nanosecs>\n",
                argv[0]);
        exit(EXIT_FAILURE);
    }
    /* Establish handler for timer signal.  */
    printf("Establishing handler for signal %d\n", SIG);
    sa.sa_flags = SA_SIGINFO;
    sa.sa_sigaction = handler;
    sigemptyset(&sa.sa_mask);
    if (sigaction(SIG, &sa, NULL) == -1)
        err(EXIT_FAILURE, "sigaction");
    /* Block timer signal temporarily.  */
    printf("Blocking signal %d\n", SIG);
    sigemptyset(&mask);
    sigaddset(&mask, SIG);
    if (sigprocmask(SIG_SETMASK, &mask, NULL) == -1)
        err(EXIT_FAILURE, "sigprocmask");
    /* 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)
        err(EXIT_FAILURE, "timer_create");
    printf("timer ID is %#jx\n", (uintmax_t) 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)
         err(EXIT_FAILURE, "timer_settime");
    /* Sleep for a while; meanwhile, the timer may expire
       multiple times.  */
    printf("Sleeping for %d seconds\n", atoi(argv[1]));
    sleep(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)
        err(EXIT_FAILURE, "sigprocmask");
    exit(EXIT_SUCCESS);
}
```

## SEE ALSO

[**clock\_gettime**(2)](/clock_gettime/2.md), [**setitimer**(2)](/setitimer/2.md), [**timer\_delete**(2)](/timer_delete/2.md), [**timer\_getoverrun**(2)](/timer_getoverrun/2.md), [**timer\_settime**(2)](/timer_settime/2.md), [**timerfd\_create**(2)](/timerfd_create/2.md), [**clock\_getcpuclockid**(3)](/clock_getcpuclockid/3.md), [**pthread\_getcpuclockid**(3)](/pthread_getcpuclockid/3.md), [**pthreads**(7)](/pthreads/7.md), [**sigevent**(3type)](/sigevent/3type.md), [**signal**(7)](/signal/7.md), [**time**(7)](/time/7.md)
