1 .\" Copyright (c) 2009 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" FIXME Linux 2.6.39 adds CLOCK_BOOTTIME, which needs be documented
27 .\" Does this also affect timerfd_create()?
29 .\" FIXME Linux 3.0 adds CLOCK_BOOTTIME_ALARM and CLOCK_REALTIME_ALARM,
30 .\" which need be documented
31 .\" Does this also affect timerfd_create()?
33 .TH TIMER_CREATE 2 2015-07-23 Linux "Linux Programmer's Manual"
35 timer_create \- create a POSIX per-process timer
38 .B #include <signal.h>
41 .BI "int timer_create(clockid_t " clockid ", struct sigevent *" sevp ,
42 .BI " timer_t *" timerid );
45 Link with \fI\-lrt\fP.
48 Feature Test Macro Requirements for glibc (see
49 .BR feature_test_macros (7)):
53 _POSIX_C_SOURCE\ >=\ 199309L
56 creates a new per-process interval timer.
57 The ID of the new timer is returned in the buffer pointed to by
59 which must be a non-null pointer.
60 This ID is unique within the process, until the timer is deleted.
61 The new timer is initially disarmed.
65 argument specifies the clock that the new timer uses to measure time.
66 It can be specified as one of the following values:
69 A settable system-wide real-time clock.
72 A nonsettable monotonically increasing clock that measures time
73 from some unspecified point in the past that does not change
75 .\" Note: the CLOCK_MONOTONIC_RAW clock added for clock_gettime()
76 .\" in 2.6.28 is not supported for POSIX timers -- mtk, Feb 2009
78 .BR CLOCK_PROCESS_CPUTIME_ID " (since Linux 2.6.12)"
79 A clock that measures (user and system) CPU time consumed by
80 (all of the threads in) the calling process.
82 .BR CLOCK_THREAD_CPUTIME_ID " (since Linux 2.6.12)"
83 A clock that measures (user and system) CPU time consumed by
85 .\" The CLOCK_MONOTONIC_RAW that was added in 2.6.28 can't be used
86 .\" to create a timer -- mtk, Feb 2009
88 As well as the above values,
90 can be specified as the
93 .BR clock_getcpuclockid (3)
95 .BR pthread_getcpuclockid (3).
101 structure that specifies how the caller
102 should be notified when the timer expires.
103 For the definition and general details of this structure, see
108 field can have the following values:
111 Don't asynchronously notify when the timer expires.
112 Progress of the timer can be monitored using
113 .BR timer_gettime (2).
116 Upon timer expiration, generate the signal
126 structure will be set to
128 At any point in time,
129 at most one signal is queued to the process for a given timer; see
130 .BR timer_getoverrun (2)
134 Upon timer expiration, invoke
135 .I sigev_notify_function
136 as if it were the start function of a new thread.
141 .BR SIGEV_THREAD_ID " (Linux-specific)"
144 but the signal is targeted at the thread whose ID is given in
145 .IR sigev_notify_thread_id ,
146 which must be a thread in the same process as the caller.
148 .IR sigev_notify_thread_id
149 field specifies a kernel thread ID, that is, the value returned by
153 This flag is intended only for use by threading libraries.
157 as NULL is equivalent to specifying a pointer to a
167 .I sigev_value.sival_int
172 returns 0, and the ID of the new timer is placed in
174 On failure, \-1 is returned, and
176 is set to indicate the error.
180 Temporary error during kernel allocation of timer structures.
187 .IR sigev_notify_thread_id
191 .\" glibc layer: malloc()
192 Could not allocate memory.
194 This system call is available since Linux 2.6.
196 POSIX.1-2001, POSIX.1-2008.
198 A program may create multiple interval timers using
201 Timers are not inherited by the child of a
203 and are disarmed and deleted during an
206 The kernel preallocates a "queued real-time signal"
207 for each timer created using
209 Consequently, the number of timers is limited by the
210 .BR RLIMIT_SIGPENDING
214 The timers created by
216 are commonly known as "POSIX (interval) timers".
217 The POSIX timers API consists of the following interfaces:
222 .BR timer_settime (2):
223 Arm (start) or disarm (stop) a timer.
225 .BR timer_gettime (2):
226 Fetch the time remaining until the next expiration of a timer,
227 along with the interval setting of the timer.
229 .BR timer_getoverrun (2):
230 Return the overrun count for the last timer expiration.
232 .BR timer_delete (2):
233 Disarm and delete a timer.
235 Since Linux 3.10, the
236 .IR /proc/[pid]/timers
237 file can be used to list the POSIX timers for the process with PID
241 for further information.
243 .SS C library/kernel differences
244 Part of the implementation of the POSIX timers API is provided by glibc.
245 .\" See nptl/sysdeps/unix/sysv/linux/timer_create.c
248 Much of the functionality for
250 is implemented within glibc, rather than the kernel.
251 (This is necessarily so,
252 since the thread involved in handling the notification is one
253 that must be managed by the C library POSIX threads implementation.)
254 Although the notification delivered to the process is via a thread,
255 internally the NPTL implementation uses a
259 along with a real-time signal that is reserved by the implementation (see
262 The implementation of the default case where
264 is NULL is handled inside glibc,
265 which invokes the underlying system call with a suitably populated
269 The timer IDs presented at user level are maintained by glibc,
270 which maps these IDs to the timer IDs employed by the kernel.
271 .\" See the glibc source file kernel-posix-timers.h for the structure
272 .\" that glibc uses to map user-space timer IDs to kernel timer IDs
273 .\" The kernel-level timer ID is exposed via siginfo.si_tid.
275 The POSIX timers system calls first appeared in Linux 2.6.
277 glibc provided an incomplete user-space implementation
279 timers only) using POSIX threads,
280 and in glibc versions before 2.17,
281 .\" glibc commit 93a78ac437ba44f493333d7e2a4b0249839ce460
282 the implementation falls back to this technique on systems
283 running pre-2.6 Linux kernels.
285 The program below takes two arguments: a sleep period in seconds,
286 and a timer frequency in nanoseconds.
287 The program establishes a handler for the signal it uses for the timer,
289 creates and arms a timer that expires with the given frequency,
290 sleeps for the specified number of seconds,
291 and then unblocks the timer signal.
292 Assuming that the timer expired at least once while the program slept,
293 the signal handler will be invoked,
294 and the handler displays some information about the timer notification.
295 The program terminates after one invocation of the signal handler.
297 In the following example run, the program sleeps for 1 second,
298 after creating a timer that has a frequency of 100 nanoseconds.
299 By the time the signal is unblocked and delivered,
300 there have been around ten million overruns.
304 $ \fB./a.out 1 100\fP
305 Establishing handler for signal 34
307 timer ID is 0x804c008
308 Sleeping for 1 seconds
311 sival_ptr = 0xbfb174f4; *sival_ptr = 0x804c008
312 overrun count = 10004886
324 #define CLOCKID CLOCK_REALTIME
327 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
331 print_siginfo(siginfo_t *si)
336 tidp = si\->si_value.sival_ptr;
338 printf(" sival_ptr = %p; ", si\->si_value.sival_ptr);
339 printf(" *sival_ptr = 0x%lx\\n", (long) *tidp);
341 or = timer_getoverrun(*tidp);
343 errExit("timer_getoverrun");
345 printf(" overrun count = %d\\n", or);
349 handler(int sig, siginfo_t *si, void *uc)
351 /* Note: calling printf() from a signal handler is not
352 strictly correct, since printf() is not async\-signal\-safe;
355 printf("Caught signal %d\\n", sig);
357 signal(sig, SIG_IGN);
361 main(int argc, char *argv[])
365 struct itimerspec its;
366 long long freq_nanosecs;
371 fprintf(stderr, "Usage: %s <sleep\-secs> <freq\-nanosecs>\\n",
376 /* Establish handler for timer signal */
378 printf("Establishing handler for signal %d\\n", SIG);
379 sa.sa_flags = SA_SIGINFO;
380 sa.sa_sigaction = handler;
381 sigemptyset(&sa.sa_mask);
382 if (sigaction(SIG, &sa, NULL) == \-1)
383 errExit("sigaction");
385 /* Block timer signal temporarily */
387 printf("Blocking signal %d\\n", SIG);
389 sigaddset(&mask, SIG);
390 if (sigprocmask(SIG_SETMASK, &mask, NULL) == \-1)
391 errExit("sigprocmask");
393 /* Create the timer */
395 sev.sigev_notify = SIGEV_SIGNAL;
396 sev.sigev_signo = SIG;
397 sev.sigev_value.sival_ptr = &timerid;
398 if (timer_create(CLOCKID, &sev, &timerid) == \-1)
399 errExit("timer_create");
401 printf("timer ID is 0x%lx\\n", (long) timerid);
403 /* Start the timer */
405 freq_nanosecs = atoll(argv[2]);
406 its.it_value.tv_sec = freq_nanosecs / 1000000000;
407 its.it_value.tv_nsec = freq_nanosecs % 1000000000;
408 its.it_interval.tv_sec = its.it_value.tv_sec;
409 its.it_interval.tv_nsec = its.it_value.tv_nsec;
411 if (timer_settime(timerid, 0, &its, NULL) == \-1)
412 errExit("timer_settime");
414 /* Sleep for a while; meanwhile, the timer may expire
417 printf("Sleeping for %d seconds\\n", atoi(argv[1]));
418 sleep(atoi(argv[1]));
420 /* Unlock the timer signal, so that timer notification
423 printf("Unblocking signal %d\\n", SIG);
424 if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == \-1)
425 errExit("sigprocmask");
433 .BR clock_gettime (2),
435 .BR timer_delete (2),
436 .BR timer_getoverrun (2),
437 .BR timer_settime (2),
438 .BR timerfd_create (2),
439 .BR clock_getcpuclockid (3),
440 .BR pthread_getcpuclockid (3),