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 2014-08-19 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.
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 ABI differences
244 Part of the implementation of the POSIX timers API is provided by glibc.
247 The functionality for
249 is implemented within glibc, rather than the kernel.
251 The timer IDs presented at user level are maintained by glibc,
252 which maps these IDs to the timer IDs employed by the kernel.
253 .\" See the glibc source file kernel-posix-timers.h for the structure
254 .\" that glibc uses to map user-space timer IDs to kernel timer IDs
255 .\" The kernel-level timer ID is exposed via siginfo.si_tid.
257 The POSIX timers system calls first appeared in Linux 2.6.
259 glibc provided an incomplete user-space implementation
261 timers only) using POSIX threads,
262 and current glibc falls back to this implementation on systems
263 running pre-2.6 Linux kernels.
265 The program below takes two arguments: a sleep period in seconds,
266 and a timer frequency in nanoseconds.
267 The program establishes a handler for the signal it uses for the timer,
269 creates and arms a timer that expires with the given frequency,
270 sleeps for the specified number of seconds,
271 and then unblocks the timer signal.
272 Assuming that the timer expired at least once while the program slept,
273 the signal handler will be invoked,
274 and the handler displays some information about the timer notification.
275 The program terminates after one invocation of the signal handler.
277 In the following example run, the program sleeps for 1 second,
278 after creating a timer that has a frequency of 100 nanoseconds.
279 By the time the signal is unblocked and delivered,
280 there have been around ten million overruns.
284 $ \fB./a.out 1 100\fP
285 Establishing handler for signal 34
287 timer ID is 0x804c008
288 Sleeping for 1 seconds
291 sival_ptr = 0xbfb174f4; *sival_ptr = 0x804c008
292 overrun count = 10004886
304 #define CLOCKID CLOCK_REALTIME
307 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
311 print_siginfo(siginfo_t *si)
316 tidp = si\->si_value.sival_ptr;
318 printf(" sival_ptr = %p; ", si\->si_value.sival_ptr);
319 printf(" *sival_ptr = 0x%lx\\n", (long) *tidp);
321 or = timer_getoverrun(*tidp);
323 errExit("timer_getoverrun");
325 printf(" overrun count = %d\\n", or);
329 handler(int sig, siginfo_t *si, void *uc)
331 /* Note: calling printf() from a signal handler is not
332 strictly correct, since printf() is not async\-signal\-safe;
335 printf("Caught signal %d\\n", sig);
337 signal(sig, SIG_IGN);
341 main(int argc, char *argv[])
345 struct itimerspec its;
346 long long freq_nanosecs;
351 fprintf(stderr, "Usage: %s <sleep\-secs> <freq\-nanosecs>\\n",
356 /* Establish handler for timer signal */
358 printf("Establishing handler for signal %d\\n", SIG);
359 sa.sa_flags = SA_SIGINFO;
360 sa.sa_sigaction = handler;
361 sigemptyset(&sa.sa_mask);
362 if (sigaction(SIG, &sa, NULL) == \-1)
363 errExit("sigaction");
365 /* Block timer signal temporarily */
367 printf("Blocking signal %d\\n", SIG);
369 sigaddset(&mask, SIG);
370 if (sigprocmask(SIG_SETMASK, &mask, NULL) == \-1)
371 errExit("sigprocmask");
373 /* Create the timer */
375 sev.sigev_notify = SIGEV_SIGNAL;
376 sev.sigev_signo = SIG;
377 sev.sigev_value.sival_ptr = &timerid;
378 if (timer_create(CLOCKID, &sev, &timerid) == \-1)
379 errExit("timer_create");
381 printf("timer ID is 0x%lx\\n", (long) timerid);
383 /* Start the timer */
385 freq_nanosecs = atoll(argv[2]);
386 its.it_value.tv_sec = freq_nanosecs / 1000000000;
387 its.it_value.tv_nsec = freq_nanosecs % 1000000000;
388 its.it_interval.tv_sec = its.it_value.tv_sec;
389 its.it_interval.tv_nsec = its.it_value.tv_nsec;
391 if (timer_settime(timerid, 0, &its, NULL) == \-1)
392 errExit("timer_settime");
394 /* Sleep for a while; meanwhile, the timer may expire
397 printf("Sleeping for %d seconds\\n", atoi(argv[1]));
398 sleep(atoi(argv[1]));
400 /* Unlock the timer signal, so that timer notification
403 printf("Unblocking signal %d\\n", SIG);
404 if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == \-1)
405 errExit("sigprocmask");
413 .BR clock_gettime (2),
415 .BR timer_delete (2),
416 .BR timer_getoverrun (2),
417 .BR timer_settime (2),
418 .BR timerfd_create (2),
419 .BR clock_getcpuclockid (3),
420 .BR pthread_getcpuclockid (3),
426 This page is part of release 3.79 of the Linux
429 A description of the project,
430 information about reporting bugs,
431 and the latest version of this page,
433 \%http://www.kernel.org/doc/man\-pages/.