1TIMER_CREATE(2)            Linux Programmer's Manual           TIMER_CREATE(2)
2
3
4

NAME

6       timer_create - create a POSIX per-process timer
7

SYNOPSIS

9       #include <signal.h>
10       #include <time.h>
11
12       int timer_create(clockid_t clockid, struct sigevent *sevp,
13                        timer_t *timerid);
14
15       Link with -lrt.
16
17   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19       timer_create(): _POSIX_C_SOURCE >= 199309L
20

DESCRIPTION

22       timer_create() creates a new per-process interval timer.  The ID of the
23       new timer is returned in the buffer pointed to by timerid,  which  must
24       be a non-null pointer.  This ID is unique within the process, until the
25       timer is deleted.  The new timer is initially disarmed.
26
27       The clockid argument specifies the clock that the  new  timer  uses  to
28       measure time.  It can be specified as one of the following values:
29
30       CLOCK_REALTIME
31              A settable system-wide real-time clock.
32
33       CLOCK_MONOTONIC
34              A  nonsettable monotonically increasing clock that measures time
35              from some unspecified point in the past that does not change af‐
36              ter system startup.
37
38       CLOCK_PROCESS_CPUTIME_ID (since Linux 2.6.12)
39              A  clock  that  measures  (user and system) CPU time consumed by
40              (all of the threads in) the calling process.
41
42       CLOCK_THREAD_CPUTIME_ID (since Linux 2.6.12)
43              A clock that measures (user and system) CPU time consumed by the
44              calling thread.
45
46       CLOCK_BOOTTIME (Since Linux 2.6.39)
47              Like  CLOCK_MONOTONIC, this is a monotonically increasing clock.
48              However, whereas the CLOCK_MONOTONIC clock does not measure  the
49              time  while a system is suspended, the CLOCK_BOOTTIME clock does
50              include the time during which the system is suspended.  This  is
51              useful   for   applications   that  need  to  be  suspend-aware.
52              CLOCK_REALTIME is not suitable for such applications, since that
53              clock is affected by discontinuous changes to the system clock.
54
55       CLOCK_REALTIME_ALARM (since Linux 3.0)
56              This  clock  is like CLOCK_REALTIME, but will wake the system if
57              it is suspended.  The caller must have the CAP_WAKE_ALARM  capa‐
58              bility in order to set a timer against this clock.
59
60       CLOCK_BOOTTIME_ALARM (since Linux 3.0)
61              This  clock  is like CLOCK_BOOTTIME, but will wake the system if
62              it is suspended.  The caller must have the CAP_WAKE_ALARM  capa‐
63              bility in order to set a timer against this clock.
64
65       CLOCK_TAI (since Linux 3.10)
66              A  system-wide  clock  derived from wall-clock time but ignoring
67              leap seconds.
68
69       See clock_getres(2) for some further details on the above clocks.
70
71       As well as the above values, clockid can be specified  as  the  clockid
72       returned   by  a  call  to  clock_getcpuclockid(3)  or  pthread_getcpu‐
73       clockid(3).
74
75       The sevp argument points to a sigevent structure that specifies how the
76       caller  should  be notified when the timer expires.  For the definition
77       and general details of this structure, see sigevent(7).
78
79       The sevp.sigev_notify field can have the following values:
80
81       SIGEV_NONE
82              Don't asynchronously notify when the timer expires.  Progress of
83              the timer can be monitored using timer_gettime(2).
84
85       SIGEV_SIGNAL
86              Upon  timer  expiration, generate the signal sigev_signo for the
87              process.  See sigevent(7)  for  general  details.   The  si_code
88              field  of  the  siginfo_t structure will be set to SI_TIMER.  At
89              any point in time, at most one signal is queued to  the  process
90              for a given timer; see timer_getoverrun(2) for more details.
91
92       SIGEV_THREAD
93              Upon  timer  expiration,  invoke  sigev_notify_function as if it
94              were the start function of a new thread.   See  sigevent(7)  for
95              details.
96
97       SIGEV_THREAD_ID (Linux-specific)
98              As  for  SIGEV_SIGNAL,  but the signal is targeted at the thread
99              whose ID is given in sigev_notify_thread_id,  which  must  be  a
100              thread  in  the  same  process  as  the  caller.   The sigev_no‐
101              tify_thread_id field specifies a kernel thread ID, that is,  the
102              value  returned by clone(2) or gettid(2).  This flag is intended
103              only for use by threading libraries.
104
105       Specifying sevp as NULL is equivalent to  specifying  a  pointer  to  a
106       sigevent  structure  in which sigev_notify is SIGEV_SIGNAL, sigev_signo
107       is SIGALRM, and sigev_value.sival_int is the timer ID.
108

RETURN VALUE

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

ERRORS

115       EAGAIN Temporary error during kernel allocation of timer structures.
116
117       EINVAL Clock ID, sigev_notify, sigev_signo,  or  sigev_notify_thread_id
118              is invalid.
119
120       ENOMEM Could not allocate memory.
121
122       ENOTSUP
123              The  kernel  does  not  support  creating  a  timer against this
124              clockid.
125
126       EPERM  clockid was CLOCK_REALTIME_ALARM or CLOCK_BOOTTIME_ALARM but the
127              caller did not have the CAP_WAKE_ALARM capability.
128

VERSIONS

130       This system call is available since Linux 2.6.
131

CONFORMING TO

133       POSIX.1-2001, POSIX.1-2008.
134

NOTES

136       A program may create multiple interval timers using timer_create().
137
138       Timers  are  not  inherited by the child of a fork(2), and are disarmed
139       and deleted during an execve(2).
140
141       The kernel preallocates a "queued real-time signal" for each timer cre‐
142       ated  using timer_create().  Consequently, the number of timers is lim‐
143       ited by the RLIMIT_SIGPENDING resource limit (see setrlimit(2)).
144
145       The timers created by timer_create() are commonly known as "POSIX  (in‐
146       terval) timers".  The POSIX timers API consists of the following inter‐
147       faces:
148
149       *  timer_create(): Create a timer.
150
151       *  timer_settime(2): Arm (start) or disarm (stop) a timer.
152
153       *  timer_gettime(2): Fetch the time remaining until the next expiration
154          of a timer, along with the interval setting of the timer.
155
156       *  timer_getoverrun(2): Return the overrun count for the last timer ex‐
157          piration.
158
159       *  timer_delete(2): Disarm and delete a timer.
160
161       Since Linux 3.10, the /proc/[pid]/timers file can be used to  list  the
162       POSIX timers for the process with PID pid.  See proc(5) for further in‐
163       formation.
164
165       Since Linux 4.10, support for POSIX timers  is  a  configurable  option
166       that  is  enabled  by  default.  Kernel support can be disabled via the
167       CONFIG_POSIX_TIMERS option.
168
169   C library/kernel differences
170       Part of the implementation of the  POSIX  timers  API  is  provided  by
171       glibc.  In particular:
172
173       *  Much  of  the  functionality  for SIGEV_THREAD is implemented within
174          glibc, rather than the kernel.  (This is necessarily so,  since  the
175          thread  involved  in  handling  the notification is one that must be
176          managed by the C library POSIX  threads  implementation.)   Although
177          the  notification  delivered  to the process is via a thread, inter‐
178          nally  the  NPTL  implementation  uses  a  sigev_notify   value   of
179          SIGEV_THREAD_ID  along  with  a real-time signal that is reserved by
180          the implementation (see nptl(7)).
181
182       *  The implementation of the default case where evp is NULL is  handled
183          inside  glibc, which invokes the underlying system call with a suit‐
184          ably populated sigevent structure.
185
186       *  The timer IDs presented at user level are maintained by glibc, which
187          maps these IDs to the timer IDs employed by the kernel.
188
189       The  POSIX  timers  system calls first appeared in Linux 2.6.  Prior to
190       this, glibc provided an incomplete user-space implementation (CLOCK_RE‐
191       ALTIME  timers  only) using POSIX threads, and in glibc versions before
192       2.17, the implementation falls back to this technique on  systems  run‐
193       ning pre-2.6 Linux kernels.
194

EXAMPLES

196       The program below takes two arguments: a sleep period in seconds, and a
197       timer frequency in nanoseconds.  The program establishes a handler  for
198       the  signal it uses for the timer, blocks that signal, creates and arms
199       a timer that expires with the given frequency, sleeps for the specified
200       number  of  seconds, and then unblocks the timer signal.  Assuming that
201       the timer expired at least once while the  program  slept,  the  signal
202       handler  will  be  invoked,  and  the handler displays some information
203       about the timer notification.  The program terminates after one invoca‐
204       tion of the signal handler.
205
206       In  the  following  example run, the program sleeps for 1 second, after
207       creating a timer that has a frequency of 100 nanoseconds.  By the  time
208       the  signal is unblocked and delivered, there have been around ten mil‐
209       lion overruns.
210
211           $ ./a.out 1 100
212           Establishing handler for signal 34
213           Blocking signal 34
214           timer ID is 0x804c008
215           Sleeping for 1 seconds
216           Unblocking signal 34
217           Caught signal 34
218               sival_ptr = 0xbfb174f4;     *sival_ptr = 0x804c008
219               overrun count = 10004886
220
221   Program source
222
223       #include <stdint.h>
224       #include <stdlib.h>
225       #include <unistd.h>
226       #include <stdio.h>
227       #include <signal.h>
228       #include <time.h>
229
230       #define CLOCKID CLOCK_REALTIME
231       #define SIG SIGRTMIN
232
233       #define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \
234                               } while (0)
235
236       static void
237       print_siginfo(siginfo_t *si)
238       {
239           timer_t *tidp;
240           int or;
241
242           tidp = si->si_value.sival_ptr;
243
244           printf("    sival_ptr = %p; ", si->si_value.sival_ptr);
245           printf("    *sival_ptr = %#jx\n", (uintmax_t) *tidp);
246
247           or = timer_getoverrun(*tidp);
248           if (or == -1)
249               errExit("timer_getoverrun");
250           else
251               printf("    overrun count = %d\n", or);
252       }
253
254       static void
255       handler(int sig, siginfo_t *si, void *uc)
256       {
257           /* Note: calling printf() from a signal handler is not safe
258              (and should not be done in production programs), since
259              printf() is not async-signal-safe; see signal-safety(7).
260              Nevertheless, we use printf() here as a simple way of
261              showing that the handler was called. */
262
263           printf("Caught signal %d\n", sig);
264           print_siginfo(si);
265           signal(sig, SIG_IGN);
266       }
267
268       int
269       main(int argc, char *argv[])
270       {
271           timer_t timerid;
272           struct sigevent sev;
273           struct itimerspec its;
274           long long freq_nanosecs;
275           sigset_t mask;
276           struct sigaction sa;
277
278           if (argc != 3) {
279               fprintf(stderr, "Usage: %s <sleep-secs> <freq-nanosecs>\n",
280                       argv[0]);
281               exit(EXIT_FAILURE);
282           }
283
284           /* Establish handler for timer signal */
285
286           printf("Establishing handler for signal %d\n", SIG);
287           sa.sa_flags = SA_SIGINFO;
288           sa.sa_sigaction = handler;
289           sigemptyset(&sa.sa_mask);
290           if (sigaction(SIG, &sa, NULL) == -1)
291               errExit("sigaction");
292
293           /* Block timer signal temporarily */
294
295           printf("Blocking signal %d\n", SIG);
296           sigemptyset(&mask);
297           sigaddset(&mask, SIG);
298           if (sigprocmask(SIG_SETMASK, &mask, NULL) == -1)
299               errExit("sigprocmask");
300
301           /* Create the timer */
302
303           sev.sigev_notify = SIGEV_SIGNAL;
304           sev.sigev_signo = SIG;
305           sev.sigev_value.sival_ptr = &timerid;
306           if (timer_create(CLOCKID, &sev, &timerid) == -1)
307               errExit("timer_create");
308
309           printf("timer ID is %#jx\n", (uintmax_t) timerid);
310
311           /* Start the timer */
312
313           freq_nanosecs = atoll(argv[2]);
314           its.it_value.tv_sec = freq_nanosecs / 1000000000;
315           its.it_value.tv_nsec = freq_nanosecs % 1000000000;
316           its.it_interval.tv_sec = its.it_value.tv_sec;
317           its.it_interval.tv_nsec = its.it_value.tv_nsec;
318
319           if (timer_settime(timerid, 0, &its, NULL) == -1)
320                errExit("timer_settime");
321
322           /* Sleep for a while; meanwhile, the timer may expire
323              multiple times */
324
325           printf("Sleeping for %d seconds\n", atoi(argv[1]));
326           sleep(atoi(argv[1]));
327
328           /* Unlock the timer signal, so that timer notification
329              can be delivered */
330
331           printf("Unblocking signal %d\n", SIG);
332           if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1)
333               errExit("sigprocmask");
334
335           exit(EXIT_SUCCESS);
336       }
337

SEE ALSO

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

COLOPHON

344       This page is part of release 5.10 of the Linux man-pages project.  A
345       description of the project, information about reporting bugs, and the
346       latest version of this page, can be found at
347       https://www.kernel.org/doc/man-pages/.
348
349
350
351Linux                             2020-11-01                   TIMER_CREATE(2)
Impressum