Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

568 Interprocess Communication Chapter 15


Note that the optional argument is the actual union, not a pointer to the union.

Usually our application must define thesemununion. However, on FreeBSD 8.0, this is
defined for us in<sys/sem.h>.

Thecmdargument specifies one of the following ten commands to be performed on
the set specified bysemid.The five commands that refer to one particular semaphore
value usesemnumto specify one member of the set. The value ofsemnumis between 0
andnsems− 1 ,inclusive.
IPC_STAT Fetch thesemid_dsstructurefor this set, storing it in the structure
pointed to byarg.buf.
IPC_SET Set thesem_perm.uid,sem_perm.gid,andsem_perm.modefields
from the structurepointed to byarg.bufin thesemid_dsstructure
associated with this set. This command can be executed only by a
process whose effective user ID equals sem_perm.cuid or
sem_perm.uidor by a process with superuser privileges.
IPC_RMID Remove the semaphoreset from the system. This removal is
immediate. Any other process still using the semaphorewill get an
error ofEIDRMon its next attempted operation on the semaphore.
This command can be executed only by a process whose effective user
ID equalssem_perm.cuidorsem_perm.uidor by a process with
superuser privileges.
GETVAL Return the value ofsemvalfor the membersemnum.
SETVAL Set the value ofsemval for the member semnum.The value is
specified byarg.val.
GETPID Return the value ofsempidfor the membersemnum.
GETNCNT Return the value ofsemncntfor the membersemnum.
GETZCNT Return the value ofsemzcntfor the membersemnum.
GETALL Fetch all the semaphorevalues in the set. These values arestored in
the array pointed to byarg.array.
SETALL Set all the semaphorevalues in the set to the values pointed to by
arg.array.
For all theGETcommands other thanGETALL,the function returns the corresponding
value. For the remaining commands, the return value is 0 if the call succeeds. On error,
thesemctlfunction setserrnoand returns−1.
The functionsemopatomically performs an array of operations on a semaphoreset.
#include <sys/sem.h>
int semop(intsemid,struct sembuf semoparray[], size_tnops);
Returns: 0 if OK,−1 on error
Thesemoparrayargument is a pointer to an array of semaphoreoperations, represented
bysembufstructures:
Free download pdf