1 .\" Copyright (c) 2009 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH TIMER_CREATE 2 2022-09-17 "Linux man-pages (unreleased)"
8 timer_create \- create a POSIX per-process timer
11 .RI ( librt ", " \-lrt )
14 .BR "#include <signal.h>" " /* Definition of " SIGEV_* " constants */"
17 .BI "int timer_create(clockid_t " clockid ", struct sigevent *restrict " sevp ,
18 .BI " timer_t *restrict " timerid );
22 Feature Test Macro Requirements for glibc (see
23 .BR feature_test_macros (7)):
28 _POSIX_C_SOURCE >= 199309L
32 creates a new per-process interval timer.
33 The ID of the new timer is returned in the buffer pointed to by
35 which must be a non-null pointer.
36 This ID is unique within the process, until the timer is deleted.
37 The new timer is initially disarmed.
41 argument specifies the clock that the new timer uses to measure time.
42 It can be specified as one of the following values:
45 A settable system-wide real-time clock.
48 A nonsettable monotonically increasing clock that measures time
49 from some unspecified point in the past that does not change
51 .\" Note: the CLOCK_MONOTONIC_RAW clock added for clock_gettime()
52 .\" in 2.6.28 is not supported for POSIX timers -- mtk, Feb 2009
54 .BR CLOCK_PROCESS_CPUTIME_ID " (since Linux 2.6.12)"
55 A clock that measures (user and system) CPU time consumed by
56 (all of the threads in) the calling process.
58 .BR CLOCK_THREAD_CPUTIME_ID " (since Linux 2.6.12)"
59 A clock that measures (user and system) CPU time consumed by
61 .\" The CLOCK_MONOTONIC_RAW that was added in 2.6.28 can't be used
62 .\" to create a timer -- mtk, Feb 2009
64 .BR CLOCK_BOOTTIME " (Since Linux 2.6.39)"
65 .\" commit 70a08cca1227dc31c784ec930099a4417a06e7d0
68 this is a monotonically increasing clock.
71 clock does not measure the time while a system is suspended, the
73 clock does include the time during which the system is suspended.
74 This is useful for applications that need to be suspend-aware.
76 is not suitable for such applications, since that clock is affected
77 by discontinuous changes to the system clock.
79 .BR CLOCK_REALTIME_ALARM " (since Linux 3.0)"
80 .\" commit 9a7adcf5c6dea63d2e47e6f6d2f7a6c9f48b9337
83 but will wake the system if it is suspended.
84 The caller must have the
86 capability in order to set a timer against this clock.
88 .BR CLOCK_BOOTTIME_ALARM " (since Linux 3.0)"
89 .\" commit 9a7adcf5c6dea63d2e47e6f6d2f7a6c9f48b9337
92 but will wake the system if it is suspended.
93 The caller must have the
95 capability in order to set a timer against this clock.
97 .BR CLOCK_TAI " (since Linux 3.10)"
98 A system-wide clock derived from wall-clock time but ignoring leap seconds.
102 for some further details on the above clocks.
104 As well as the above values,
106 can be specified as the
108 returned by a call to
109 .BR clock_getcpuclockid (3)
111 .BR pthread_getcpuclockid (3).
117 structure that specifies how the caller
118 should be notified when the timer expires.
119 For the definition and general details of this structure, see
124 field can have the following values:
127 Don't asynchronously notify when the timer expires.
128 Progress of the timer can be monitored using
129 .BR timer_gettime (2).
132 Upon timer expiration, generate the signal
142 structure will be set to
144 At any point in time,
145 at most one signal is queued to the process for a given timer; see
146 .BR timer_getoverrun (2)
150 Upon timer expiration, invoke
151 .I sigev_notify_function
152 as if it were the start function of a new thread.
157 .BR SIGEV_THREAD_ID " (Linux-specific)"
160 but the signal is targeted at the thread whose ID is given in
161 .IR sigev_notify_thread_id ,
162 which must be a thread in the same process as the caller.
164 .I sigev_notify_thread_id
165 field specifies a kernel thread ID, that is, the value returned by
169 This flag is intended only for use by threading libraries.
173 as NULL is equivalent to specifying a pointer to a
183 .I sigev_value.sival_int
188 returns 0, and the ID of the new timer is placed in
190 On failure, \-1 is returned, and
192 is set to indicate the error.
196 Temporary error during kernel allocation of timer structures.
203 .I sigev_notify_thread_id
207 .\" glibc layer: malloc()
208 Could not allocate memory.
211 The kernel does not support creating a timer against this
217 .B CLOCK_REALTIME_ALARM
219 .B CLOCK_BOOTTIME_ALARM
220 but the caller did not have the
224 This system call is available since Linux 2.6.
226 POSIX.1-2001, POSIX.1-2008.
228 A program may create multiple interval timers using
231 Timers are not inherited by the child of a
233 and are disarmed and deleted during an
236 The kernel preallocates a "queued real-time signal"
237 for each timer created using
239 Consequently, the number of timers is limited by the
244 The timers created by
246 are commonly known as "POSIX (interval) timers".
247 The POSIX timers API consists of the following interfaces:
252 .BR timer_settime (2):
253 Arm (start) or disarm (stop) a timer.
255 .BR timer_gettime (2):
256 Fetch the time remaining until the next expiration of a timer,
257 along with the interval setting of the timer.
259 .BR timer_getoverrun (2):
260 Return the overrun count for the last timer expiration.
262 .BR timer_delete (2):
263 Disarm and delete a timer.
265 Since Linux 3.10, the
266 .IR /proc/ pid /timers
267 file can be used to list the POSIX timers for the process with PID
271 for further information.
274 .\" baa73d9e478ff32d62f3f9422822b59dd9a95a21
275 support for POSIX timers is a configurable option that is enabled by default.
276 Kernel support can be disabled via the
277 .B CONFIG_POSIX_TIMERS
280 .SS C library/kernel differences
281 Part of the implementation of the POSIX timers API is provided by glibc.
282 .\" See nptl/sysdeps/unix/sysv/linux/timer_create.c
285 Much of the functionality for
287 is implemented within glibc, rather than the kernel.
288 (This is necessarily so,
289 since the thread involved in handling the notification is one
290 that must be managed by the C library POSIX threads implementation.)
291 Although the notification delivered to the process is via a thread,
292 internally the NPTL implementation uses a
296 along with a real-time signal that is reserved by the implementation (see
299 The implementation of the default case where
301 is NULL is handled inside glibc,
302 which invokes the underlying system call with a suitably populated
306 The timer IDs presented at user level are maintained by glibc,
307 which maps these IDs to the timer IDs employed by the kernel.
308 .\" See the glibc source file kernel-posix-timers.h for the structure
309 .\" that glibc uses to map user-space timer IDs to kernel timer IDs
310 .\" The kernel-level timer ID is exposed via siginfo.si_tid.
312 The POSIX timers system calls first appeared in Linux 2.6.
314 glibc provided an incomplete user-space implementation
316 timers only) using POSIX threads,
317 and in glibc versions before 2.17,
318 .\" glibc commit 93a78ac437ba44f493333d7e2a4b0249839ce460
319 the implementation falls back to this technique on systems
320 running pre-2.6 Linux kernels.
322 The program below takes two arguments: a sleep period in seconds,
323 and a timer frequency in nanoseconds.
324 The program establishes a handler for the signal it uses for the timer,
326 creates and arms a timer that expires with the given frequency,
327 sleeps for the specified number of seconds,
328 and then unblocks the timer signal.
329 Assuming that the timer expired at least once while the program slept,
330 the signal handler will be invoked,
331 and the handler displays some information about the timer notification.
332 The program terminates after one invocation of the signal handler.
334 In the following example run, the program sleeps for 1 second,
335 after creating a timer that has a frequency of 100 nanoseconds.
336 By the time the signal is unblocked and delivered,
337 there have been around ten million overruns.
341 $ \fB./a.out 1 100\fP
342 Establishing handler for signal 34
344 timer ID is 0x804c008
345 Sleeping for 1 seconds
348 sival_ptr = 0xbfb174f4; *sival_ptr = 0x804c008
349 overrun count = 10004886
354 .\" SRC BEGIN (timer_create.c)
363 #define CLOCKID CLOCK_REALTIME
366 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
370 print_siginfo(siginfo_t *si)
375 tidp = si\->si_value.sival_ptr;
377 printf(" sival_ptr = %p; ", si\->si_value.sival_ptr);
378 printf(" *sival_ptr = %#jx\en", (uintmax_t) *tidp);
380 or = timer_getoverrun(*tidp);
382 errExit("timer_getoverrun");
384 printf(" overrun count = %d\en", or);
388 handler(int sig, siginfo_t *si, void *uc)
390 /* Note: calling printf() from a signal handler is not safe
391 (and should not be done in production programs), since
392 printf() is not async\-signal\-safe; see signal\-safety(7).
393 Nevertheless, we use printf() here as a simple way of
394 showing that the handler was called. */
396 printf("Caught signal %d\en", sig);
398 signal(sig, SIG_IGN);
402 main(int argc, char *argv[])
406 long long freq_nanosecs;
409 struct itimerspec its;
412 fprintf(stderr, "Usage: %s <sleep\-secs> <freq\-nanosecs>\en",
417 /* Establish handler for timer signal. */
419 printf("Establishing handler for signal %d\en", SIG);
420 sa.sa_flags = SA_SIGINFO;
421 sa.sa_sigaction = handler;
422 sigemptyset(&sa.sa_mask);
423 if (sigaction(SIG, &sa, NULL) == \-1)
424 errExit("sigaction");
426 /* Block timer signal temporarily. */
428 printf("Blocking signal %d\en", SIG);
430 sigaddset(&mask, SIG);
431 if (sigprocmask(SIG_SETMASK, &mask, NULL) == \-1)
432 errExit("sigprocmask");
434 /* Create the timer. */
436 sev.sigev_notify = SIGEV_SIGNAL;
437 sev.sigev_signo = SIG;
438 sev.sigev_value.sival_ptr = &timerid;
439 if (timer_create(CLOCKID, &sev, &timerid) == \-1)
440 errExit("timer_create");
442 printf("timer ID is %#jx\en", (uintmax_t) timerid);
444 /* Start the timer. */
446 freq_nanosecs = atoll(argv[2]);
447 its.it_value.tv_sec = freq_nanosecs / 1000000000;
448 its.it_value.tv_nsec = freq_nanosecs % 1000000000;
449 its.it_interval.tv_sec = its.it_value.tv_sec;
450 its.it_interval.tv_nsec = its.it_value.tv_nsec;
452 if (timer_settime(timerid, 0, &its, NULL) == \-1)
453 errExit("timer_settime");
455 /* Sleep for a while; meanwhile, the timer may expire
458 printf("Sleeping for %d seconds\en", atoi(argv[1]));
459 sleep(atoi(argv[1]));
461 /* Unlock the timer signal, so that timer notification
464 printf("Unblocking signal %d\en", SIG);
465 if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == \-1)
466 errExit("sigprocmask");
475 .BR clock_gettime (2),
477 .BR timer_delete (2),
478 .BR timer_getoverrun (2),
479 .BR timer_settime (2),
480 .BR timerfd_create (2),
481 .BR clock_getcpuclockid (3),
482 .BR pthread_getcpuclockid (3),