]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/sem_wait.3
getauxval.3: wfix
[thirdparty/man-pages.git] / man3 / sem_wait.3
1 '\" t
2 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .TH SEM_WAIT 3 2015-08-08 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 sem_wait, sem_timedwait, sem_trywait \- lock a semaphore
29 .SH SYNOPSIS
30 .nf
31 .B #include <semaphore.h>
32 .sp
33 .BI "int sem_wait(sem_t *" sem );
34 .sp
35 .BI "int sem_trywait(sem_t *" sem );
36 .sp
37 .BI "int sem_timedwait(sem_t *" sem ", const struct timespec *" abs_timeout );
38 .fi
39 .sp
40 Link with \fI\-pthread\fP.
41 .sp
42 .in -4n
43 Feature Test Macro Requirements for glibc (see
44 .BR feature_test_macros (7)):
45 .in
46 .sp
47 .BR sem_timedwait ():
48 _POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600
49 .SH DESCRIPTION
50 .BR sem_wait ()
51 decrements (locks) the semaphore pointed to by
52 .IR sem .
53 If the semaphore's value is greater than zero,
54 then the decrement proceeds, and the function returns, immediately.
55 If the semaphore currently has the value zero,
56 then the call blocks until either it becomes possible to perform
57 the decrement (i.e., the semaphore value rises above zero),
58 or a signal handler interrupts the call.
59
60 .BR sem_trywait ()
61 is the same as
62 .BR sem_wait (),
63 except that if the decrement cannot be immediately performed,
64 then call returns an error
65 .RI ( errno
66 set to
67 .BR EAGAIN )
68 instead of blocking.
69
70 .BR sem_timedwait ()
71 is the same as
72 .BR sem_wait (),
73 except that
74 .I abs_timeout
75 specifies a limit on the amount of time that the call
76 should block if the decrement cannot be immediately performed.
77 The
78 .I abs_timeout
79 argument points to a structure that specifies an absolute timeout
80 in seconds and nanoseconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
81 This structure is defined as follows:
82
83 .nf
84 .in +4n
85 struct timespec {
86 time_t tv_sec; /* Seconds */
87 long tv_nsec; /* Nanoseconds [0 .. 999999999] */
88 };
89 .in
90 .fi
91 .PP
92 If the timeout has already expired by the time of the call,
93 and the semaphore could not be locked immediately,
94 then
95 .BR sem_timedwait ()
96 fails with a timeout error
97 .RI ( errno
98 set to
99 .BR ETIMEDOUT ).
100
101 If the operation can be performed immediately, then
102 .BR sem_timedwait ()
103 never fails with a timeout error, regardless of the value of
104 .IR abs_timeout .
105 Furthermore, the validity of
106 .I abs_timeout
107 is not checked in this case.
108 .SH RETURN VALUE
109 All of these functions return 0 on success;
110 on error, the value of the semaphore is left unchanged,
111 \-1 is returned, and
112 .I errno
113 is set to indicate the error.
114 .SH ERRORS
115 .TP
116 .B EINTR
117 The call was interrupted by a signal handler; see
118 .BR signal (7).
119 .TP
120 .B EINVAL
121 .I sem
122 is not a valid semaphore.
123 .PP
124 The following additional error can occur for
125 .BR sem_trywait ():
126 .TP
127 .B EAGAIN
128 The operation could not be performed without blocking (i.e., the
129 semaphore currently has the value zero).
130 .PP
131 The following additional errors can occur for
132 .BR sem_timedwait ():
133 .TP
134 .B EINVAL
135 The value of
136 .I abs_timeout.tv_nsecs
137 is less than 0, or greater than or equal to 1000 million.
138 .TP
139 .B ETIMEDOUT
140 The call timed out before the semaphore could be locked.
141 .\" POSIX.1-2001 also allows EDEADLK -- "A deadlock condition
142 .\" was detected", but this does not occur on Linux(?).
143 .SH ATTRIBUTES
144 For an explanation of the terms used in this section, see
145 .BR attributes (7).
146 .TS
147 allbox;
148 lbw26 lb lb
149 l l l.
150 Interface Attribute Value
151 T{
152 .BR sem_wait (),
153 .BR sem_trywait (),
154 .BR sem_timedwait ()
155 T} Thread safety MT-Safe
156 .TE
157 .SH CONFORMING TO
158 POSIX.1-2001, POSIX.1-2008.
159 .SH NOTES
160 A signal handler always interrupts a blocked call to
161 one of these functions, regardless of the use of the
162 .BR sigaction (2)
163 .B SA_RESTART
164 flag.
165 .\" sem_wait() is always interrupted on most other implementations,
166 .\" but on FreeBSD 5.4 SA_RESTART does cause restarting.
167 .SH EXAMPLE
168 .PP
169 The (somewhat trivial) program shown below operates on an
170 unnamed semaphore.
171 The program expects two command-line arguments.
172 The first argument specifies a seconds value that is used to
173 set an alarm timer to generate a
174 .B SIGALRM
175 signal.
176 This handler performs a
177 .BR sem_post (3)
178 to increment the semaphore that is being waited on in
179 .I main()
180 using
181 .BR sem_timedwait ().
182 The second command-line argument specifies the length
183 of the timeout, in seconds, for
184 .BR sem_timedwait ().
185 The following shows what happens on two different runs of the program:
186
187 .in +4n
188 .nf
189 .RB "$" " ./a.out 2 3"
190 About to call sem_timedwait()
191 sem_post() from handler
192 sem_timedwait() succeeded
193 .RB "$" " ./a.out 2 1"
194 About to call sem_timedwait()
195 sem_timedwait() timed out
196 .fi
197 .in
198 .SS Program source
199 \&
200 .nf
201 #include <unistd.h>
202 #include <stdio.h>
203 #include <stdlib.h>
204 #include <semaphore.h>
205 #include <time.h>
206 #include <assert.h>
207 #include <errno.h>
208 #include <signal.h>
209
210 sem_t sem;
211
212 #define handle_error(msg) \\
213 do { perror(msg); exit(EXIT_FAILURE); } while (0)
214
215 static void
216 handler(int sig)
217 {
218 write(STDOUT_FILENO, "sem_post() from handler\\n", 24);
219 if (sem_post(&sem) == \-1) {
220 write(STDERR_FILENO, "sem_post() failed\\n", 18);
221 _exit(EXIT_FAILURE);
222 }
223 }
224
225 int
226 main(int argc, char *argv[])
227 {
228 struct sigaction sa;
229 struct timespec ts;
230 int s;
231
232 if (argc != 3) {
233 fprintf(stderr, "Usage: %s <alarm\-secs> <wait\-secs>\\n",
234 argv[0]);
235 exit(EXIT_FAILURE);
236 }
237
238 if (sem_init(&sem, 0, 0) == \-1)
239 handle_error("sem_init");
240
241 /* Establish SIGALRM handler; set alarm timer using argv[1] */
242
243 sa.sa_handler = handler;
244 sigemptyset(&sa.sa_mask);
245 sa.sa_flags = 0;
246 if (sigaction(SIGALRM, &sa, NULL) == \-1)
247 handle_error("sigaction");
248
249 alarm(atoi(argv[1]));
250
251 /* Calculate relative interval as current time plus
252 number of seconds given argv[2] */
253
254 if (clock_gettime(CLOCK_REALTIME, &ts) == \-1)
255 handle_error("clock_gettime");
256
257 ts.tv_sec += atoi(argv[2]);
258
259 printf("main() about to call sem_timedwait()\\n");
260 while ((s = sem_timedwait(&sem, &ts)) == \-1 && errno == EINTR)
261 continue; /* Restart if interrupted by handler */
262
263 /* Check what happened */
264
265 if (s == \-1) {
266 if (errno == ETIMEDOUT)
267 printf("sem_timedwait() timed out\\n");
268 else
269 perror("sem_timedwait");
270 } else
271 printf("sem_timedwait() succeeded\\n");
272
273 exit((s == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
274 }
275 .fi
276 .SH SEE ALSO
277 .BR clock_gettime (2),
278 .BR sem_getvalue (3),
279 .BR sem_post (3),
280 .BR sem_overview (7),
281 .BR time (7)