Plan 9 from Bell Labs’s /usr/web/sources/contrib/fgb/root/sys/lib/ape/man/3/semget

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


.\" Copyright (c) 2001-2003 The Open Group, All Rights Reserved 
.TH "SEMGET" 3P 2003 "IEEE/The Open Group" "POSIX Programmer's Manual"
.\" semget 
.SH PROLOG
This manual page is part of the POSIX Programmer's Manual.
The Linux implementation of this interface may differ (consult
the corresponding Linux manual page for details of Linux behavior),
or the interface may not be implemented on Linux.
.SH NAME
semget \- get set of XSI semaphores
.SH SYNOPSIS
.LP
\fB#include <sys/sem.h>
.br
.sp
int semget(key_t\fP \fIkey\fP\fB, int\fP \fInsems\fP\fB, int\fP \fIsemflg\fP\fB);
\fP
\fB
.br
\fP
.SH DESCRIPTION
.LP
The \fIsemget\fP() function operates on XSI semaphores (see the Base
Definitions volume of IEEE\ Std\ 1003.1-2001, Section 4.15, Semaphore).
It is unspecified whether this function interoperates
with the realtime interprocess communication facilities defined in
\fIRealtime\fP .
.LP
The \fIsemget\fP() function shall return the semaphore identifier
associated with \fIkey\fP.
.LP
A semaphore identifier with its associated \fBsemid_ds\fP data structure
and its associated set of \fInsems\fP semaphores (see
\fI<sys/sem.h>\fP) is created for \fIkey\fP if one of the following
is true:
.IP " *" 3
The argument \fIkey\fP is equal to IPC_PRIVATE.
.LP
.IP " *" 3
The argument \fIkey\fP does not already have a semaphore identifier
associated with it and (\fIsemflg\fP &IPC_CREAT) is
non-zero.
.LP
.LP
Upon creation, the \fBsemid_ds\fP data structure associated with the
new semaphore identifier is initialized as follows:
.IP " *" 3
In the operation permissions structure \fIsem_perm.cuid\fP, \fIsem_perm.uid\fP,
\fIsem_perm.cgid\fP, and \fIsem_perm.gid\fP
shall be set equal to the effective user ID and effective group ID,
respectively, of the calling process.
.LP
.IP " *" 3
The low-order 9 bits of \fIsem_perm.mode\fP shall be set equal to
the low-order 9 bits of \fIsemflg\fP.
.LP
.IP " *" 3
The variable \fIsem_nsems\fP shall be set equal to the value of \fInsems\fP.
.LP
.IP " *" 3
The variable \fIsem_otime\fP shall be set equal to 0 and \fIsem_ctime\fP
shall be set equal to the current time.
.LP
.IP " *" 3
The data structure associated with each semaphore in the set shall
not be initialized. The \fIsemctl\fP() function with the command SETVAL
or SETALL can be used to initialize each
semaphore.
.LP
.SH RETURN VALUE
.LP
Upon successful completion, \fIsemget\fP() shall return a non-negative
integer, namely a semaphore identifier; otherwise, it
shall return -1 and set \fIerrno\fP to indicate the error.
.SH ERRORS
.LP
The \fIsemget\fP() function shall fail if:
.TP 7
.B EACCES
A semaphore identifier exists for \fIkey\fP, but operation permission
as specified by the low-order 9 bits of \fIsemflg\fP
would not be granted; see \fIXSI Interprocess Communication\fP .
.TP 7
.B EEXIST
A semaphore identifier exists for the argument \fIkey\fP but ((\fIsemflg\fP
&IPC_CREAT) &&(\fIsemflg\fP
&IPC_EXCL)) is non-zero.
.TP 7
.B EINVAL
The value of \fInsems\fP is either less than or equal to 0 or greater
than the system-imposed limit, or a semaphore identifier
exists for the argument \fIkey\fP, but the number of semaphores in
the set associated with it is less than \fInsems\fP and
\fInsems\fP is not equal to 0.
.TP 7
.B ENOENT
A semaphore identifier does not exist for the argument \fIkey\fP and
(\fIsemflg\fP &IPC_CREAT) is equal to 0.
.TP 7
.B ENOSPC
A semaphore identifier is to be created but the system-imposed limit
on the maximum number of allowed semaphores system-wide
would be exceeded.
.sp
.LP
\fIThe following sections are informative.\fP
.SH EXAMPLES
.SS Creating a Semaphore Identifier
.LP
The following example gets a unique semaphore key using the \fIftok\fP()
function, then
gets a semaphore ID associated with that key using the \fIsemget\fP()
function (the first call also tests to make sure the
semaphore exists). If the semaphore does not exist, the program creates
it, as shown by the second call to \fIsemget\fP(). In
creating the semaphore for the queuing process, the program attempts
to create one semaphore with read/write permission for all. It
also uses the IPC_EXCL flag, which forces \fIsemget\fP() to fail if
the semaphore already exists.
.LP
After creating the semaphore, the program uses a call to \fIsemop\fP()
to initialize it
to the values in the \fIsbuf\fP array. The number of processes that
can execute concurrently without queuing is initially set to
2. The final call to \fIsemget\fP() creates a semaphore identifier
that can be used later in the program.
.sp
.RS
.nf

\fB#include <sys/types.h>
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <pwd.h>
#include <fcntl.h>
#include <limits.h>
\&...
key_t semkey;
int semid, pfd, fv;
struct sembuf sbuf;
char *lgn;
char filename[PATH_MAX+1];
struct stat outstat;
struct passwd *pw;
\&...
/* Get unique key for semaphore. */
if ((semkey = ftok("/tmp", 'a')) == (key_t) -1) {
    perror("IPC error: ftok"); exit(1);
}
.sp

/* Get semaphore ID associated with this key. */
if ((semid = semget(semkey, 0, 0)) == -1) {
.sp

    /* Semaphore does not exist - Create. */
    if ((semid = semget(semkey, 1, IPC_CREAT | IPC_EXCL | S_IRUSR |
        S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) != -1)
    {
        /* Initialize the semaphore. */
        sbuf.sem_num = 0;
        sbuf.sem_op = 2;  /* This is the number of runs
                             without queuing. */
        sbuf.sem_flg = 0;
        if (semop(semid, &sbuf, 1) == -1) {
            perror("IPC error: semop"); exit(1);
        }
    }
    else if (errno == EEXIST) {
        if ((semid = semget(semkey, 0, 0)) == -1) {
            perror("IPC error 1: semget"); exit(1);
        }
    }
    else {
        perror("IPC error 2: semget"); exit(1);
    }
}
\&...
\fP
.fi
.RE
.SH APPLICATION USAGE
.LP
The POSIX Realtime Extension defines alternative interfaces for interprocess
communication. Application developers who need to
use IPC should design their applications so that modules using the
IPC routines described in \fIXSI Interprocess Communication\fP can
be easily modified to use the alternative
interfaces.
.SH RATIONALE
.LP
None.
.SH FUTURE DIRECTIONS
.LP
None.
.SH SEE ALSO
.LP
\fIXSI Interprocess Communication\fP, \fIRealtime\fP, \fIsemctl\fP(),
\fIsemop\fP(), \fIsem_close\fP(), \fIsem_destroy\fP(), \fIsem_getvalue\fP(),
\fIsem_init\fP(), \fIsem_open\fP(), \fIsem_post\fP(), \fIsem_unlink\fP(),
\fIsem_wait\fP(), the Base Definitions volume of IEEE\ Std\ 1003.1-2001,
\fI<sys/sem.h>\fP
.SH COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form
from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
-- Portable Operating System Interface (POSIX), The Open Group Base
Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
Electrical and Electronics Engineers, Inc and The Open Group. In the
event of any discrepancy between this version and the original IEEE and
The Open Group Standard, the original IEEE and The Open Group Standard
is the referee document. The original Standard can be obtained online at
http://www.opengroup.org/unix/online.html .

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.