sem_close a close a named POSIX semaphore #include semaphore

advertisement
sem_close − close a named POSIX semaphore #include <sys/semaphore.h> int sem_close(sem_t *sem);
sem_close() is used to close a named semaphore. A successful call to sem_close() will do the following: Remove the process’s descriptor for the semaphore referenced by the specified sem_t structure Remove the
semaphore referenced by the specified sem_t structure if the semaphore is marked for removal by a call to
sem_unlink() and there are no other descriptors referencing this semaphore. When the process’s descriptor
for the semaphore referenced by is removed, subsequent use of this semaphore by this process will fail. Descriptors for named semaphores are also removed by processes on exit. Calling sem_close() does not affect
other processes referencing the same semaphore. To use this function, link in the realtime library by specifying -lrt on the compiler or linker command line. The following call to sem_close() will close a named
semaphore referred to by by removing the process’s descriptor to the semaphore and removing the
semaphore if it is marked for removal by a previous sem_unlink() and there are no descriptors referencing it.
sem_close(sem);
If the semaphore was closed and the descriptors referencing it were removed, sem_close() returns 0 to the
caller. If the semaphore could not be closed, the call returns -1 and sets errno to indicate the error.
sem_close() fails and does not perform the requested operation if the following condition is encountered:
[EINVAL] The argument is not a valid named semaphore. sem_init(2), sem_open(2), sem_unlink(2),
<semaphore.h>. sem_close(): POSIX sem_destroy − destroy an unnamed POSIX semaphore #include
<sys/semaphore.h> int sem_destroy(sem_t *sem); sem_destroy() is used to destroy an unnamed semaphore. A
successful call to sem_destroy() will invalidate the unnamed semaphore referred to by and removes all descriptors referencing it. The semaphore should have been created by a previous call to sem_init() and there
should not be any processes blocked on it. To use this function, link in the realtime library by specifying -lrt
on the compiler or linker command line. The following call to sem_destroy() will destroy an unnamed
semaphore referred to by and remove all descriptors referencing it.
sem_destroy(sem);
If the semaphore was destroyed and the descriptors referencing it were removed, sem_destroy() returns 0 to
the caller. If the semaphore could not be destroyed, the call returns -1 and sets errno to indicate the error.
sem_destroy() fails and does not perform the requested operation if any of the following conditions are encountered: [EBUSY] There are threads currently blocked on the semaphore or there are outstanding locks
held on the semaphore. [EINVAL] The argument is not a valid unnamed semaphore. sem_init(2),
sem_open(2), <semaphore.h>. sem_destroy(): POSIX sem_getvalue − get the value of a POSIX semaphore #include <sys/semaphore.h> int sem_getvalue(sem_t *sem, int *sval); sem_getvalue() is used to read the value of
the semaphore. The value of the semaphore specified by is read, at some unspecified time during the call, and
then stored into If the semaphore value is <= 0, at that time, the semaphore is considered unavailable. If the
semaphore value is > 0, at that time, the semaphore is considered available. If is positive, it is equal to the
number of locks available on the semaphore, at the time the semaphore value was read. If is negative, its absolute value is equal to the number of blocked threads waiting for the semaphore to become available, at the
time the semaphore value was read. If the specified semaphore referred to by is a named semaphore, then
this semaphore must have been opened by the calling process with sem_open() and the process must have
read permission on this semaphore. To use this function, link in the realtime library by specifying -lrt on the
compiler or linker command line. The following call to sem_getvalue() will read the value of the semaphore
and store it in
sem_getvalue(sem,sval);
A successful call to sem_getvalue() will return 0. Otherwise, the call to sem_getvalue() will return -1 with errno set to the appropriate value of the error condition. sem_getvalue() fails and does not perform the requested operation if any of the following conditions are encountered: [EPERM] The calling process does not have
the privileges necessary to read the semaphore. [EINVAL] The argument does not refer to a valid semaphore.
sem_open(2), <semaphore.h>. sem_getvalue(): POSIX sem_init − initialize an unnamed POSIX semaphore #include <sys/semaphore.h> int sem_init(sem_t *sem, int pshared, unsigned int value); sem_init() is used to initialize an unnamed semaphore. A successful call to sem_init() will create a new unnamed semaphore referred
to by if one does not exist, initialize the unnamed semaphore descriptor, referred to by to the non-negative
value specified by If the unnamed semaphore already exists, i.e. created by a previous call to sem_init(), it is
re-initialized only if its current value is equal to its initial value (set by the last successful call to sem_init()). If so,
the initial value of the unnamed semaphore is re-initialized to the value argument. Otherwise, the call fails. The argument specifies if the unnamed semaphore is sharable with other processes. If is equal to 0, the unnamed
semaphore is not shared with other processes. If is non-zero, the unnamed semaphore is sharable with any processes
that can access The access mode specified for the unnamed semaphore allows read and write permissions to all processes. If the calling process may attach to the shared sem_t structure, it is assumed it may operate on the
semaphore. To use this function, link in the realtime library by specifying -lrt on the compiler or linker command line. The following call to sem_init() will create a new unnamed semaphore referred to by if one does
not exist, initialize the unnamed semaphore descriptor, referred to by to the non-negative value specified by
sem_init(sem, pshared, value);
If the semaphore was created and initialized, sem_init() returns 0 to the caller. If the semaphore could not be
created/initialized, the call returns -1 and sets errno to indicate the error. sem_init() fails and does not perform the requested operation if any of the following conditions are encountered: [EPERM] The calling process does not have the privileges necessary to initialize the semaphore. [EBUSY] There are threads currently
blocked on the semaphore or there are outstanding locks held on the semaphore. [EINVAL] The argument is
greater than {_POSIX_SEM_VALUE_MAX}. [ENOSPC] There are insufficient resources to perform the operation or the upper limit on the number of semaphores is reached. sem_destroy(2), sem_post(2), sem_trywait(2),
sem_wait(2), <semaphore.h>. sem_init(): POSIX sem_open − create/open a named POSIX semaphore #include
<sys/semaphore.h> sem_t * sem_open(const char *name, int oflag, mode_t mode,
unsigned int value); sem_open() is used to open or create a named semaphore. A successful call to
sem_open() will create a descriptor for the semaphore specified by The pointer to the semaphore returned by
sem_open() can be used to access the semaphore associated with in subsequent operations. The argument
points to a string referring to a semaphore. It should begin with a "/" and shall conform to pathname rules
except that no path component should be "." or "..". The argument specifies whether a semaphore is to be
created or not. The following bits in it may be set: O_CREAT If this flag is set, a new semaphore is created if
it does not already exist. If this flag is not set, the semaphore should already exist. O_EXCL If this flag is set,
the call fails if the semaphore already exists. This flag is valid only when O_CREAT is also set; otherwise, it is
ignored. The and arguments are provided to supply the permissions and the initial value information necessary for creating a new semaphore. To use this function, link in the realtime library by specifying -lrt on the
compiler or linker command line. The following call to sem_open() will create a new named semaphore if one
does not exist, which depends on the flags specified in has the permissions specified in and has an initial value
of
sem_open(name, oflag, mode, value);
If the semaphore was created and initialized, sem_open() returns a pointer to a sem_t structure containing the
index of the new descriptor. If the semaphore could not be created/initialized, the call returns -1 and sets errno to indicate the error. If the named semaphore is already opened by the calling process, a descriptor and a
sem_t structure for the named semaphore already exists for the calling process. A new descriptor is not created and a pointer to the existing sem_t structure is returned for this call. sem_open() fails and does not perform the requested operation if any of the following conditions are encountered: [EACCES] The named
semaphore exists and the process does not have the permissions to open the semaphore as described by or the
named semaphore does not exist and the process does not have the permission to open it. [EEXIST] The flags
O_CREAT and O_EXCL are set in and the named semaphore exists. [EINTR] A signal interrupted the
sem_open() operation. [EINVAL] The argument is greater than {_POSIX_SEM_VALUE_MAX} and the
O_CREAT flag was specified in [EINVAL] The argument does not begin with "/" or contains "." or ".." as a
pathname component. [EMFILE] Too many semaphore descriptors are currently in use by this process.
[ENAMETOOLONG] The name string is longer than {PATH_MAX}. [ENFILE] There are too many
semaphores in the system. [ENOENT] The flag O_CREAT is not set in and the named semaphore does not exist. [ENOSPC] There are insufficient resources for the creation of a new named semaphore. sem_close(2),
sem_post(2), sem_wait(2), sem_unlink(2), <semaphore.h>. sem_open(): POSIX sem_post − unlock a POSIX
semaphore #include <sys/semaphore.h> int sem_post(sem_t *sem); sem_post() is used to post the semaphore
referenced by The calling thread will not return from its call to sem_post() unless it can either: increment the
semaphore value, if there are no blocked threads on this semaphore; give the semaphore to a blocked thread,
if there are any blocked threads on this semaphore; or have an error condition. If the semaphore value is < 0,
the semaphore has blocked threads, waiting for it to become available (the absolute value of the semaphore’s
value indicates the number of waiters at that moment). If the semaphore value is >= 0, the semaphore has no
waiters. If the semaphore has no waiters at the time its value is checked, the semaphore’s value will be atomically incremented, with respect to the checking of its value, up to its maximum value as specified by
{_POSIX_SEM_VALUE_MAX}. If the semaphore has waiters at the time its value is checked, the semaphore value is not changed. Instead, the calling thread will attempt to wake up a waiter. If the semaphore has waiters having
realtime priorities, the thread must wake up the highest priority waiter. Otherwise the thread at the head of the channel queue is woken up. When a waiter is successfully woken, the semaphore being posted will be given to the
woken waiter. In other words, the state of the semaphore remains unchanged. Instead, the semaphore being posted
will be inherited by the waiter being woken from this call to sem_post(). If the specified semaphore referred to by is
a named semaphore, then this semaphore must have been opened by the calling process with sem_open(). The calling process must have both read and write permissions on the semaphore to perform this operation. The sem_post()
routine may be called asynchronously, i.e. from a signal handler. To use this function, link in the realtime library by specifying -lrt on the compiler or linker command line. The following call to sem_post() will post the
semaphore
sem_post(sem);
A successful call to sem_post() will return 0 and the calling thread would have posted the semaphore. Otherwise, the call to sem_post() will return -1 with errno set to the appropriate value of the error condition.
sem_post() fails and does not perform the requested operation if any of the following conditions are encountered: [EPERM] The calling process does not have the privileges necessary to post the semaphore. [EINVAL]
The argument does not refer to a valid semaphore. <semaphore.h>. sem_post(): POSIX sem_wait, sem_trywait − lock a POSIX semaphore #include <sys/semaphore.h> int sem_wait(sem_t *sem); int sem_trywait(sem_t
*sem); sem_wait() is used to lock a semaphore. The calling thread will not return from its call to sem_wait()
until one of the following events occur: it successfully obtains a lock on the semaphore; it is interrupted by a
signal or an error condition occurs. sem_trywait() is used to lock a semaphore, if it is available. The value of
the semaphore is checked at some unspecified time during the call. If the semaphore is available at the time
its value is checked, the calling thread will atomically, with respect to the checking of the value, lock the
semaphore. The thread will now own a lock on the semaphore; the call will return successfully. If the
semaphore is unavailable at the time its value is checked, then the call returns -1 with errno set to EAGAIN.
If the specified semaphore referred to by is a named semaphore, then this semaphore must have been opened
by the calling process with sem_open(). The calling process must have both read and write permissions on the
semaphore to perform these operations. The semaphore will be locked upon successful return and will stay locked
until it is explicitly released by a call to sem_post(). To use this function, link in the realtime library by specifying
-lrt on the compiler or linker command line. The following call to sem_wait() will lock the semaphore
sem_wait(sem);
The following call to sem_trywait() will lock the semaphore if it is available.
sem_trywait(sem);
A successful call to sem_wait() will return 0 and the calling thread will then own a lock on the semaphore.
Otherwise, the call to sem_wait() will return -1 with errno set to the appropriate value of the error condition.
A successful call to sem_trywait() will return 0, if the semaphore was available and the calling thread was able
to lock the semaphore. Otherwise, the call to sem_trywait() will return -1 with errno set to the appropriate
value of the error condition. sem_wait() and sem_trywait() fail and do not perform the requested operation if
any of the following conditions are encountered: [EPERM] The calling process does not have the privileges
necessary to lock the semaphore. [EAGAIN] The semaphore was not available and hence could not be locked
by sem_trywait(). This error condition only occurs in sem_trywait(). [EINVAL] The argument does not refer to a
valid semaphore. [EINTR] The function was interrupted by a signal sem_post(2), <semaphore.h>.
sem_wait(),sem_trywait(): POSIX sem_unlink − unlink a named POSIX semaphore #include <sys/semaphore.h>
int sem_unlink(const char *name); sem_unlink() is used to unlink named semaphores. A successful call to
sem_unlink() marks the semaphore, specified by for removal. Calling sem_unlink() does not affect processes,
including the calling process, which currently have a descriptor, obtained from a call to sem_open(). Named
semaphores are uniquely identified by character strings. All character string names will be pre-processed to ensure
variations of a pathname resolve to the same semaphore name. If the semaphore is successfully marked for removal
by a call to sem_unlink(), the semaphore will be removed when all processes remove their descriptors to the specified semaphore by calling sem_close(). Subsequent calls to sem_open() using the string will refer to a new
semaphore. To use this function, link in the realtime library by specifying -lrt on the compiler or linker command line. The following call to sem_unlink() will remove the named semaphore named by the string If the
semaphore is currently referenced by one or more processes, the semaphore will be marked for removal and
removed when there are no more processes referencing it.
sem_unlink(name);
If the semaphore was unlinked successfully, sem_unlink() returns 0. If the semaphore could not be unlinked,
the call returns -1 and sets errno to indicate the error. sem_unlink() fails and does not perform the requested
operation if any of the following conditions are encountered: [EACCES] The named semaphore exists and the
process does not have the permissions to unlink the semaphore. [ENAMETOOLONG] The string is longer
than {PATH_MAX}. [ENOENT] The flag O_CREAT is not set in oflag (see and the named semaphore does
not exist. sem_close(2), sem_open(2), <semaphore.h>. sem_unlink(): POSIX
sem_wait, sem_trywait − lock a POSIX semaphore #include <sys/semaphore.h> int sem_wait(sem_t *sem); int
sem_trywait(sem_t *sem); sem_wait() is used to lock a semaphore. The calling thread will not return from its
call to sem_wait() until one of the following events occur: it successfully obtains a lock on the semaphore; it is
interrupted by a signal or an error condition occurs. sem_trywait() is used to lock a semaphore, if it is available. The value of the semaphore is checked at some unspecified time during the call. If the semaphore is
available at the time its value is checked, the calling thread will atomically, with respect to the checking of the
value, lock the semaphore. The thread will now own a lock on the semaphore; the call will return successfully. If the semaphore is unavailable at the time its value is checked, then the call returns -1 with errno set to
EAGAIN. If the specified semaphore referred to by is a named semaphore, then this semaphore must have
been opened by the calling process with sem_open(). The calling process must have both read and write permissions on the semaphore to perform these operations. The semaphore will be locked upon successful return and will
stay locked until it is explicitly released by a call to sem_post(). To use this function, link in the realtime library by
specifying -lrt on the compiler or linker command line. The following call to sem_wait() will lock the
semaphore
sem_wait(sem);
The following call to sem_trywait() will lock the semaphore if it is available.
sem_trywait(sem);
A successful call to sem_wait() will return 0 and the calling thread will then own a lock on the semaphore.
Otherwise, the call to sem_wait() will return -1 with errno set to the appropriate value of the error condition.
A successful call to sem_trywait() will return 0, if the semaphore was available and the calling thread was able
to lock the semaphore. Otherwise, the call to sem_trywait() will return -1 with errno set to the appropriate
value of the error condition. sem_wait() and sem_trywait() fail and do not perform the requested operation if
any of the following conditions are encountered: [EPERM] The calling process does not have the privileges
necessary to lock the semaphore. [EAGAIN] The semaphore was not available and hence could not be locked
by sem_trywait(). This error condition only occurs in sem_trywait(). [EINVAL] The argument does not refer to a
valid semaphore. [EINTR] The function was interrupted by a signal sem_post(2), <semaphore.h>.
sem_wait(),sem_trywait(): POSIX semctl − semaphore control operations
#include <sys/sem.h>
int semctl(int semid,
int semnum,
int cmd,
union arg
);
union semun {
int val;
struct semid_ds ∗buf;
ushort ∗array;
} arg;
The semctl() system call provides a variety of semaphore control operations as specified by For the meaning
of unspecified variables, see in The following values for are executed with respect to the semaphore specified
by and GETVAL Return the value of Requires semaphore Read permission. SETVAL Set the value of to
where is the fourth argument of semctl() taken as an int. When this is successfully executed, the value corresponding to the specified semaphore in all processes is cleared. Requires semaphore Alter permission. GETPID
Return the value of Requires semaphore Read permission. GETNCNT Return the value of Requires
semaphore Read permission. GETZCNT Return the value of Requires semaphore Read permission. The following values for return and set, respectively, every in the set of semaphores. GETALL Place into array
pointed to by where is the fourth argument of semctl() taken as a pointer to unsigned short int. Requires
semaphore Read permission. SETALL Set according to the array pointed to by where is the fourth argument
of semctl() taken as a pointer to unsigned short int. When this is successfully executed, the values corresponding
to each specified semaphore in all processes are cleared. Requires semaphore Alter permission. The following values for are also available: IPC_STAT Place the current value of each member of the data structure associated
with into the structure pointed to by where is the fourth argument of semctl() taken as a pointer to struct
semid_ds. The contents of this structure are defined in Requires semaphore Read permission. IPC_SET Set the
value of the following members of the data structure associated with to the corresponding value found in the
structure pointed to by where is the fourth argument of semctl() taken as a pointer to struct semid_ds:
sem_perm.uid
sem_perm.gid
sem_perm.mode /* only low 9 bits */
This can only be executed by a process that has an effective user ID equal to either that of superuser or to the
value of either sem_perm.uid or sem_perm.cuid in the data structure associated with IPC_RMID Remove the
semaphore identifier specified by from the system and destroy the set of semaphores and data structure associated with it. This can only be executed by a process that has an effective user ID equal to either that of superuser or to the value of either sem_perm.uid or sem_perm.cuid in the data structure associated with Upon
successful completion, semctl() returns a value based on as follows: GETVAL The value of GETNCNT The
value of GETZCNT The value of GETPID The value of All others return 0. If it fails, it returns -1 and sets errno to indicate the error. If semctl() fails, it sets errno to one of the following values: [EACCES] Operation
permission is denied to the calling process (see in [EFAULT] is SETVAL, GETALL, SETALL, IPC_SET, or
IPC_STAT, and is an invalid pointer. [EINVAL] is not a valid semaphore identifier. [EINVAL] is less than zero or
greater than or equal sem_nsems. [EINVAL] is not a valid command, or the command contains invalid parameters.
[EPERM] is equal to IPC_RMID or IPC_SET and the process does not have an effective user ID equal to either that of superuser or to the value of either sem_perm.uid or sem_perm.cuid in the data structure associated with [ERANGE] is SETVAL or SETALL and the value to which is to be set is greater than the system imposed maximum. The following call to semctl() initializes the set of 4 semaphores to the values 0, 1, 0, and 1
respectively. This example assumes the process has a valid representing a set of 4 semaphores as shown in the
manual entry. For an example of performing "P" and "V" operations on the semaphores below, refer to
union semun {
int val;
struct semid_ds ∗buf;
ushort ∗array;
} arg;
ushort semarray[4];
semarray[0] = 0;
semarray[1] = 1;
semarray[2] = 0;
semarray[3] = 1;
arg.array = &semarray[0];
semctl (mysemid, 0, SETALL, arg);
ipcrm(1), ipcs(1), semget(2), semop(2), stdipc(3C), glossary(9). semctl(): SVID2, SVID3, XPG2, XPG3, XPG4
semget − get set of semaphores #include <sys/sem.h> int semget(key_t key, int nsems, int semflg); semget() returns the semaphore identifier associated with A semaphore identifier and associated data structure and set
containing semaphores are created for if one of the following is true: is equal to IPC_PRIVATE. This call creates a new identifier, subject to available resources. The identifier is never returned by another call to semget() until
it has been released by a call to semctl(). The identifier should be used among the calling process and its descen-
dents; however, it is not a requirement. The resource can be accessed by any process having the proper permissions.
does not already have a semaphore identifier associated with it, and & IPC_CREAT) is ‘‘true’’. Specific behavior
can be requested by the following masks into IPC_CREAT: Create a semaphore identifier if one does not already
exist for IPC_EXCL: If IPC_CREAT is specified and already has a semaphore identifier associated with it, return an error. The low-order 9 bits of are the semaphore operation permissions which are defined in Upon
creation, the data structure associated with the new semaphore identifier is initialized as follows: In the operation-permission structure, sem_perm.cuid and sem_perm.uid are set equal to the effective-user-ID of the
calling process, while sem_perm.cgid and sem_perm.gid are set to the effective-group-ID of the calling process. The low-order 9 bits of sem_perm.mode are set equal to the low-order 9 bits of sem_nsems is set equal
to the value of sem_otime is set equal to 0 and sem_ctime is set equal to the current time. The following call to
semget() returns a semid associated with the key returned by ftok("myfile", ’A’). If a semid associated with
the key does not exist, a new semid, set of 4 semaphores, and associated data structure will be created. If a
semid for the key already exists, the semid is simply returned.
int semid;
mysemid = semget (ftok("myfile",’A’), 4, IPC_CREAT | 0600);
Upon successful completion, a non-negative integer, namely a semaphore identifier, is returned. Otherwise, a
value of −1 is returned and errno is set to indicate the error. semget() fails if one or more of the following is
true: is either less than or equal to zero or greater than the system-imposed limit. A semaphore identifier exists for but operation permission as specified by the low-order 9 bits of would not be granted. A semaphore
identifier exists for but the number of semaphores in the set associated with it is less than and is not equal to
zero. A semaphore identifier does not exist for and & IPC_CREAT) is ‘‘false’’. A semaphore identifier is to be
created, but the system-imposed limit on the maximum number of allowed semaphore identifiers system wide would
be exceeded. A semaphore identifier exists for but ((semflg& IPC_CREAT) && (semflg & IPC_EXCL)) is
‘‘true’’. ipcrm(1), ipcs(1), semctl(2), semop(2), stdipc(3C). semget(): SVID2, SVID3, XPG2, XPG3, XPG4 semop − semaphore operations
#include <sys/sem.h>
int semop(
int semid,
struct sembuf *sops,
size_t nsops
);
semop() is used to atomically perform an array of semaphore operations on the set of semaphores associated
with the semaphore identifier specified by is a pointer to the array of semaphore-operation structures. is the
number of such structures in the array. The contents of each structure includes the following members:
ushort sem_num;
/* semaphore number */
short sem_op
/* semaphore operation */
short sem_flg;
/* operation flags */
Each semaphore operation specified by is performed on the corresponding semaphore specified by and
Semaphore array operations are atomic in that none of the semaphore operations are performed until blocking conditions on all of the semaphores in the array have been removed. specifies one of three semaphore operations as follows: If is a negative integer, one of the following occurs: If (see in is greater than or equal to the
absolute value of the absolute value of is subtracted from Also, if SEM_UNDO) is ‘‘true’’, the absolute value of
is added to the calling process’s value (see and for the specified semaphore. If is less than the absolute value of and
IPC_NOWAIT) is ‘‘true’’, semop() returns immediately. If is less than the absolute value of and
IPC_NOWAIT) is ‘‘false’’, semop() increments the semncnt associated with the specified semaphore and suspend execution of the calling process until one of the following conditions occur: becomes greater than or
equal to the absolute value of When this occurs, the value of semncnt associated with the specified semaphore
is decremented, the absolute value of is subtracted from and, if SEM_UNDO) is ‘‘true’’, the absolute value of is
added to the calling process’s value for the specified semaphore. The for which the calling process is awaiting action is removed from the system (see When this occurs, errno is set equal to and a value of −1 is returned. The
calling process receives a signal that is to be caught. When this occurs, the value of semncnt associated with
the specified semaphore is decremented, and the calling process resumes execution in the manner prescribed
in If is a positive integer, the value of is added to and, if SEM_UNDO) is ‘‘true’’, the value of is subtracted from
the calling process’s value for the specified semaphore. If is zero, one of the following occurs: If is zero, semop()
proceeds to the next semaphore operation specified by or returns immediately if this is the last operation. If is
not equal to zero and IPC_NOWAIT) is ‘‘true’’, semop() returns immediately. If is not equal to zero and
IPC_NOWAIT) is ‘‘false’’, semop() increments the semzcnt associated with the specified semaphore and suspends execution of the calling process until one of the following occurs: becomes zero, at which time the value
of semzcnt associated with the specified semaphore is decremented. The for which the calling process is
awaiting action is removed from the system. When this occurs, errno is set equal to and a value of −1 is returned. The calling process receives a signal that is to be caught. When this occurs, the value of semzcnt associated with the specified semaphore is decremented, and the calling process resumes execution in the manner prescribed in The following call to semop() atomically performs a "P" or "get" operation on the second
semaphore in the semaphore set and a "V" or "release" operation on the third semaphore in the set. This example assumes the process has a valid which represents a set of 4 semaphores as shown on the manual page.
It also assumes that the of the semaphores in the set have been initialized as shown in the manual entry.
struct sembuf sops[4];
sops[0].sem_num = 1;
sops[0].sem_op = -1;
/* P (get) */
sops[0].sem_flg = 0;
sops[1].sem_num = 2;
sops[1].sem_op = 1;
/* V (release) */
sops[1].sem_flg = 0;
semop (mysemid, sops, 2);
If semop() returns due to the receipt of a signal, a value of −1 is returned to the calling process and errno is set
to If it returns due to the removal of a from the system, a value of −1 is returned and errno is set to Upon successful completion, a non-negative value is returned. Otherwise, a value of −1 is returned and errno is set to
indicate the error. semop() fails if one or more of the following is true for any of the semaphore operations
specified by is not a valid semaphore identifier. is less than zero or greater than or equal to the number of
semaphores in the set associated with is greater than the system-imposed maximum. Operation permission is
denied to the calling process (see The operation would result in suspension of the calling process but
IPC_NOWAIT) is ‘‘true’’. The limit on the number of individual processes requesting an SEM_UNDO would be
exceeded. The number of individual semaphores for which the calling process requests a SEM_UNDO would
exceed the limit. An operation would cause a to overflow the system-imposed limit. An operation would
cause a value to overflow the system-imposed limit. points to an illegal address. The reliable detection of this
error will be implementation dependent. Upon successful completion, the value of for each semaphore specified in the array pointed to by is set equal to the process of the calling process. The value of sem_otime in the
data structure associated with the semaphore identifier will be set to the current time. Check all references to
for appropriateness on systems that support can affect the behavior described on this page. ipcs(1), exec(2),
exit(2), fork(2), semctl(2), semget(2), stdipc(3C), signal(5). semop(): SVID2, SVID3, XPG2, XPG3, XPG4
Download