内容简介
#include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> int semctl(int semid, int semnum, int cmd, ...) |
描述
semctl() performs the control operation specified by%uA0cmd%uA0on the semaphore set identified by%uA0semid, or on the%uA0semnum-th semaphore of that set. (The semaphores in a set are numbered starting at 0.)This function has three or four arguments, depending on%uA0cmd. When there are four, the fourth has the type%uA0union semun. The%uA0calling program%uA0must define this union as follows:
union semun { int val /* Value for SETVAL */ struct semid_ds *buf /* Buffer for IPC_STAT, IPC_SET */ unsigned short *array /* Array for GETALL, SETALL */ struct seminfo *__buf /* Buffer for IPC_INFO (Linux specific) */ } |
The%uA0semid_dsꃚta structure is defined in <sys/sem.h> as follows:
struct semid_ds { struct ipc_perm sem_perm /* Ownership and permissions time_t sem_otime /* Last semop time */ time_t sem_ctime /* Last change time */ unsigned short sem_nsems /* No. of semaphores in set */ } |
The%uA0ipc_perm%uA0structure is defined in <sys/ipc.h> as follows (the highlighted fields are settable using%uA0IPC_SET):
struct ipc_perm { key_t key /* Key supplied to semget() */ uid_t uid /* Effective UID of owner */ gid_t gid /* Effective GID of owner */ uid_t cuid /* Effective UID of creator */ gid_t cgid /* Effective GID of creator */ unsigned short mode /* Permissions */ unsigned short seq /* Sequence number */ } |
Valid values for%uA0cmd%uA0are:
标签 | 描述 | |
---|---|---|
IPC_STAT | Copy information from the kernel data structure associated withsemid%uA0into the%uA0semid_ds%uA0structure pointed to by%uA0arg.buf. The argument%uA0semnum%uA0is ignored. The calling process must have read permission on the semaphore set. | |
IPC_SET | Write the values of some members of the%uA0semid_ds%uA0structure pointed to by%uA0arg.buf%uA0to the kernel data structure associated with this semaphore set, updating also its%uA0sem_ctime%uA0member. The following members of the structure are updated:sem_perm.uid,%uA0sem_perm.gid, and (the least significant 9 bits of)sem_perm.mode. The effective UID of the calling process must match the owner (sem_perm.uid) or creator (sem_perm.cuid) of the semaphore set, or the caller must be privileged. The argument%uA0semnum%uA0is ignored. | |
IPC_RMID | Immediately remove the semaphore set, awakening all processes blocked in%uA0semop() calls on the set (with an error return and%uA0errno%uA0set to%uA0EIDRM). The effective user ID of the calling process must match the creator or owner of the semaphore set, or the caller must be privileged. The argumentsemnum%uA0is ignored. | |
IPC_INFO%uA0(Linux specific) | ||
%uA0 |
Returns information about system-wide semaphore limits and parameters in the structure pointed to by%uA0arg.__buf. This structure is of type%uA0seminfo, defined in%uA0<sys/sem.h>%uA0if the _GNU_SOURCE feature test macro is defined:
|
|
SEM_INFO%uA0(Linux specific) | ||
%uA0 | Returns a%uA0seminfo%uA0structure containing the same information as for%uA0IPC_INFO, except that the following fields are returned with information about system resources consumed by semaphores: the%uA0semusz%uA0field returns the number of semaphore sets that currently exist on the system and the%uA0semaem%uA0field returns the total number of semaphores in all semaphore sets on the system. | |
SEM_STAT%uA0(Linux specific) | ||
%uA0 | Returns a%uA0semid_ds%uA0structure as for%uA0IPC_STAT. However, thesemid%uA0argument is not a semaphore identifier, but instead an index into the kernel’s internal array that maintains information about all semaphore sets on the system. | |
GETALL | Return%uA0semval%uA0(i.e., the current value) for all semaphores of the set into%uA0arg.array.%uA0The argument%uA0semnum%uA0is ignored. The calling process must have read permission on the semaphore set. | |
GETNCNT | The system call returns the value of%uA0semncnt%uA0(i.e., the number of processes waiting for the value of this semaphore to increase) for the%uA0semnum-th semaphore of the set (i.e. the number of processes waiting for an increase of%uA0semval%uA0for thesemnum-th semaphore of the set). The calling process must have read permission on the semaphore set. | |
GETPID | The system call returns the value of%uA0sempid%uA0for the%uA0semnum-th semaphore of the set (i.e. the PID of the process that executed the last%uA0semop() call for the%uA0semnum-th semaphore of the set). The calling process must have read permission on the semaphore set. | |
GETVAL | The system call returns the value of%uA0semval%uA0for the%uA0semnum-th semaphore of the set. The calling process must have read permission on the semaphore set. | |
GETZCNT | The system call returns the value of%uA0semzcnt%uA0(i.e., the number of processes waiting for the value of this semaphore to become zero) for the%uA0semnum-th semaphore of the set (i.e. the number of processes waiting for%uA0semval%uA0of the%uA0semnum-th semaphore of the set to become 0). The calling process must have read permission on the semaphore set. | |
SETALL | Set%uA0semval%uA0for all semaphores of the set using%uA0arg.array,updating also the%uA0sem_ctime%uA0member of the%uA0semid_ds%uA0structure associated with the set. Undo entries (see%uA0semop(2)) are cleared for altered semaphores in all processes. If the changes to semaphore values would permit blocked%uA0semop() calls in other processes to proceed, then those processes are woken up. The argument%uA0semnum%uA0is ignored. The calling process must have alter (write) permission on the semaphore set. | |
SETVAL | Set the value of%uA0semval%uA0to%uA0arg.val%uA0for the%uA0semnum-th semaphore of the set, updating also the%uA0sem_ctime%uA0member of the%uA0semid_ds%uA0structure associated with the set. Undo entries are cleared for altered semaphores in all processes. If the changes to semaphore values would permit blocked%uA0semop() calls in other processes to proceed, then those processes are woken up. The calling process must have alter permission on the semaphore set. |
返回值
On failure%uA0semctl() returns -1 with%uA0errno%uA0indicating the error.Otherwise the system call returns a nonnegative value depending on%uA0cmd%uA0as follows:
标签 | 描述 |
---|---|
GETNCNT | the value of%uA0semncnt. |
GETPID | the value of%uA0sempid. |
GETVAL | the value of%uA0semval. |
GETZCNT | the value of%uA0semzcnt. |
IPC_INFO | the index of the highest used entry in the kernel’s internal array recording information about all semaphore sets. (This information can be used with repeated%uA0SEM_STAT%uA0operations to obtain information about all semaphore sets on the system.) |
SEM_INFO | As for%uA0IPC_INFO. |
SEM_STAT | the identifier of the semaphore set whose index was given insemid. |
错误
On failure,%uA0errno%uA0will be set to one of the following:标签 | 描述 |
---|---|
EACCES | The argument%uA0cmd%uA0has one of the values%uA0GETALL,%uA0GETPID,GETVAL,%uA0GETNCNT,%uA0GETZCNT,%uA0IPC_STAT,%uA0SEM_STAT,SETALL, or%uA0SETVAL%uA0and the calling process does not have the required permissions on the semaphore set and does not have the%uA0CAP_IPC_OWNERꃊpability. |
EFAULT | The address pointed to by%uA0arg.buf%uA0or%uA0arg.array%uA0isn’t accessible. |
EIDRM | The semaphore set was removed. |
EINVAL | Invalid value for%uA0cmd%uA0or%uA0semid. Or: for a%uA0SEM_STAT%uA0operation, the index value specified in%uA0semid%uA0referred to an array slot that is currently unused. |
EPERM | The argument%uA0cmd%uA0has the value%uA0IPC_SET%uA0or%uA0IPC_RMID%uA0but the effective user ID of the calling process is not the creator (as found in%uA0sem_perm.cuid) or the owner (as found insem_perm.uid) of the semaphore set, and the process does not have the%uA0CAP_SYS_ADMINꃊpability. |
ERANGE | The argument%uA0cmd%uA0has the value%uA0SETALL%uA0or%uA0SETVAL%uA0and the value to which%uA0semval%uA0is to be set (for some semaphore of the set) is less than 0 or greater than the implementation limitSEMVMX. |
注意
The%uA0IPC_INFO,%uA0SEM_STAT%uA0and%uA0SEM_INFO%uA0operations are used by the%uA0ipcs(8) program to provide information on allocated resources. In the future these may modified or moved to a /proc file system interface.Various fields in a%uA0struct semid_ds%uA0were shorts under Linux 2.2 and have become longs under Linux 2.4. To take advantage of this, a recompilation under glibc-2.1.91 or later should suffice. (The kernel distinguishes old and new calls by an IPC_64 flag in%uA0cmd.)
In some earlier versions of glibc, the%uA0semun%uA0union was defined in <sys/sem.h>, but POSIX.1-2001 requires that the caller define this union. On versions of glibc where this union is%uA0notꃞfined, the macro%uA0_SEM_SEMUN_UNDEFINED%uA0is defined in <sys/sem.h>.
The following system limit on semaphore sets affects a%uA0semctl() call:
标签 | 描述 |
---|---|
SEMVMX | Maximum value for%uA0semval: implementation dependent (32767). |
Under Linux,%uA0semctl() is not a system call, but is implemented via the system call%uA0ipc(2).
遵循于
SVr4, POSIX.1-2001.另请参阅
-
%uA0