1TIMER_CREATE(2) Linux Programmer's Manual TIMER_CREATE(2)
2
3
4
6 timer_create - create a POSIX per-process timer
7
9 #include <signal.h>
10 #include <time.h>
11
12 int timer_create(clockid_t clockid, struct sigevent *sevp,
13 timer_t *timerid);
14
15 Link with -lrt.
16
17 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19 timer_create(): _POSIX_C_SOURCE >= 199309L
20
22 timer_create() creates a new per-process interval timer. The ID of the
23 new timer is returned in the buffer pointed to by timerid, which must
24 be a non-null pointer. This ID is unique within the process, until the
25 timer is deleted. The new timer is initially disarmed.
26
27 The clockid argument specifies the clock that the new timer uses to
28 measure time. It can be specified as one of the following values:
29
30 CLOCK_REALTIME
31 A settable system-wide real-time clock.
32
33 CLOCK_MONOTONIC
34 A nonsettable monotonically increasing clock that measures time
35 from some unspecified point in the past that does not change
36 after system startup.
37
38 CLOCK_PROCESS_CPUTIME_ID (since Linux 2.6.12)
39 A clock that measures (user and system) CPU time consumed by
40 (all of the threads in) the calling process.
41
42 CLOCK_THREAD_CPUTIME_ID (since Linux 2.6.12)
43 A clock that measures (user and system) CPU time consumed by the
44 calling thread.
45
46 CLOCK_BOOTTIME (Since Linux 2.6.39)
47 Like CLOCK_MONOTONIC, this is a monotonically increasing clock.
48 However, whereas the CLOCK_MONOTONIC clock does not measure the
49 time while a system is suspended, the CLOCK_BOOTTIME clock does
50 include the time during which the system is suspended. This is
51 useful for applications that need to be suspend-aware.
52 CLOCK_REALTIME is not suitable for such applications, since that
53 clock is affected by discontinuous changes to the system clock.
54
55 CLOCK_REALTIME_ALARM (since Linux 3.0)
56 This clock is like CLOCK_REALTIME, but will wake the system if
57 it is suspended. The caller must have the CAP_WAKE_ALARM capa‐
58 bility in order to set a timer against this clock.
59
60 CLOCK_BOOTTIME_ALARM (since Linux 3.0)
61 This clock is like CLOCK_BOOTTIME, but will wake the system if
62 it is suspended. The caller must have the CAP_WAKE_ALARM capa‐
63 bility in order to set a timer against this clock.
64
65 CLOCK_TAI (since Linux 3.10)
66 A system-wide clock derived from wall-clock time but ignoring
67 leap seconds.
68
69 See clock_getres(2) for some further details on the above clocks.
70
71 As well as the above values, clockid can be specified as the clockid
72 returned by a call to clock_getcpuclockid(3) or pthread_getcpu‐
73 clockid(3).
74
75 The sevp argument points to a sigevent structure that specifies how the
76 caller should be notified when the timer expires. For the definition
77 and general details of this structure, see sigevent(7).
78
79 The sevp.sigev_notify field can have the following values:
80
81 SIGEV_NONE
82 Don't asynchronously notify when the timer expires. Progress of
83 the timer can be monitored using timer_gettime(2).
84
85 SIGEV_SIGNAL
86 Upon timer expiration, generate the signal sigev_signo for the
87 process. See sigevent(7) for general details. The si_code
88 field of the siginfo_t structure will be set to SI_TIMER. At
89 any point in time, at most one signal is queued to the process
90 for a given timer; see timer_getoverrun(2) for more details.
91
92 SIGEV_THREAD
93 Upon timer expiration, invoke sigev_notify_function as if it
94 were the start function of a new thread. See sigevent(7) for
95 details.
96
97 SIGEV_THREAD_ID (Linux-specific)
98 As for SIGEV_SIGNAL, but the signal is targeted at the thread
99 whose ID is given in sigev_notify_thread_id, which must be a
100 thread in the same process as the caller. The
101 sigev_notify_thread_id field specifies a kernel thread ID, that
102 is, the value returned by clone(2) or gettid(2). This flag is
103 intended only for use by threading libraries.
104
105 Specifying sevp as NULL is equivalent to specifying a pointer to a
106 sigevent structure in which sigev_notify is SIGEV_SIGNAL, sigev_signo
107 is SIGALRM, and sigev_value.sival_int is the timer ID.
108
110 On success, timer_create() returns 0, and the ID of the new timer is
111 placed in *timerid. On failure, -1 is returned, and errno is set to
112 indicate the error.
113
115 EAGAIN Temporary error during kernel allocation of timer structures.
116
117 EINVAL Clock ID, sigev_notify, sigev_signo, or sigev_notify_thread_id
118 is invalid.
119
120 ENOMEM Could not allocate memory.
121
122 ENOTSUP
123 The kernel does not support creating a timer against this
124 clockid.
125
126 EPERM clockid was CLOCK_REALTIME_ALARM or ,BR CLOCK_BOOTTIME_ALARM but
127 the caller did not have the CAP_WAKE_ALARM capability.
128
130 This system call is available since Linux 2.6.
131
133 POSIX.1-2001, POSIX.1-2008.
134
136 A program may create multiple interval timers using timer_create().
137
138 Timers are not inherited by the child of a fork(2), and are disarmed
139 and deleted during an execve(2).
140
141 The kernel preallocates a "queued real-time signal" for each timer cre‐
142 ated using timer_create(). Consequently, the number of timers is lim‐
143 ited by the RLIMIT_SIGPENDING resource limit (see setrlimit(2)).
144
145 The timers created by timer_create() are commonly known as "POSIX
146 (interval) timers". The POSIX timers API consists of the following
147 interfaces:
148
149 * timer_create(): Create a timer.
150
151 * timer_settime(2): Arm (start) or disarm (stop) a timer.
152
153 * timer_gettime(2): Fetch the time remaining until the next expiration
154 of a timer, along with the interval setting of the timer.
155
156 * timer_getoverrun(2): Return the overrun count for the last timer
157 expiration.
158
159 * timer_delete(2): Disarm and delete a timer.
160
161 Since Linux 3.10, the /proc/[pid]/timers file can be used to list the
162 POSIX timers for the process with PID pid. See proc(5) for further
163 information.
164
165 Since Linux 4.10, support for POSIX timers is a configurable option
166 that is enabled by default. Kernel support can be disabled via the
167 CONFIG_POSIX_TIMERS option.
168
169 C library/kernel differences
170 Part of the implementation of the POSIX timers API is provided by
171 glibc. In particular:
172
173 * Much of the functionality for SIGEV_THREAD is implemented within
174 glibc, rather than the kernel. (This is necessarily so, since the
175 thread involved in handling the notification is one that must be
176 managed by the C library POSIX threads implementation.) Although
177 the notification delivered to the process is via a thread, inter‐
178 nally the NPTL implementation uses a sigev_notify value of
179 SIGEV_THREAD_ID along with a real-time signal that is reserved by
180 the implementation (see nptl(7)).
181
182 * The implementation of the default case where evp is NULL is handled
183 inside glibc, which invokes the underlying system call with a suit‐
184 ably populated sigevent structure.
185
186 * The timer IDs presented at user level are maintained by glibc, which
187 maps these IDs to the timer IDs employed by the kernel.
188
189 The POSIX timers system calls first appeared in Linux 2.6. Prior to
190 this, glibc provided an incomplete user-space implementation
191 (CLOCK_REALTIME timers only) using POSIX threads, and in glibc versions
192 before 2.17, the implementation falls back to this technique on systems
193 running pre-2.6 Linux kernels.
194
196 The program below takes two arguments: a sleep period in seconds, and a
197 timer frequency in nanoseconds. The program establishes a handler for
198 the signal it uses for the timer, blocks that signal, creates and arms
199 a timer that expires with the given frequency, sleeps for the specified
200 number of seconds, and then unblocks the timer signal. Assuming that
201 the timer expired at least once while the program slept, the signal
202 handler will be invoked, and the handler displays some information
203 about the timer notification. The program terminates after one invoca‐
204 tion of the signal handler.
205
206 In the following example run, the program sleeps for 1 second, after
207 creating a timer that has a frequency of 100 nanoseconds. By the time
208 the signal is unblocked and delivered, there have been around ten mil‐
209 lion overruns.
210
211 $ ./a.out 1 100
212 Establishing handler for signal 34
213 Blocking signal 34
214 timer ID is 0x804c008
215 Sleeping for 1 seconds
216 Unblocking signal 34
217 Caught signal 34
218 sival_ptr = 0xbfb174f4; *sival_ptr = 0x804c008
219 overrun count = 10004886
220
221 Program source
222
223 #include <stdlib.h>
224 #include <unistd.h>
225 #include <stdio.h>
226 #include <signal.h>
227 #include <time.h>
228
229 #define CLOCKID CLOCK_REALTIME
230 #define SIG SIGRTMIN
231
232 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
233 } while (0)
234
235 static void
236 print_siginfo(siginfo_t *si)
237 {
238 timer_t *tidp;
239 int or;
240
241 tidp = si->si_value.sival_ptr;
242
243 printf(" sival_ptr = %p; ", si->si_value.sival_ptr);
244 printf(" *sival_ptr = 0x%lx\n", (long) *tidp);
245
246 or = timer_getoverrun(*tidp);
247 if (or == -1)
248 errExit("timer_getoverrun");
249 else
250 printf(" overrun count = %d\n", or);
251 }
252
253 static void
254 handler(int sig, siginfo_t *si, void *uc)
255 {
256 /* Note: calling printf() from a signal handler is not safe
257 (and should not be done in production programs), since
258 printf() is not async-signal-safe; see signal-safety(7).
259 Nevertheless, we use printf() here as a simple way of
260 showing that the handler was called. */
261
262 printf("Caught signal %d\n", sig);
263 print_siginfo(si);
264 signal(sig, SIG_IGN);
265 }
266
267 int
268 main(int argc, char *argv[])
269 {
270 timer_t timerid;
271 struct sigevent sev;
272 struct itimerspec its;
273 long long freq_nanosecs;
274 sigset_t mask;
275 struct sigaction sa;
276
277 if (argc != 3) {
278 fprintf(stderr, "Usage: %s <sleep-secs> <freq-nanosecs>\n",
279 argv[0]);
280 exit(EXIT_FAILURE);
281 }
282
283 /* Establish handler for timer signal */
284
285 printf("Establishing handler for signal %d\n", SIG);
286 sa.sa_flags = SA_SIGINFO;
287 sa.sa_sigaction = handler;
288 sigemptyset(&sa.sa_mask);
289 if (sigaction(SIG, &sa, NULL) == -1)
290 errExit("sigaction");
291
292 /* Block timer signal temporarily */
293
294 printf("Blocking signal %d\n", SIG);
295 sigemptyset(&mask);
296 sigaddset(&mask, SIG);
297 if (sigprocmask(SIG_SETMASK, &mask, NULL) == -1)
298 errExit("sigprocmask");
299
300 /* Create the timer */
301
302 sev.sigev_notify = SIGEV_SIGNAL;
303 sev.sigev_signo = SIG;
304 sev.sigev_value.sival_ptr = &timerid;
305 if (timer_create(CLOCKID, &sev, &timerid) == -1)
306 errExit("timer_create");
307
308 printf("timer ID is 0x%lx\n", (long) timerid);
309
310 /* Start the timer */
311
312 freq_nanosecs = atoll(argv[2]);
313 its.it_value.tv_sec = freq_nanosecs / 1000000000;
314 its.it_value.tv_nsec = freq_nanosecs % 1000000000;
315 its.it_interval.tv_sec = its.it_value.tv_sec;
316 its.it_interval.tv_nsec = its.it_value.tv_nsec;
317
318 if (timer_settime(timerid, 0, &its, NULL) == -1)
319 errExit("timer_settime");
320
321 /* Sleep for a while; meanwhile, the timer may expire
322 multiple times */
323
324 printf("Sleeping for %d seconds\n", atoi(argv[1]));
325 sleep(atoi(argv[1]));
326
327 /* Unlock the timer signal, so that timer notification
328 can be delivered */
329
330 printf("Unblocking signal %d\n", SIG);
331 if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1)
332 errExit("sigprocmask");
333
334 exit(EXIT_SUCCESS);
335 }
336
338 clock_gettime(2), setitimer(2), timer_delete(2), timer_getoverrun(2),
339 timer_settime(2), timerfd_create(2), clock_getcpuclockid(3),
340 pthread_getcpuclockid(3), pthreads(7), sigevent(7), signal(7), time(7)
341
343 This page is part of release 5.07 of the Linux man-pages project. A
344 description of the project, information about reporting bugs, and the
345 latest version of this page, can be found at
346 https://www.kernel.org/doc/man-pages/.
347
348
349
350Linux 2020-04-11 TIMER_CREATE(2)