Name
semctl - semaphore control operations
Library
libc.lib
Synopsis
|
int
semctl (int semid, int semnum, int cmd, ...);
|
Return values
On success, when
cmd
is one of
GETVAL, GETPID, GETNCNT
or
GETZCNT,
semctl
returns the corresponding value; otherwise, 0 is returned.
On failure, -1 is returned, and
errno
is set to indicate the error.
Detailed description
The
semctl
system call
performs the operation indicated by
cmd
on the semaphore set indicated by
semid.
A fourth argument, a
union semun arg,
is required for certain values of
cmd.
For the commands that use the
arg
argument,
union semun
is defined as follows:
union semun {
int val; /* value for SETVAL */
struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
u_short *array; /* array for GETALL & SETALL */
};
Commands are performed as follows:
IPC_STAT
|
Fetch the semaphore set’s
struct semid_ds,
storing it in the memory pointed to by
arg.buf.
|
IPC_SET
|
Changes the
sem_perm.uid,
sem_perm.gid,
and
sem_perm.mode
members of the semaphore set’s
struct semid_ds
to match those of the struct pointed to by
arg.buf.
|
IPC_RMID
|
Immediately removes the semaphore set from the system.
The calling
process’s uid must equal the semaphore set’s
sem_perm.uid
or
sem_perm.cuid,
|
GETVAL
|
Return the value of semaphore number
semnum.
|
SETVAL
|
Set the value of semaphore number
semnum
to
arg.val.
Outstanding adjust on exit values for this semaphore in any process
are cleared.
|
GETPID
|
Return the pid of the last process to perform an operation on
semaphore number
semnum.
|
GETNCNT
|
Return the number of processes waiting for semaphore number
semnum ’s
value to become greater than its current value.
|
GETZCNT
|
Return the number of processes waiting for semaphore number
semnum ’s
value to become 0.
|
GETALL
|
Fetch the value of all of the semaphores in the set into the
array pointed to by
arg.array.
|
SETALL
|
Set the values of all of the semaphores in the set to the values
in the array pointed to by
arg.array.
Outstanding adjust on exit values for all semaphores in this set,
in any process are cleared.
|
The
struct semid_ds
is defined as follows:
struct semid_ds {
struct ipc_perm sem_perm; /* operation permission struct */
struct sem *sem_base; /* pointer to first semaphore in set */
u_short sem_nsems; /* number of sems in set */
time_t sem_otime; /* last operation time */
long sem_pad1; /* SVABI/386 says I need this here */
time_t sem_ctime; /* last change time */
/* Times measured in secs since */
/* 00:00:00 GMT, Jan. 1, 1970 */
long sem_pad2; /* SVABI/386 says I need this here */
long sem_pad3[4]; /* SVABI/386 says I need this here */
};
Examples
#include <sys/ipc.h>
#include <sys/sem.h>
#include <stdio.h>
#include <errno.h>
#define SEM_SET_KEY 1000
#define NO_OF_SEMAPHORES 2
int main(void)
{
int sem_set_id;
union semun sem_val;
/*
* Create 2 semaphores in a set, with access only to
* the owner
*/
if((sem_set_id = semget(SEM_SET_KEY, NO_OF_SEMAPHORES, IPC_CREAT | 0600))
== -1) {
printf("Semaphore creation failed with errno %d\n", errno);
return -1;
}
/*
* Initialize the first semaphore in our set to 1
*/
sem_val.array = NULL;
sem_val.buf = NULL;
sem_val.val = 1;
if(semctl(sem_set_id, 0, SETVAL, sem_val) == -1) {
printf("Could not initialize first semaphore (errno %d)\n", errno);
return -1;
}
/*
* Initialize the second semaphore in our set to 0
*/
sem_val.val = 0;
if(semctl(sem_set_id, 1, SETVAL, sem_val) == -1) {
printf("Could not initialize second semaphore (errno %d)\n", errno);
return -1;
}
/*
* Delete the semaphore set
*/
if(semctl(sem_set_id, NO_OF_SEMAPHORES, IPC_RMID) == -1) {
printf("Could not delete semaphore set (errno %d)\n", errno);
return -1;
}
return 0;
}
Errors
The
semctl
system call
will fail if:
[EINVAL]
|
|
No semaphore set corresponds to
semid.
|
[EINVAL]
|
|
The
semnum
argument
is not in the range of valid semaphores for given semaphore set.
|
[EPERM]
|
|
The calling process’s uid does not match the uid of
the semaphore set’s owner or creator.
|
[EACCES]
|
|
Permission denied due to mismatch between operation and mode of
semaphore set.
|
[ERANGE]
|
|
SETVAL
or
SETALL
attempted to set a semaphore outside the allowable range
[0 .. SEMVMX.]
|
See also
semget,
semop
Feedback
For additional information or queries on this page send feedback
© 2005-2007 Nokia
|
|