1*8269e767SBrooks Davis.\" 2*8269e767SBrooks Davis.\" Copyright (c) 1995 David Hovemeyer <daveho@infocom.com> 3*8269e767SBrooks Davis.\" 4*8269e767SBrooks Davis.\" All rights reserved. 5*8269e767SBrooks Davis.\" 6*8269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without 7*8269e767SBrooks Davis.\" modification, are permitted provided that the following conditions 8*8269e767SBrooks Davis.\" are met: 9*8269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright 10*8269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer. 11*8269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright 12*8269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer in the 13*8269e767SBrooks Davis.\" documentation and/or other materials provided with the distribution. 14*8269e767SBrooks Davis.\" 15*8269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR 16*8269e767SBrooks Davis.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17*8269e767SBrooks Davis.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18*8269e767SBrooks Davis.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, 19*8269e767SBrooks Davis.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20*8269e767SBrooks Davis.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21*8269e767SBrooks Davis.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22*8269e767SBrooks Davis.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23*8269e767SBrooks Davis.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24*8269e767SBrooks Davis.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25*8269e767SBrooks Davis.\" 26*8269e767SBrooks Davis.Dd September 22, 1995 27*8269e767SBrooks Davis.Dt SEMOP 2 28*8269e767SBrooks Davis.Os 29*8269e767SBrooks Davis.Sh NAME 30*8269e767SBrooks Davis.Nm semop 31*8269e767SBrooks Davis.Nd atomic array of operations on a semaphore set 32*8269e767SBrooks Davis.Sh LIBRARY 33*8269e767SBrooks Davis.Lb libc 34*8269e767SBrooks Davis.Sh SYNOPSIS 35*8269e767SBrooks Davis.In sys/types.h 36*8269e767SBrooks Davis.In sys/ipc.h 37*8269e767SBrooks Davis.In sys/sem.h 38*8269e767SBrooks Davis.Ft int 39*8269e767SBrooks Davis.Fn semop "int semid" "struct sembuf *array" "size_t nops" 40*8269e767SBrooks Davis.Sh DESCRIPTION 41*8269e767SBrooks DavisThe 42*8269e767SBrooks Davis.Fn semop 43*8269e767SBrooks Davissystem call 44*8269e767SBrooks Davisatomically performs the array of operations indicated by 45*8269e767SBrooks Davis.Fa array 46*8269e767SBrooks Davison the semaphore set indicated by 47*8269e767SBrooks Davis.Fa semid . 48*8269e767SBrooks DavisThe length of 49*8269e767SBrooks Davis.Fa array 50*8269e767SBrooks Davisis indicated by 51*8269e767SBrooks Davis.Fa nops . 52*8269e767SBrooks DavisEach operation is encoded in a 53*8269e767SBrooks Davis.Vt "struct sembuf" , 54*8269e767SBrooks Daviswhich is defined as follows: 55*8269e767SBrooks Davis.\" 56*8269e767SBrooks Davis.\" From <sys/sem.h> 57*8269e767SBrooks Davis.\" 58*8269e767SBrooks Davis.Bd -literal 59*8269e767SBrooks Davisstruct sembuf { 60*8269e767SBrooks Davis u_short sem_num; /* semaphore # */ 61*8269e767SBrooks Davis short sem_op; /* semaphore operation */ 62*8269e767SBrooks Davis short sem_flg; /* operation flags */ 63*8269e767SBrooks Davis}; 64*8269e767SBrooks Davis.Ed 65*8269e767SBrooks Davis.Pp 66*8269e767SBrooks DavisFor each element in 67*8269e767SBrooks Davis.Fa array , 68*8269e767SBrooks Davis.Va sem_op 69*8269e767SBrooks Davisand 70*8269e767SBrooks Davis.Va sem_flg 71*8269e767SBrooks Davisdetermine an operation to be performed on semaphore number 72*8269e767SBrooks Davis.Va sem_num 73*8269e767SBrooks Davisin the set. 74*8269e767SBrooks DavisThe values 75*8269e767SBrooks Davis.Dv SEM_UNDO 76*8269e767SBrooks Davisand 77*8269e767SBrooks Davis.Dv IPC_NOWAIT 78*8269e767SBrooks Davismay be 79*8269e767SBrooks Davis.Em OR Ns 'ed 80*8269e767SBrooks Davisinto the 81*8269e767SBrooks Davis.Va sem_flg 82*8269e767SBrooks Davismember in order to modify the behavior of the given operation. 83*8269e767SBrooks Davis.Pp 84*8269e767SBrooks DavisThe operation performed depends as follows on the value of 85*8269e767SBrooks Davis.Va sem_op : 86*8269e767SBrooks Davis.\" 87*8269e767SBrooks Davis.\" This section is based on the description of semop() in 88*8269e767SBrooks Davis.\" Stevens, _Advanced Programming in the UNIX Environment_, 89*8269e767SBrooks Davis.\" and the semop(2) description in The Open Group Unix2 specification. 90*8269e767SBrooks Davis.\" 91*8269e767SBrooks Davis.Bl -bullet 92*8269e767SBrooks Davis.It 93*8269e767SBrooks DavisWhen 94*8269e767SBrooks Davis.Va sem_op 95*8269e767SBrooks Davisis positive and the process has alter permission, 96*8269e767SBrooks Davisthe semaphore's value is incremented by 97*8269e767SBrooks Davis.Va sem_op Ns 's 98*8269e767SBrooks Davisvalue. 99*8269e767SBrooks DavisIf 100*8269e767SBrooks Davis.Dv SEM_UNDO 101*8269e767SBrooks Davisis specified, the semaphore's adjust on exit value is decremented by 102*8269e767SBrooks Davis.Va sem_op Ns 's 103*8269e767SBrooks Davisvalue. 104*8269e767SBrooks DavisA positive value for 105*8269e767SBrooks Davis.Va sem_op 106*8269e767SBrooks Davisgenerally corresponds to a process releasing a resource 107*8269e767SBrooks Davisassociated with the semaphore. 108*8269e767SBrooks Davis.It 109*8269e767SBrooks DavisThe behavior when 110*8269e767SBrooks Davis.Va sem_op 111*8269e767SBrooks Davisis negative and the process has alter permission, 112*8269e767SBrooks Davisdepends on the current value of the semaphore: 113*8269e767SBrooks Davis.Bl -bullet 114*8269e767SBrooks Davis.It 115*8269e767SBrooks DavisIf the current value of the semaphore is greater than or equal to 116*8269e767SBrooks Davisthe absolute value of 117*8269e767SBrooks Davis.Va sem_op , 118*8269e767SBrooks Davisthen the value is decremented by the absolute value of 119*8269e767SBrooks Davis.Va sem_op . 120*8269e767SBrooks DavisIf 121*8269e767SBrooks Davis.Dv SEM_UNDO 122*8269e767SBrooks Davisis specified, the semaphore's adjust on exit 123*8269e767SBrooks Davisvalue is incremented by the absolute value of 124*8269e767SBrooks Davis.Va sem_op . 125*8269e767SBrooks Davis.It 126*8269e767SBrooks DavisIf the current value of the semaphore is less than the absolute value of 127*8269e767SBrooks Davis.Va sem_op , 128*8269e767SBrooks Davisone of the following happens: 129*8269e767SBrooks Davis.\" XXX a *second* sublist? 130*8269e767SBrooks Davis.Bl -bullet 131*8269e767SBrooks Davis.It 132*8269e767SBrooks DavisIf 133*8269e767SBrooks Davis.Dv IPC_NOWAIT 134*8269e767SBrooks Daviswas specified, then 135*8269e767SBrooks Davis.Fn semop 136*8269e767SBrooks Davisreturns immediately with a return value of 137*8269e767SBrooks Davis.Er EAGAIN . 138*8269e767SBrooks Davis.It 139*8269e767SBrooks DavisOtherwise, the calling process is put to sleep until one of the following 140*8269e767SBrooks Davisconditions is satisfied: 141*8269e767SBrooks Davis.\" XXX We already have two sublists, why not a third? 142*8269e767SBrooks Davis.Bl -bullet 143*8269e767SBrooks Davis.It 144*8269e767SBrooks DavisSome other process removes the semaphore with the 145*8269e767SBrooks Davis.Dv IPC_RMID 146*8269e767SBrooks Davisoption of 147*8269e767SBrooks Davis.Xr semctl 2 . 148*8269e767SBrooks DavisIn this case, 149*8269e767SBrooks Davis.Fn semop 150*8269e767SBrooks Davisreturns immediately with a return value of 151*8269e767SBrooks Davis.Er EIDRM . 152*8269e767SBrooks Davis.It 153*8269e767SBrooks DavisThe process receives a signal that is to be caught. 154*8269e767SBrooks DavisIn this case, the process will resume execution as defined by 155*8269e767SBrooks Davis.Xr sigaction 2 . 156*8269e767SBrooks Davis.It 157*8269e767SBrooks DavisThe semaphore's 158*8269e767SBrooks Davisvalue is greater than or equal to the absolute value of 159*8269e767SBrooks Davis.Va sem_op . 160*8269e767SBrooks DavisWhen this condition becomes true, the semaphore's value is decremented 161*8269e767SBrooks Davisby the absolute value of 162*8269e767SBrooks Davis.Va sem_op , 163*8269e767SBrooks Davisthe semaphore's adjust on exit value is incremented by the 164*8269e767SBrooks Davisabsolute value of 165*8269e767SBrooks Davis.Va sem_op . 166*8269e767SBrooks Davis.El 167*8269e767SBrooks Davis.El 168*8269e767SBrooks Davis.El 169*8269e767SBrooks Davis.Pp 170*8269e767SBrooks DavisA negative value for 171*8269e767SBrooks Davis.Va sem_op 172*8269e767SBrooks Davisgenerally means that a process is waiting for a resource to become 173*8269e767SBrooks Davisavailable. 174*8269e767SBrooks Davis.It 175*8269e767SBrooks DavisWhen 176*8269e767SBrooks Davis.Va sem_op 177*8269e767SBrooks Davisis zero and the process has read permission, 178*8269e767SBrooks Davisone of the following will occur: 179*8269e767SBrooks Davis.Bl -bullet 180*8269e767SBrooks Davis.It 181*8269e767SBrooks DavisIf the current value of the semaphore is equal to zero 182*8269e767SBrooks Davisthen 183*8269e767SBrooks Davis.Fn semop 184*8269e767SBrooks Daviscan return immediately. 185*8269e767SBrooks Davis.It 186*8269e767SBrooks DavisIf 187*8269e767SBrooks Davis.Dv IPC_NOWAIT 188*8269e767SBrooks Daviswas specified, then 189*8269e767SBrooks Davis.Fn semop 190*8269e767SBrooks Davisreturns immediately with a return value of 191*8269e767SBrooks Davis.Er EAGAIN . 192*8269e767SBrooks Davis.It 193*8269e767SBrooks DavisOtherwise, the calling process is put to sleep until one of the following 194*8269e767SBrooks Davisconditions is satisfied: 195*8269e767SBrooks Davis.\" XXX Another nested sublists 196*8269e767SBrooks Davis.Bl -bullet 197*8269e767SBrooks Davis.It 198*8269e767SBrooks DavisSome other process removes the semaphore with the 199*8269e767SBrooks Davis.Dv IPC_RMID 200*8269e767SBrooks Davisoption of 201*8269e767SBrooks Davis.Xr semctl 2 . 202*8269e767SBrooks DavisIn this case, 203*8269e767SBrooks Davis.Fn semop 204*8269e767SBrooks Davisreturns immediately with a return value of 205*8269e767SBrooks Davis.Er EIDRM . 206*8269e767SBrooks Davis.It 207*8269e767SBrooks DavisThe process receives a signal that is to be caught. 208*8269e767SBrooks DavisIn this case, the process will resume execution as defined by 209*8269e767SBrooks Davis.Xr sigaction 2 . 210*8269e767SBrooks Davis.It 211*8269e767SBrooks DavisThe semaphore's value becomes zero. 212*8269e767SBrooks Davis.El 213*8269e767SBrooks Davis.El 214*8269e767SBrooks Davis.El 215*8269e767SBrooks Davis.Pp 216*8269e767SBrooks DavisFor each semaphore a process has in use, the kernel maintains an 217*8269e767SBrooks Davis.Dq "adjust on exit" 218*8269e767SBrooks Davisvalue, as alluded to earlier. 219*8269e767SBrooks DavisWhen a process 220*8269e767SBrooks Davisexits, either voluntarily or involuntarily, the adjust on exit value 221*8269e767SBrooks Davisfor each semaphore is added to the semaphore's value. 222*8269e767SBrooks DavisThis can 223*8269e767SBrooks Davisbe used to ensure that a resource is released if a process terminates 224*8269e767SBrooks Davisunexpectedly. 225*8269e767SBrooks Davis.Sh RETURN VALUES 226*8269e767SBrooks Davis.Rv -std semop 227*8269e767SBrooks Davis.Sh ERRORS 228*8269e767SBrooks DavisThe 229*8269e767SBrooks Davis.Fn semop 230*8269e767SBrooks Davissystem call will fail if: 231*8269e767SBrooks Davis.Bl -tag -width Er 232*8269e767SBrooks Davis.It Bq Er EINVAL 233*8269e767SBrooks DavisNo semaphore set corresponds to 234*8269e767SBrooks Davis.Fa semid , 235*8269e767SBrooks Davisor the process would exceed the system-defined limit for the number of 236*8269e767SBrooks Davisper-process 237*8269e767SBrooks Davis.Dv SEM_UNDO 238*8269e767SBrooks Davisstructures. 239*8269e767SBrooks Davis.It Bq Er EACCES 240*8269e767SBrooks DavisPermission denied due to mismatch between operation and mode of 241*8269e767SBrooks Davissemaphore set. 242*8269e767SBrooks Davis.It Bq Er EAGAIN 243*8269e767SBrooks DavisThe semaphore's value would have resulted in the process being put to sleep 244*8269e767SBrooks Davisand 245*8269e767SBrooks Davis.Dv IPC_NOWAIT 246*8269e767SBrooks Daviswas specified. 247*8269e767SBrooks Davis.It Bq Er E2BIG 248*8269e767SBrooks DavisToo many operations were specified. 249*8269e767SBrooks Davis.Bq Dv SEMOPM 250*8269e767SBrooks Davis.It Bq Er EFBIG 251*8269e767SBrooks Davis.\" 252*8269e767SBrooks Davis.\" I'd have thought this would be EINVAL, but the source says 253*8269e767SBrooks Davis.\" EFBIG. 254*8269e767SBrooks Davis.\" 255*8269e767SBrooks Davis.Va sem_num 256*8269e767SBrooks Daviswas not in the range of valid semaphores for the set. 257*8269e767SBrooks Davis.It Bq Er EIDRM 258*8269e767SBrooks DavisThe semaphore set was removed from the system. 259*8269e767SBrooks Davis.It Bq Er EINTR 260*8269e767SBrooks DavisThe 261*8269e767SBrooks Davis.Fn semop 262*8269e767SBrooks Davissystem call was interrupted by a signal. 263*8269e767SBrooks Davis.It Bq Er ENOSPC 264*8269e767SBrooks DavisThe system 265*8269e767SBrooks Davis.Dv SEM_UNDO 266*8269e767SBrooks Davispool 267*8269e767SBrooks Davis.Bq Dv SEMMNU 268*8269e767SBrooks Davisis full. 269*8269e767SBrooks Davis.It Bq Er ERANGE 270*8269e767SBrooks DavisThe requested operation would cause either 271*8269e767SBrooks Davisthe semaphore's current value 272*8269e767SBrooks Davis.Bq Dv SEMVMX 273*8269e767SBrooks Davisor its adjust on exit value 274*8269e767SBrooks Davis.Bq Dv SEMAEM 275*8269e767SBrooks Davisto exceed the system-imposed limits. 276*8269e767SBrooks Davis.El 277*8269e767SBrooks Davis.Sh SEE ALSO 278*8269e767SBrooks Davis.Xr semctl 2 , 279*8269e767SBrooks Davis.Xr semget 2 , 280*8269e767SBrooks Davis.Xr sigaction 2 281*8269e767SBrooks Davis.Sh BUGS 282*8269e767SBrooks DavisThe 283*8269e767SBrooks Davis.Fn semop 284*8269e767SBrooks Davissystem call 285*8269e767SBrooks Davismay block waiting for memory even if 286*8269e767SBrooks Davis.Dv IPC_NOWAIT 287*8269e767SBrooks Daviswas specified. 288