Name

semget - get a semaphore set identifier

Library

libc.lib

Synopsis

  #include <sys/types.h>
  #include <sys/ipc.h>
  #include <sys/sem.h>
  int semget (key_t key, int nsems, int flag);

Return values

The semget call returns the id of a semaphore set if successful; otherwise, -1 is returned and errno is set to indicate the error.

Detailed description

Based on the values of key and flag, semget returns the identifier of a newly created or previously existing set of semaphores. The key is analogous to a filename: it provides a handle that names an IPC object. There are three ways to specify a key:

The mode of a newly created IPC object is determined by OR ’ing the following constants into the flag argument:

SEM_R Read access for user.
SEM_A Alter access for user.
( SEM_R>>3)
  Read access for group.
( SEM_A>>3)
  Alter access for group.
( SEM_R>>6)
  Read access for other.
( SEM_A>>6)
  Alter access for other.

If a new set of semaphores is being created, nsems is used to indicate the number of semaphores the set should contain. Otherwise, nsems may be specified as 0.


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;
    int perm;
    /*
     * Create 2 semaphores in a set, with access only to
     * the owner
     */
    perm = SEM_R | SEM_A;
    if((sem_set_id = semget(SEM_SET_KEY, NO_OF_SEMAPHORES, IPC_CREAT | perm))
        == -1) {
       printf("Semaphore creation failed with errno %d\n", errno);
       return -1;
    }
    return 0;
}

         


Errors

The semget call will fail with:
[EACCES]
  Access permission failure.
[EEXIST]
  IPC_CREAT and IPC_EXCL were specified, and a semaphore set corresponding to key already exists.
[EINVAL]
  The number of semaphores requested exceeds the system imposed maximum per set.
[ENOSPC]
  A semaphore set has to be created but the system wide maximum number of semaphores would be exceeded.
[ENOSPC]
  The kernel could not allocate a struct semid_ds.
[ENOENT]
  No semaphore set was found corresponding to key, and IPC_CREAT was not specified.

See also

semctl, semop, ftok

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top