1.\" Copyright (C) 2000 Jason Evans <jasone@canonware.com>. 2.\" All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice(s), this list of conditions and the following disclaimer as 9.\" the first lines of this file unmodified other than the possible 10.\" addition of one or more copyright notices. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice(s), this list of conditions and the following disclaimer in 13.\" the documentation and/or other materials provided with the 14.\" distribution. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY 17.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE 20.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 23.\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 25.\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 26.\" EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27.\" 28.\" $FreeBSD$ 29.Dd February 15, 2000 30.Dt SEM_WAIT 3 31.Os 32.Sh NAME 33.Nm sem_wait , 34.Nm sem_trywait 35.Nd decrement (lock) a semaphore 36.Sh SYNOPSIS 37.Fd #include <semaphore.h> 38.Ft int 39.Fn sem_wait "sem_t *sem" 40.Ft int 41.Fn sem_trywait "sem_t *sem" 42.Sh DESCRIPTION 43The 44.Fn sem_wait 45function decrements (locks) the semaphore pointed to by 46.Fa sem , 47but blocks if the value of 48.Fa sem 49is zero, until the value is non-zero and the value can be decremented. 50.Pp 51The 52.Fn sem_trywait 53function decrements (locks) the semaphore pointed to by 54.Fa sem 55only if the value is non-zero. Otherwise, the semaphore is not decremented and 56an error is returned. 57.Sh RETURN VALUES 58If successful, 59.Fn sem_wait 60and 61.Fn sem_trywait 62return 0. Otherwise, -1 is returned and 63.Va errno 64is set to indicate the type of 65error. 66.Sh ERRORS 67.Fn sem_wait 68and 69.Fn sem_trywait 70will fail if: 71.Bl -tag -width Er 72.It Bq Er EINVAL 73.Fa sem 74points to an invalid semaphore. 75.El 76.Pp 77Additionally, 78.Fn sem_trywait 79will fail if: 80.Bl -tag -width Er 81.It Bq Er EAGAIN 82The semaphore value was zero, and thus could not be decremented. 83.El 84.Sh SEE ALSO 85.Xr sem_post 3 86.Sh STANDARDS 87.Fn sem_wait 88and 89.Fn sem_trywait 90conform to ISO/IEC 9945-1 ANSI/IEEE 91.Pq Dq Tn POSIX 92Std 1003.1 1996 Edition. 93