sem(FP) 6 January 1993 sem(FP) Name sem - controls structures for UNIX System V semaphores Syntax #include <sys/sem.h> #include <sys/ipc.h> Description A semaphore identifier (semid) is a unique positive integer created by a semget(S) system call. Each semid has a set of semaphores and a data structure associated with it. The data structure is referred to as semidds and contains the following members: struct ipc_perm sem_perm; /* operation permission struct */ struct sem *sem_base; /* ptr to first semaphore in set */ ushort sem_nsems; /* number of sems in set; see below */ struct lockb sem_cilock; /* lock for MPX */ time_t sem_otime; /* time of last semop (S) operation */ time_t sem_ctime; /* time of last semctl (S) change */ /* Times measured in secs since */ /* 00:00:00 GMT, Jan. 1, 1970 */ semperm is an ipcperm structure that specifies the semaphore operation permission. This structure includes the following members: ushort uid; /* user id */ ushort gid; /* group id */ ushort cuid; /* creator user id */ ushort cgid; /* creator group id */ ushort mode; /* access permission (see below) */ ushort seq; /* slot usage sequence number */ key_t key; /* key */ The value of semnsems is equal to the number of semaphores in the set. Each semaphore in the set is referenced by a positive integer referred to as a semnum. semnum values run sequentially from 0 to the value of semnsems minus 1. semotime is the time of the last semop(S) operation, and semctime is the time of the last semctl(S) operation that changed a member of the above structure. A semaphore is a data structure that contains the following members: ushort semval; /* semaphore value */ short sempid; /* pid of last operation */ ushort semncnt; /* # awaiting semval > cval */ ushort semzcnt; /* # awaiting semval = 0 */ semval is a non-negative unsigned short. sempid is equal to the process ID of the last process that performed a semaphore operation on this sema- phore. semncnt is a count of the number of processes that are currently suspended waiting for this semaphore's semval to become greater than its current value. semzcnt is a count of the number of processes that are currently suspended waiting for this semaphore's semval to become zero. Upon creation, the data structure associated with the new semaphore iden- tifier is initialized as follows: + semperm.cuid, semperm.uid, semper.cgid, and semperm.gid are set equal to the effective user ID and effective group ID of the calling process. + The low-order 9 bits of semperm.mode are set equal to the low-order 9 bits of the semflg argument. + semnsems is set equal to the value of nsems. + semotime is set equal to 0 and semctime is set equal to the current time. The program that creates the semaphore array must issue the semctl(S) call with the SETVAL command to establish the initial value of the sem structure. In addition to the structures described here, each process using the semaphore array must create a local data structure, identified by the sops argument to the semop call. See the semop(S) reference page for a description of the members of that structure. Semaphore operation permissions Access permissions for the semaphore are set according to values given to the semflg argument to the semget(S) system call, defined as: 00400 Read by user (SEM_R) 00200 Alter by user (SEM_W) 00040 Read by group 00020 Read by others 00004 Alter by group 00002 Alter by others Read and alter permissions for a semid are granted to a process if one or more of the following is true: + The effective user ID of the process is super user. + The effective user ID of the process matches semperm.uid or semperm.cuid in the data structure associated with semid and the appropriate ``user'' portion (0600) bit of semperm.mode is set. + The effective group ID of the process matches semperm.gid or semperm.cgid in the data structure associated with semid and the appropriate bit of the ``user'' portion (060) of semperm.mode is set. + The appropriate bit of the``other'' portion (06) of semperm.mode is set. Otherwise, the corresponding permissions are denied. Note that permission 7 is equivalent to permission 6 (thus, 0660 and 0770 are equivalent). See also ipcrm(ADM), semctl(S), semget(S), semop(S)