OSDN Git Service

b38b5730cebc3d8f3e3e626a62a2bde93133b467
[linuxjm/LDP_man-pages.git] / original / man2 / timer_create.2
1 .\" Copyright (c) 2009 Linux Foundation, written by Michael Kerrisk
2 .\"     <mtk.manpages@gmail.com>
3 .\"
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.
7 .\"
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.
12 .\"
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
19 .\" professionally.
20 .\"
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 2010-09-27 Linux "Linux Programmer's Manual"
24 .SH NAME
25 timer_create \- create a POSIX per-process timer
26 .SH SYNOPSIS
27 .nf
28 .B  #include <signal.h>
29 .B  #include <time.h>
30
31 .BI "int timer_create(clockid_t " clockid ", struct sigevent *" sevp ,
32 .BI "                 timer_t *" timerid );
33 .fi
34
35 Link with \fI\-lrt\fP.
36 .sp
37 .in -4n
38 Feature Test Macro Requirements for glibc (see
39 .BR feature_test_macros (7)):
40 .in
41 .sp
42 .BR timer_create ():
43 _POSIX_C_SOURCE\ >=\ 199309L
44 .SH DESCRIPTION
45 .BR timer_create ()
46 creates a new per-process interval timer.
47 The ID of the new timer is returned in the buffer pointed to by
48 .IR timerid ,
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.
52
53 The
54 .I clockid
55 argument specifies the clock that the new timer uses to measure time.
56 It can be specified as one of the following values:
57 .TP
58 .B CLOCK_REALTIME
59 A settable system-wide real-time clock.
60 .TP
61 .B CLOCK_MONOTONIC
62 A nonsettable monotonically increasing clock that measures time
63 from some unspecified point in the past that does not change
64 after system startup.
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
67 .TP
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.
71 .TP
72 .BR CLOCK_THREAD_CPUTIME_ID " (since Linux 2.6.12)"
73 A clock that measures (user and system) CPU time consumed by
74 the calling thread.
75 .\" The CLOCK_MONOTONIC_RAW that was added in 2.6.28 can't be used
76 .\" to create a timer -- mtk, Feb 2009
77 .PP
78 As well as the above values,
79 .I clockid
80 can be specified as the
81 .I clockid
82 returned by a call to
83 .BR clock_getcpuclockid (3)
84 or
85 .BR pthread_getcpuclockid (3).
86
87 The
88 .I sevp
89 argument points to a
90 .I sigevent
91 structure that specifies how the caller
92 should be notified when the timer expires.
93 For the definition and general details of this structure, see
94 .BR sigevent (7).
95
96 The
97 .I sevp.sigev_notify
98 field can have the following values:
99 .TP
100 .BR SIGEV_NONE
101 Don't asynchronously notify when the timer expires.
102 Progress of the timer can be monitored using
103 .BR timer_gettime (2).
104 .TP
105 .BR SIGEV_SIGNAL
106 Upon timer expiration, generate the signal
107 .I sigev_signo
108 for the process.
109 See
110 .BR sigevent (7)
111 for general details.
112 The
113 .I si_code
114 field of the
115 .I siginfo_t
116 structure will be set to
117 .BR SI_TIMER .
118 At any point in time,
119 at most one signal is queued to the process for a given timer; see
120 .BR timer_getoverrun (2)
121 for more details.
122 .TP
123 .BR SIGEV_THREAD
124 Upon timer expiration, invoke
125 .I sigev_notify_function
126 as if it were the start function of a new thread.
127 See
128 .BR sigevent (7)
129 for details.
130 .TP
131 .BR SIGEV_THREAD_ID " (Linux-specific)"
132 As for
133 .BR SIGEV_SIGNAL ,
134 but the signal is targeted at the thread whose ID is given in
135 .IR sigev_notify_thread_id ,
136 which must be a thread in the same process as the caller.
137 The
138 .IR sigev_notify_thread_id
139 field specifies a kernel thread ID, that is, the value returned by
140 .BR clone (2)
141 or
142 .BR gettid (2).
143 This flag is only intended for use by threading libraries.
144 .PP
145 Specifying
146 .I sevp
147 as NULL is equivalent to specifying a pointer to a
148 .I sigevent
149 structure in which
150 .I sigev_notify
151 is
152 .BR SIGEV_SIGNAL ,
153 .I sigev_signo
154 is
155 .BR SIGALRM ,
156 and
157 .I sigev_value.sival_int
158 is the timer ID.
159 .SH RETURN VALUE
160 On success,
161 .BR timer_create ()
162 returns 0, and the ID of the new timer is placed in
163 .IR *timerid .
164 On failure, \-1 is returned, and
165 .I errno
166 is set to indicate the error.
167 .SH ERRORS
168 .TP
169 .B EAGAIN
170 Temporary error during kernel allocation of timer structures.
171 .TP
172 .B EINVAL
173 Clock ID,
174 .IR sigev_notify ,
175 .IR sigev_signo ,
176 or
177 .IR sigev_notify_thread_id
178 is invalid.
179 .TP
180 .B ENOMEM
181 .\" glibc layer: malloc()
182 Could not allocate memory.
183 .SH VERSIONS
184 This system call is available since Linux 2.6.
185 .SH CONFORMING TO
186 POSIX.1-2001.
187 .SH NOTES
188 A program may create multiple interval timers using
189 .BR timer_create ().
190
191 Timers are not inherited by the child of a
192 .BR fork (2),
193 and are disarmed and deleted during an
194 .BR execve (2).
195
196 The kernel preallocates a "queued real-time signal"
197 for each timer created using
198 .BR timer_create ().
199 Consequently, the number of timers is limited by the
200 .BR RLIMIT_SIGPENDING
201 resource limit (see
202 .BR setrlimit (2)).
203
204 The timers created by
205 .BR timer_create ()
206 are commonly known as "POSIX (interval) timers".
207 The POSIX timers API consists of the following interfaces:
208 .IP * 3
209 .BR timer_create ():
210 Create a timer.
211 .IP *
212 .BR timer_settime (2):
213 Arm (start) or disarm (stop) a timer.
214 .IP *
215 .BR timer_gettime (2):
216 Fetch the time remaining until the next expiration of a timer,
217 along with the interval setting of the timer.
218 .IP *
219 .BR timer_getoverrun (2):
220 Return the overrun count for the last timer expiration.
221 .IP *
222 .BR timer_delete (2):
223 Disarm and delete a timer.
224 .PP
225 Part of the implementation of the POSIX timers API is provided by glibc.
226 In particular:
227 .IP * 3
228 The functionality for
229 .BR SIGEV_THREAD
230 is implemented within glibc, rather than the kernel.
231 .IP *
232 The timer IDs presented at user level are maintained by glibc,
233 which maps these IDs to the timer IDs employed by the kernel.
234 .\" See the glibc source file kernel-posix-timers.h for the structure
235 .\" that glibc uses to map userspace timer IDs to kernel timer IDs
236 .\" The kernel-level timer ID is exposed via siginfo.si_tid.
237 .PP
238 The POSIX timers system calls first appeared in Linux 2.6.
239 Prior to this,
240 glibc provided an incomplete userspace implementation
241 .RB ( CLOCK_REALTIME
242 timers only) using POSIX threads,
243 and current glibc falls back to this implementation on systems
244 running pre-2.6 Linux kernels.
245 .SH EXAMPLE
246 The program below takes two arguments: a sleep period in seconds,
247 and a timer frequency in nanoseconds.
248 The program establishes a handler for the signal it uses for the timer,
249 blocks that signal,
250 creates and arms a timer that expires with the given frequency,
251 sleeps for the specified number of seconds,
252 and then unblocks the timer signal.
253 Assuming that the timer expired at least once while the program slept,
254 the signal handler will be invoked,
255 and the handler displays some information about the timer notification.
256 The program terminates after one invocation of the signal handler.
257
258 In the following example run, the program sleeps for 1 second,
259 after creating a timer that has a frequency of 100 nanoseconds.
260 By the time the signal is unblocked and delivered,
261 there have been around ten million overruns.
262 .in +4n
263 .nf
264
265 $ \fB./a.out 1 10\fP
266 Establishing handler for signal 34
267 Blocking signal 34
268 timer ID is 0x804c008
269 Sleeping for 1 seconds
270 Unblocking signal 34
271 Caught signal 34
272     sival_ptr = 0xbfb174f4;     *sival_ptr = 0x804c008
273     overrun count = 10004886
274 .fi
275 .in
276 .SS Program Source
277 \&
278 .nf
279 #include <stdlib.h>
280 #include <unistd.h>
281 #include <stdio.h>
282 #include <signal.h>
283 #include <time.h>
284
285 #define CLOCKID CLOCK_REALTIME
286 #define SIG SIGRTMIN
287
288 #define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \\
289                         } while (0)
290
291 static void
292 print_siginfo(siginfo_t *si)
293 {
294     timer_t *tidp;
295     int or;
296
297     tidp = si\->si_value.sival_ptr;
298
299     printf("    sival_ptr = %p; ", si\->si_value.sival_ptr);
300     printf("    *sival_ptr = 0x%lx\\n", (long) *tidp);
301
302     or = timer_getoverrun(*tidp);
303     if (or == \-1)
304         errExit("timer_getoverrun");
305     else
306         printf("    overrun count = %d\\n", or);
307 }
308
309 static void
310 handler(int sig, siginfo_t *si, void *uc)
311 {
312     /* Note: calling printf() from a signal handler is not
313        strictly correct, since printf() is not async\-signal\-safe;
314        see signal(7) */
315
316     printf("Caught signal %d\\n", sig);
317     print_siginfo(si);
318     signal(sig, SIG_IGN);
319 }
320
321 int
322 main(int argc, char *argv[])
323 {
324     timer_t timerid;
325     struct sigevent sev;
326     struct itimerspec its;
327     long long freq_nanosecs;
328     sigset_t mask;
329     struct sigaction sa;
330
331     if (argc != 3) {
332         fprintf(stderr, "Usage: %s <sleep\-secs> <freq\-nanosecs>\\n",
333                 argv[0]);
334         exit(EXIT_FAILURE);
335     }
336
337     /* Establish handler for timer signal */
338
339     printf("Establishing handler for signal %d\\n", SIG);
340     sa.sa_flags = SA_SIGINFO;
341     sa.sa_sigaction = handler;
342     sigemptyset(&sa.sa_mask);
343     if (sigaction(SIG, &sa, NULL) == \-1)
344         errExit("sigaction");
345
346     /* Block timer signal temporarily */
347
348     printf("Blocking signal %d\\n", SIG);
349     sigemptyset(&mask);
350     sigaddset(&mask, SIG);
351     if (sigprocmask(SIG_SETMASK, &mask, NULL) == \-1)
352         errExit("sigprocmask");
353
354     /* Create the timer */
355
356     sev.sigev_notify = SIGEV_SIGNAL;
357     sev.sigev_signo = SIG;
358     sev.sigev_value.sival_ptr = &timerid;
359     if (timer_create(CLOCKID, &sev, &timerid) == \-1)
360         errExit("timer_create");
361
362     printf("timer ID is 0x%lx\\n", (long) timerid);
363
364     /* Start the timer */
365
366     freq_nanosecs = atoll(argv[2]);
367     its.it_value.tv_sec = freq_nanosecs / 1000000000;
368     its.it_value.tv_nsec = freq_nanosecs % 1000000000;
369     its.it_interval.tv_sec = its.it_value.tv_sec;
370     its.it_interval.tv_nsec = its.it_value.tv_nsec;
371
372     if (timer_settime(timerid, 0, &its, NULL) == \-1)
373          errExit("timer_settime");
374
375     /* Sleep for a while; meanwhile, the timer may expire
376        multiple times */
377
378     printf("Sleeping for %d seconds\\n", atoi(argv[1]));
379     sleep(atoi(argv[1]));
380
381     /* Unlock the timer signal, so that timer notification
382        can be delivered */
383
384     printf("Unblocking signal %d\\n", SIG);
385     if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == \-1)
386         errExit("sigprocmask");
387
388     exit(EXIT_SUCCESS);
389 }
390 .fi
391 .SH SEE ALSO
392 .BR clock_gettime (2),
393 .BR setitimer (2),
394 .BR timer_delete (2),
395 .BR timer_getoverrun (2),
396 .BR timer_settime (2),
397 .BR timerfd_create (2),
398 .BR clock_getcpuclockid (3),
399 .BR pthread_getcpuclockid (3),
400 .BR pthreads (7),
401 .BR sigevent (7),
402 .BR signal (7),
403 .BR time (7)