1 .\" Copyright (c) 2009 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .TH TIMER_CREATE 2 2009-02-20 Linux "Linux Programmer's Manual"
25 timer_create \- create a POSIX per-process timer
28 .B #include <signal.h>
31 .BI "int timer_create(clockid_t " clockid ", struct sigevent *" evp ,
32 .BI " timer_t *" timerid );
35 Link with \fI\-lrt\fP.
38 Feature Test Macro Requirements for glibc (see
39 .BR feature_test_macros (7)):
43 _POSIX_C_SOURCE >= 199309
46 creates a new per-process interval timer.
47 The ID of the new timer is returned in the buffer pointed to by
49 which must be a non-NULL pointer.
50 This ID is unique within the process, until the timer is deleted.
51 The new timer is initially disarmed.
55 argument specifies the clock that the new timer uses to measure time.
56 It can be specified as one of the following values:
59 A settable system-wide real-time clock.
62 A nonsettable monotonically increasing clock that measures time
63 from some unspecified point in the past that does not change
65 .\" Note: the CLOCK_MONOTONIC_RAW clock added for clock_gettime()
66 .\" in 2.6.28 is not supported for POSIX timers -- mtk, Feb 2009
68 .BR CLOCK_PROCESS_CPUTIME_ID " (since Linux 2.6.12)"
69 A clock that measures (user and system) CPU time consumed by
70 (all of the threads in) the calling process.
72 .BR CLOCK_THREAD_CPUTIME_ID " (since Linux 2.6.12)"
73 A clock that measures (user and system) CPU time consumed by
75 .\" The CLOCK_MONOTONIC_RAW that was added in 2.6.28 can't be used
76 .\" to create a timer -- mtk, Feb 2009
78 As well as the above values,
80 can be specified as the
83 .BR clock_getcpuclockid (3)
85 .BR pthread_getcpuclockid (3).
91 structure that specifies how the caller
92 should be notified when the timer expires.
93 This structure is defined something like the following:
103 int sigev_notify; /* Notification method */
104 int sigev_signo; /* Timer expiration signal */
105 union sigval sigev_value; /* Value accompanying signal or
106 passed to thread function */
107 void (*sigev_notify_function) (union sigval);
108 /* Function used for thread
109 notifications (SIGEV_THREAD) */
110 void *sigev_notify_attributes;
111 /* Attributes for notification thread
113 pid_t sigev_notify_thread_id;
114 /* ID of thread to signal (SIGEV_THREAD_ID) */
119 Some of these fields may be defined as part of a union:
120 a program should only employ those fields relevant
121 to the value specified in
123 This field can have the following values:
126 Don't asynchronously notify when the timer expires.
127 Progress of the timer can be monitored using
128 .BR timer_gettime (2).
131 Upon timer expiration, generate the signal
136 is a real-time signal,
137 then it will be accompanied by the data specified in
139 (like the signal-accompanying data for
141 At any point in time,
142 at most one signal is queued to the process for a given timer; see
143 .BR timer_getoverrun (2)
147 Upon timer expiration, invoke
148 .I sigev_notify_function
149 as if it were the start function of a new thread.
150 (Among the implementation possibilities here are that
151 each timer notification could result in the creation of a new thread,
152 or that a single thread is created to receive all notifications.)
153 The function is invoked with
155 as its sole argument.
157 .I sigev_notify_attributes
158 is not NULL, it should point to a
160 structure that defines attributes for the new thread (see
161 .BR pthread_attr_init (3)).
163 .BR SIGEV_THREAD_ID " (Linux-specific)"
166 but the signal is targeted at the thread whose ID is given in
167 .IR sigev_notify_thread_id ,
168 which must be a thread in the same process as the caller.
170 .IR sigev_notify_thread_id
171 field specifies a kernel thread ID, that is, the value returned by
175 This flag is only intended for use by threading libraries.
179 as NULL is equivalent to specifying a pointer to a
189 .I sigev_value.sival_int
194 returns 0, and the ID of the new timer is placed in
196 On failure, \-1 is returned, and
198 is set to indicate the error.
202 Temporary error during kernel allocation of timer structures.
209 .IR sigev_notify_thread_id
213 .\" glibc layer: malloc()
214 Could not allocate memory.
216 This system call is available since Linux 2.6.
220 A program may create multiple interval timers using
223 Timers are not inherited by the child of a
225 and are disarmed and deleted during an
228 The kernel preallocates a "queued real-time signal"
229 for each timer created using
231 Consequently, the number of timers is limited by the
232 .BR RLIMIT_SIGPENDING
236 The timers created by
238 are commonly known as "POSIX (interval) timers".
239 The POSIX timers API consists of the following interfaces:
244 .BR timer_settime (2):
245 Arm (start) or disarm (stop) a timer.
247 .BR timer_gettime (2):
248 Fetch the time remaining until the next expiration of a timer,
249 along with the interval setting of the timer.
251 .BR timer_getoverrun (2):
252 Return the overrun count for the last timer expiration.
254 .BR timer_delete (2):
255 Disarm and delete a timer.
257 Part of the implementation of the POSIX timers API is provided by glibc.
260 The functionality for
262 is implemented within glibc, rather than the kernel.
264 The timer IDs presented at user level are maintained by glibc,
265 which maps these IDs to the timer IDs employed by the kernel.
266 .\" See the glibc source file kernel-posix-timers.h for the structure
267 .\" that glibc uses to map userspace timer IDs to kernel timer IDs
268 .\" The kernel-level timer ID is exposed via siginfo.si_tid.
270 The POSIX timers system calls first appeared in Linux 2.6.
272 glibc provided an incomplete userspace implementation
274 timers only) using POSIX threads,
275 and current glibc falls back to this implementation on systems
276 running pre-2.6 Linux kernels.
278 The program below takes two arguments: a sleep period in seconds,
279 and a timer frequency in nanoseconds.
280 The program establishes a handler for the signal it uses for the timer,
282 creates and arms a timer that expires with the given frequency,
283 sleeps for the specified number of seconds,
284 and then unblocks the timer signal.
285 Assuming that the timer expired at least once while the program slept,
286 the signal handler will be invoked,
287 and the handler displays some information about the timer notification.
288 The program terminates after one invocation of the signal handler.
290 In the following example run, the program sleeps for 1 second,
291 after creating a timer that has a frequency of 100 nanoseconds.
292 By the time the signal is unblocked and delivered,
293 there have been around ten million overruns.
298 Establishing handler for signal 34
300 timer ID is 0x804c008
301 Sleeping for 1 seconds
304 sival_ptr = 0xbfb174f4; *sival_ptr = 0x804c008
305 overrun count = 10004886
317 #define CLOCKID CLOCK_REALTIME
320 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
324 print_siginfo(siginfo_t *si)
329 tidp = si\->si_value.sival_ptr;
331 printf(" sival_ptr = %p; ", si\->si_value.sival_ptr);
332 printf(" *sival_ptr = 0x%lx\\n", (long) *tidp);
334 or = timer_getoverrun(*tidp);
336 errExit("timer_getoverrun");
338 printf(" overrun count = %d\\n", or);
342 handler(int sig, siginfo_t *si, void *uc)
344 /* Note: calling printf() from a signal handler is not
345 strictly correct, since printf() is not async\-signal\-safe;
348 printf("Caught signal %d\\n", sig);
350 signal(sig, SIG_IGN);
354 main(int argc, char *argv[])
358 struct itimerspec its;
359 long long freq_nanosecs;
364 fprintf(stderr, "Usage: %s <sleep\-secs> <freq\-nanosecs>\\n",
369 /* Establish handler for timer signal */
371 printf("Establishing handler for signal %d\\n", SIG);
372 sa.sa_flags = SA_SIGINFO;
373 sa.sa_sigaction = handler;
374 sigemptyset(&sa.sa_mask);
375 if (sigaction(SIG, &sa, NULL) == \-1)
376 errExit("sigaction");
378 /* Block timer signal temporarily */
380 printf("Blocking signal %d\\n", SIG);
382 sigaddset(&mask, SIG);
383 if (sigprocmask(SIG_SETMASK, &mask, NULL) == \-1)
384 errExit("sigprocmask");
386 /* Create the timer */
388 sev.sigev_notify = SIGEV_SIGNAL;
389 sev.sigev_signo = SIG;
390 sev.sigev_value.sival_ptr = &timerid;
391 if (timer_create(CLOCKID, &sev, &timerid) == \-1)
392 errExit("timer_create");
394 printf("timer ID is 0x%lx\\n", (long) timerid);
396 /* Start the timer */
398 freq_nanosecs = atoll(argv[2]);
399 its.it_value.tv_sec = freq_nanosecs / 1000000000;
400 its.it_value.tv_nsec = freq_nanosecs % 1000000000;
401 its.it_interval.tv_sec = its.it_value.tv_sec;
402 its.it_interval.tv_nsec = its.it_value.tv_nsec;
404 if (timer_settime(timerid, 0, &its, NULL) == \-1)
405 errExit("timer_settime");
407 /* Sleep for a while; meanwhile, the timer may expire
410 printf("Sleeping for %d seconds\\n", atoi(argv[1]));
411 sleep(atoi(argv[1]));
413 /* Unlock the timer signal, so that timer notification
416 printf("Unblocking signal %d\\n", SIG);
417 if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == \-1)
418 errExit("sigprocmask");
424 .BR clock_gettime (2),
426 .BR timer_delete (2),
427 .BR timer_settime (2),
428 .BR timer_getoverrun (2),
429 .BR timerfd_create (2),
430 .BR clock_getcpuclockid (3),
431 .BR pthread_getcpuclockid (3),