1TIMER_CREATE(3P) POSIX Programmer's Manual TIMER_CREATE(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 timer_create — create a per-process timer
14
16 #include <signal.h>
17 #include <time.h>
18
19 int timer_create(clockid_t clockid, struct sigevent *restrict evp,
20 timer_t *restrict timerid);
21
23 The timer_create() function shall create a per-process timer using the
24 specified clock, clock_id, as the timing base. The timer_create() func‐
25 tion shall return, in the location referenced by timerid, a timer ID of
26 type timer_t used to identify the timer in timer requests. This timer
27 ID shall be unique within the calling process until the timer is
28 deleted. The particular clock, clock_id, is defined in <time.h>. The
29 timer whose ID is returned shall be in a disarmed state upon return
30 from timer_create().
31
32 The evp argument, if non-NULL, points to a sigevent structure. This
33 structure, allocated by the application, defines the asynchronous noti‐
34 fication to occur as specified in Section 2.4.1, Signal Generation and
35 Delivery when the timer expires. If the evp argument is NULL, the
36 effect is as if the evp argument pointed to a sigevent structure with
37 the sigev_notify member having the value SIGEV_SIGNAL, the sigev_signo
38 having a default signal number, and the sigev_value member having the
39 value of the timer ID.
40
41 Each implementation shall define a set of clocks that can be used as
42 timing bases for per-process timers. All implementations shall support
43 a clock_id of CLOCK_REALTIME. If the Monotonic Clock option is sup‐
44 ported, implementations shall support a clock_id of CLOCK_MONOTONIC.
45
46 Per-process timers shall not be inherited by a child process across a
47 fork() and shall be disarmed and deleted by an exec.
48
49 If _POSIX_CPUTIME is defined, implementations shall support clock_id
50 values representing the CPU-time clock of the calling process.
51
52 If _POSIX_THREAD_CPUTIME is defined, implementations shall support
53 clock_id values representing the CPU-time clock of the calling thread.
54
55 It is implementation-defined whether a timer_create() function will
56 succeed if the value defined by clock_id corresponds to the CPU-time
57 clock of a process or thread different from the process or thread
58 invoking the function.
59
60 If evp->sigev_sigev_notify is SIGEV_THREAD and
61 sev->sigev_notify_attributes is not NULL, if the attribute pointed to
62 by sev->sigev_notify_attributes has a thread stack address specified by
63 a call to pthread_attr_setstack(), the results are unspecified if the
64 signal is generated more than once.
65
67 If the call succeeds, timer_create() shall return zero and update the
68 location referenced by timerid to a timer_t, which can be passed to the
69 per-process timer calls. If an error occurs, the function shall return
70 a value of −1 and set errno to indicate the error. The value of timerid
71 is undefined if an error occurs.
72
74 The timer_create() function shall fail if:
75
76 EAGAIN The system lacks sufficient signal queuing resources to honor
77 the request.
78
79 EAGAIN The calling process has already created all of the timers it is
80 allowed by this implementation.
81
82 EINVAL The specified clock ID is not defined.
83
84 ENOTSUP
85 The implementation does not support the creation of a timer
86 attached to the CPU-time clock that is specified by clock_id and
87 associated with a process or thread different from the process
88 or thread invoking timer_create().
89
90 The following sections are informative.
91
93 None.
94
96 If a timer is created which has evp->sigev_sigev_notify set to
97 SIGEV_THREAD and the attribute pointed to by
98 evp->sigev_notify_attributes has a thread stack address specified by a
99 call to pthread_attr_setstack(), the memory dedicated as a thread stack
100 cannot be recovered. The reason for this is that the threads created in
101 response to a timer expiration are created detached, or in an unspeci‐
102 fied way if the thread attribute's detachstate is PTHREAD_CREATE_JOIN‐
103 ABLE. In neither case is it valid to call pthread_join(), which makes
104 it impossible to determine the lifetime of the created thread which
105 thus means the stack memory cannot be reused.
106
108 Periodic Timer Overrun and Resource Allocation
109 The specified timer facilities may deliver realtime signals (that is,
110 queued signals) on implementations that support this option. Since
111 realtime applications cannot afford to lose notifications of asynchro‐
112 nous events, like timer expirations or asynchronous I/O completions, it
113 must be possible to ensure that sufficient resources exist to deliver
114 the signal when the event occurs. In general, this is not a difficulty
115 because there is a one-to-one correspondence between a request and a
116 subsequent signal generation. If the request cannot allocate the signal
117 delivery resources, it can fail the call with an [EAGAIN] error.
118
119 Periodic timers are a special case. A single request can generate an
120 unspecified number of signals. This is not a problem if the requesting
121 process can service the signals as fast as they are generated, thus
122 making the signal delivery resources available for delivery of subse‐
123 quent periodic timer expiration signals. But, in general, this cannot
124 be assured—processing of periodic timer signals may ``overrun''; that
125 is, subsequent periodic timer expirations may occur before the cur‐
126 rently pending signal has been delivered.
127
128 Also, for signals, according to the POSIX.1‐1990 standard, if subse‐
129 quent occurrences of a pending signal are generated, it is implementa‐
130 tion-defined whether a signal is delivered for each occurrence. This is
131 not adequate for some realtime applications. So a mechanism is required
132 to allow applications to detect how many timer expirations were delayed
133 without requiring an indefinite amount of system resources to store the
134 delayed expirations.
135
136 The specified facilities provide for an overrun count. The overrun
137 count is defined as the number of extra timer expirations that occurred
138 between the time a timer expiration signal is generated and the time
139 the signal is delivered. The signal-catching function, if it is con‐
140 cerned with overruns, can retrieve this count on entry. With this
141 method, a periodic timer only needs one ``signal queuing resource''
142 that can be allocated at the time of the timer_create() function call.
143
144 A function is defined to retrieve the overrun count so that an applica‐
145 tion need not allocate static storage to contain the count, and an
146 implementation need not update this storage asynchronously on timer
147 expirations. But, for some high-frequency periodic applications, the
148 overhead of an additional system call on each timer expiration may be
149 prohibitive. The functions, as defined, permit an implementation to
150 maintain the overrun count in user space, associated with the timerid.
151 The timer_getoverrun() function can then be implemented as a macro that
152 uses the timerid argument (which may just be a pointer to a user space
153 structure containing the counter) to locate the overrun count with no
154 system call overhead. Other implementations, less concerned with this
155 class of applications, can avoid the asynchronous update of user space
156 by maintaining the count in a system structure at the cost of the extra
157 system call to obtain it.
158
159 Timer Expiration Signal Parameters
160 The Realtime Signals Extension option supports an application-specific
161 datum that is delivered to the extended signal handler. This value is
162 explicitly specified by the application, along with the signal number
163 to be delivered, in a sigevent structure. The type of the application-
164 defined value can be either an integer constant or a pointer. This
165 explicit specification of the value, as opposed to always sending the
166 timer ID, was selected based on existing practice.
167
168 It is common practice for realtime applications (on non-POSIX systems
169 or realtime extended POSIX systems) to use the parameters of event han‐
170 dlers as the case label of a switch statement or as a pointer to an
171 application-defined data structure. Since timer_ids are dynamically
172 allocated by the timer_create() function, they can be used for neither
173 of these functions without additional application overhead in the sig‐
174 nal handler; for example, to search an array of saved timer IDs to as‐
175 sociate the ID with a constant or application data structure.
176
178 None.
179
181 clock_getres(), timer_delete(), timer_getoverrun()
182
183 The Base Definitions volume of POSIX.1‐2008, <signal.h>, <time.h>
184
186 Portions of this text are reprinted and reproduced in electronic form
187 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
188 -- Portable Operating System Interface (POSIX), The Open Group Base
189 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
190 cal and Electronics Engineers, Inc and The Open Group. (This is
191 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
192 event of any discrepancy between this version and the original IEEE and
193 The Open Group Standard, the original IEEE and The Open Group Standard
194 is the referee document. The original Standard can be obtained online
195 at http://www.unix.org/online.html .
196
197 Any typographical or formatting errors that appear in this page are
198 most likely to have been introduced during the conversion of the source
199 files to man page format. To report such errors, see https://www.ker‐
200 nel.org/doc/man-pages/reporting_bugs.html .
201
202
203
204IEEE/The Open Group 2013 TIMER_CREATE(3P)