1.\" $OpenBSD: semget.2,v 1.17 2007/05/31 19:19:33 jmc Exp $ 2.\" $NetBSD: semget.2,v 1.2 1997/03/27 08:20:41 mikel Exp $ 3.\" 4.\" Copyright (c) 1995 Frank van der Linden 5.\" All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. All advertising materials mentioning features or use of this software 16.\" must display the following acknowledgement: 17.\" This product includes software developed for the NetBSD Project 18.\" by Frank van der Linden 19.\" 4. The name of the author may not be used to endorse or promote products 20.\" derived from this software without specific prior written permission 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32.\"/ 33.Dd $Mdocdate: May 31 2007 $ 34.Dt SEMGET 2 35.Os 36.Sh NAME 37.Nm semget 38.Nd get semaphore set 39.Sh SYNOPSIS 40.Fd #include <sys/types.h> 41.Fd #include <sys/ipc.h> 42.Fd #include <sys/sem.h> 43.Ft int 44.Fn semget "key_t key" "int nsems" "int semflg" 45.Sh DESCRIPTION 46The 47.Fn semget 48system call returns the semaphore identifier associated with 49.Fa key . 50.Pp 51A new set containing 52.Fa nsems 53semaphores is created if either 54.Fa key 55is equal to 56.Dv IPC_PRIVATE , 57or 58.Fa key 59does not have a semaphore set associated with it and the 60.Dv IPC_CREAT 61bit is set in 62.Fa semflg . 63.Pp 64The access modes of the created semaphores is specified in 65.Fa semflg 66by 67.Tn OR Ns 'ing 68the following values: 69.Bd -literal -offset indent 70SEM_A alter permission for owner 71SEM_R read permission for owner 72 73SEM_A >> 3 alter permission for group 74SEM_R >> 3 read permission for group 75 76SEM_A >> 6 alter permission for other 77SEM_R >> 6 read permission for other 78.Ed 79.Pp 80If a new set of semaphores is created, the data structure associated with it 81(the 82.Va semid_ds 83structure, see 84.Xr semctl 2 ) 85is initialized as follows: 86.Bl -bullet 87.It 88.Va sem_perm.cuid 89and 90.Va sem_perm.uid 91are set to the effective UID of the calling process. 92.It 93.Va sem_perm.gid 94and 95.Va sem_perm.cgid 96are set to the effective GID of the calling process. 97.It 98.Va sem_perm.mode 99is set to the lower 9 bits of 100.Fa semflg . 101.It 102.Va sem_nsems 103is set to the value of 104.Fa nsems . 105.It 106.Va sem_ctime 107is set to the current time. 108.It 109.Va sem_otime 110is set to 0. 111.El 112.Sh RETURN VALUES 113.Fn semget 114returns a non-negative semaphore identifier if successful. 115Otherwise, \-1 is returned and 116.Va errno 117is set to reflect the error. 118.Sh ERRORS 119.Bl -tag -width Er 120.It Bq Er EACCES 121The caller has no permission to access a semaphore set already associated with 122.Fa key . 123.It Bq Er EEXIST 124Both 125.Dv IPC_CREAT 126and 127.Dv IPC_EXCL 128are set in 129.Fa semflg , 130and a semaphore set is already associated with 131.Fa key . 132.It Bq Er EINVAL 133.Va nsems 134is less than or equal to 0 or greater than the system limit for the 135number in a semaphore set. 136.Pp 137A semaphore set associated with 138.Fa key 139exists, but has fewer semaphores than the number specified in 140.Fa nsems . 141.It Bq Er ENOSPC 142A new set of semaphores could not be created because the system limit 143for the number of semaphores or the number of semaphore sets has been 144reached. 145.It Bq Er ENOENT 146.Dv IPC_CREAT 147was not set in 148.Fa semflg 149and no semaphore set associated with 150.Fa key 151was found. 152.El 153.Sh SEE ALSO 154.Xr semctl 2 , 155.Xr semop 2 , 156.Xr ftok 3 157