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.
24 .\" FIXME: Linux 2.6.39 adds CLOCK_BOOTTIME
25 .\" Does this also affect timerfd_create()?
26 .\" FIXME: Linux 2.3.0 adds CLOCK_BOOTTIME_ALARM and CLOCK_REALTIME_ALARM
27 .\" Does this also affect timerfd_create()?
29 .\"*******************************************************************
31 .\" This file was generated with po4a. Translate the source file.
33 .\"*******************************************************************
34 .TH TIMER_CREATE 2 2010\-09\-27 Linux "Linux Programmer's Manual"
36 timer_create \- create a POSIX per\-process timer
39 \fB#include <signal.h>\fP
40 \fB#include <time.h>\fP
42 \fBint timer_create(clockid_t \fP\fIclockid\fP\fB, struct sigevent *\fP\fIsevp\fP\fB,\fP
43 \fB timer_t *\fP\fItimerid\fP\fB);\fP
46 Link with \fI\-lrt\fP.
49 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照):
52 \fBtimer_create\fP(): _POSIX_C_SOURCE\ >=\ 199309L
54 \fBtimer_create\fP() creates a new per\-process interval timer. The ID of the
55 new timer is returned in the buffer pointed to by \fItimerid\fP, which must be
56 a non\-NULL pointer. This ID is unique within the process, until the timer
57 is deleted. The new timer is initially disarmed.
59 The \fIclockid\fP argument specifies the clock that the new timer uses to
60 measure time. It can be specified as one of the following values:
63 A settable system\-wide real\-time clock.
66 .\" Note: the CLOCK_MONOTONIC_RAW clock added for clock_gettime()
67 .\" in 2.6.28 is not supported for POSIX timers -- mtk, Feb 2009
68 A nonsettable monotonically increasing clock that measures time from some
69 unspecified point in the past that does not change after system startup.
71 \fBCLOCK_PROCESS_CPUTIME_ID\fP (since Linux 2.6.12)
72 A clock that measures (user and system) CPU time consumed by (all of the
73 threads in) the calling process.
75 \fBCLOCK_THREAD_CPUTIME_ID\fP (since Linux 2.6.12)
76 .\" The CLOCK_MONOTONIC_RAW that was added in 2.6.28 can't be used
77 .\" to create a timer -- mtk, Feb 2009
78 A clock that measures (user and system) CPU time consumed by the calling
81 As well as the above values, \fIclockid\fP can be specified as the \fIclockid\fP
82 returned by a call to \fBclock_getcpuclockid\fP(3) or
83 \fBpthread_getcpuclockid\fP(3).
85 The \fIsevp\fP argument points to a \fIsigevent\fP structure that specifies how
86 the caller should be notified when the timer expires. For the definition
87 and general details of this structure, see \fBsigevent\fP(7).
89 The \fIsevp.sigev_notify\fP field can have the following values:
92 Don't asynchronously notify when the timer expires. Progress of the timer
93 can be monitored using \fBtimer_gettime\fP(2).
96 Upon timer expiration, generate the signal \fIsigev_signo\fP for the process.
97 See \fBsigevent\fP(7) for general details. The \fIsi_code\fP field of the
98 \fIsiginfo_t\fP structure will be set to \fBSI_TIMER\fP. At any point in time, at
99 most one signal is queued to the process for a given timer; see
100 \fBtimer_getoverrun\fP(2) for more details.
103 Upon timer expiration, invoke \fIsigev_notify_function\fP as if it were the
104 start function of a new thread. See \fBsigevent\fP(7) for details.
106 \fBSIGEV_THREAD_ID\fP (Linux\-specific)
107 As for \fBSIGEV_SIGNAL\fP, but the signal is targeted at the thread whose ID is
108 given in \fIsigev_notify_thread_id\fP, which must be a thread in the same
109 process as the caller. The \fIsigev_notify_thread_id\fP field specifies a
110 kernel thread ID, that is, the value returned by \fBclone\fP(2) or
111 \fBgettid\fP(2). This flag is only intended for use by threading libraries.
113 Specifying \fIsevp\fP as NULL is equivalent to specifying a pointer to a
114 \fIsigevent\fP structure in which \fIsigev_notify\fP is \fBSIGEV_SIGNAL\fP,
115 \fIsigev_signo\fP is \fBSIGALRM\fP, and \fIsigev_value.sival_int\fP is the timer ID.
117 On success, \fBtimer_create\fP() returns 0, and the ID of the new timer is
118 placed in \fI*timerid\fP. On failure, \-1 is returned, and \fIerrno\fP is set to
123 Temporary error during kernel allocation of timer structures.
126 Clock ID, \fIsigev_notify\fP, \fIsigev_signo\fP, or \fIsigev_notify_thread_id\fP is
130 .\" glibc layer: malloc()
131 Could not allocate memory.
133 This system call is available since Linux 2.6.
137 A program may create multiple interval timers using \fBtimer_create\fP().
139 Timers are not inherited by the child of a \fBfork\fP(2), and are disarmed and
140 deleted during an \fBexecve\fP(2).
142 The kernel preallocates a "queued real\-time signal" for each timer created
143 using \fBtimer_create\fP(). Consequently, the number of timers is limited by
144 the \fBRLIMIT_SIGPENDING\fP resource limit (see \fBsetrlimit\fP(2)).
146 The timers created by \fBtimer_create\fP() are commonly known as "POSIX
147 (interval) timers". The POSIX timers API consists of the following
150 \fBtimer_create\fP(): Create a timer.
152 \fBtimer_settime\fP(2): Arm (start) or disarm (stop) a timer.
154 \fBtimer_gettime\fP(2): Fetch the time remaining until the next expiration of a
155 timer, along with the interval setting of the timer.
157 \fBtimer_getoverrun\fP(2): Return the overrun count for the last timer
160 \fBtimer_delete\fP(2): Disarm and delete a timer.
162 Part of the implementation of the POSIX timers API is provided by glibc. In
165 The functionality for \fBSIGEV_THREAD\fP is implemented within glibc, rather
168 .\" See the glibc source file kernel-posix-timers.h for the structure
169 .\" that glibc uses to map userspace timer IDs to kernel timer IDs
170 .\" The kernel-level timer ID is exposed via siginfo.si_tid.
171 The timer IDs presented at user level are maintained by glibc, which maps
172 these IDs to the timer IDs employed by the kernel.
174 The POSIX timers system calls first appeared in Linux 2.6. Prior to this,
175 glibc provided an incomplete userspace implementation (\fBCLOCK_REALTIME\fP
176 timers only) using POSIX threads, and current glibc falls back to this
177 implementation on systems running pre\-2.6 Linux kernels.
179 The program below takes two arguments: a sleep period in seconds, and a
180 timer frequency in nanoseconds. The program establishes a handler for the
181 signal it uses for the timer, blocks that signal, creates and arms a timer
182 that expires with the given frequency, sleeps for the specified number of
183 seconds, and then unblocks the timer signal. Assuming that the timer
184 expired at least once while the program slept, the signal handler will be
185 invoked, and the handler displays some information about the timer
186 notification. The program terminates after one invocation of the signal
189 In the following example run, the program sleeps for 1 second, after
190 creating a timer that has a frequency of 100 nanoseconds. By the time the
191 signal is unblocked and delivered, there have been around ten million
196 $ \fB./a.out 1 100\fP
197 Establishing handler for signal 34
199 timer ID is 0x804c008
200 Sleeping for 1 seconds
203 sival_ptr = 0xbfb174f4; *sival_ptr = 0x804c008
204 overrun count = 10004886
216 #define CLOCKID CLOCK_REALTIME
219 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
223 print_siginfo(siginfo_t *si)
228 tidp = si\->si_value.sival_ptr;
230 printf(" sival_ptr = %p; ", si\->si_value.sival_ptr);
231 printf(" *sival_ptr = 0x%lx\en", (long) *tidp);
233 or = timer_getoverrun(*tidp);
235 errExit("timer_getoverrun");
237 printf(" overrun count = %d\en", or);
241 handler(int sig, siginfo_t *si, void *uc)
243 /* Note: calling printf() from a signal handler is not
244 strictly correct, since printf() is not async\-signal\-safe;
247 printf("Caught signal %d\en", sig);
249 signal(sig, SIG_IGN);
253 main(int argc, char *argv[])
257 struct itimerspec its;
258 long long freq_nanosecs;
263 fprintf(stderr, "Usage: %s <sleep\-secs> <freq\-nanosecs>\en",
268 /* Establish handler for timer signal */
270 printf("Establishing handler for signal %d\en", SIG);
271 sa.sa_flags = SA_SIGINFO;
272 sa.sa_sigaction = handler;
273 sigemptyset(&sa.sa_mask);
274 if (sigaction(SIG, &sa, NULL) == \-1)
275 errExit("sigaction");
277 /* Block timer signal temporarily */
279 printf("Blocking signal %d\en", SIG);
281 sigaddset(&mask, SIG);
282 if (sigprocmask(SIG_SETMASK, &mask, NULL) == \-1)
283 errExit("sigprocmask");
285 /* Create the timer */
287 sev.sigev_notify = SIGEV_SIGNAL;
288 sev.sigev_signo = SIG;
289 sev.sigev_value.sival_ptr = &timerid;
290 if (timer_create(CLOCKID, &sev, &timerid) == \-1)
291 errExit("timer_create");
293 printf("timer ID is 0x%lx\en", (long) timerid);
295 /* Start the timer */
297 freq_nanosecs = atoll(argv[2]);
298 its.it_value.tv_sec = freq_nanosecs / 1000000000;
299 its.it_value.tv_nsec = freq_nanosecs % 1000000000;
300 its.it_interval.tv_sec = its.it_value.tv_sec;
301 its.it_interval.tv_nsec = its.it_value.tv_nsec;
303 if (timer_settime(timerid, 0, &its, NULL) == \-1)
304 errExit("timer_settime");
306 /* Sleep for a while; meanwhile, the timer may expire
309 printf("Sleeping for %d seconds\en", atoi(argv[1]));
310 sleep(atoi(argv[1]));
312 /* Unlock the timer signal, so that timer notification
315 printf("Unblocking signal %d\en", SIG);
316 if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == \-1)
317 errExit("sigprocmask");
323 \fBclock_gettime\fP(2), \fBsetitimer\fP(2), \fBtimer_delete\fP(2),
324 \fBtimer_getoverrun\fP(2), \fBtimer_settime\fP(2), \fBtimerfd_create\fP(2),
325 \fBclock_getcpuclockid\fP(3), \fBpthread_getcpuclockid\fP(3), \fBpthreads\fP(7),
326 \fBsigevent\fP(7), \fBsignal\fP(7), \fBtime\fP(7)