1SEM_TRYWAIT(3P) POSIX Programmer's Manual SEM_TRYWAIT(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 sem_trywait, sem_wait — lock a semaphore
14
16 #include <semaphore.h>
17
18 int sem_trywait(sem_t *sem);
19 int sem_wait(sem_t *sem);
20
22 The sem_trywait() function shall lock the semaphore referenced by sem
23 only if the semaphore is currently not locked; that is, if the sema‐
24 phore value is currently positive. Otherwise, it shall not lock the
25 semaphore.
26
27 The sem_wait() function shall lock the semaphore referenced by sem by
28 performing a semaphore lock operation on that semaphore. If the sema‐
29 phore value is currently zero, then the calling thread shall not return
30 from the call to sem_wait() until it either locks the semaphore or the
31 call is interrupted by a signal.
32
33 Upon successful return, the state of the semaphore shall be locked and
34 shall remain locked until the sem_post() function is executed and
35 returns successfully.
36
37 The sem_wait() function is interruptible by the delivery of a signal.
38
40 The sem_trywait() and sem_wait() functions shall return zero if the
41 calling process successfully performed the semaphore lock operation on
42 the semaphore designated by sem. If the call was unsuccessful, the
43 state of the semaphore shall be unchanged, and the function shall
44 return a value of −1 and set errno to indicate the error.
45
47 The sem_trywait() function shall fail if:
48
49 EAGAIN The semaphore was already locked, so it cannot be immediately
50 locked by the sem_trywait() operation.
51
52 The sem_trywait() and sem_wait() functions may fail if:
53
54 EDEADLK
55 A deadlock condition was detected.
56
57 EINTR A signal interrupted this function.
58
59 EINVAL The sem argument does not refer to a valid semaphore.
60
61 The following sections are informative.
62
64 None.
65
67 Applications using these functions may be subject to priority inver‐
68 sion, as discussed in the Base Definitions volume of POSIX.1‐2008, Sec‐
69 tion 3.287, Priority Inversion.
70
72 None.
73
75 None.
76
78 semctl(), semget(), semop(), sem_post(), sem_timedwait()
79
80 The Base Definitions volume of POSIX.1‐2008, Section 3.287, Priority
81 Inversion, Section 4.11, Memory Synchronization, <semaphore.h>
82
84 Portions of this text are reprinted and reproduced in electronic form
85 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
86 -- Portable Operating System Interface (POSIX), The Open Group Base
87 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
88 cal and Electronics Engineers, Inc and The Open Group. (This is
89 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
90 event of any discrepancy between this version and the original IEEE and
91 The Open Group Standard, the original IEEE and The Open Group Standard
92 is the referee document. The original Standard can be obtained online
93 at http://www.unix.org/online.html .
94
95 Any typographical or formatting errors that appear in this page are
96 most likely to have been introduced during the conversion of the source
97 files to man page format. To report such errors, see https://www.ker‐
98 nel.org/doc/man-pages/reporting_bugs.html .
99
100
101
102IEEE/The Open Group 2013 SEM_TRYWAIT(3P)