next up previous contents
Next: SYSTEM CALL: semop() Up: 6.4.3 Semaphores Previous: Kernel sem structure

SYSTEM CALL: semget()

In order to create a new semaphore set, or access an existing set, the semget() system call is used.

  SYSTEM CALL: semget();                                                          

  PROTOTYPE: int semget ( key_t key, int nsems, int semflg );                                             
    RETURNS: semaphore set IPC identifier on success
             -1 on error: errno = EACCESS (permission denied)
                                  EEXIST (set exists, cannot create (IPC_EXCL))
                                  EIDRM (set is marked for deletion)
                                  ENOENT (set does not exist, no IPC_CREAT was used)
                                  ENOMEM (Not enough memory to create new set)
                                  ENOSPC (Maximum set limit exceeded)

The first argument to semget() is the key value (in our case returned by a call to ftok()). This key value is then compared to existing key values that exist within the kernel for other semaphore sets. At that point, the open or access operation is dependent upon the contents of the semflg argument.


Create the semaphore set if it doesn't already exist in the kernel.


When used with IPC_CREAT, fail if semaphore set already exists.

If IPC_CREAT is used alone, semget() either returns the semaphore set identifier for a newly created set, or returns the identifier for a set which exists with the same key value. If IPC_EXCL is used along with IPC_CREAT, then either a new set is created, or if the set exists, the call fails with -1. IPC_EXCL is useless by itself, but when combined with IPC_CREAT, it can be used as a facility to guarantee that no existing semaphore set is opened for access.

As with the other forms of System V IPC, an optional octal mode may be OR'd into the mask to form the permissions on the semaphore set.

The nsems argument specifies the number of semaphores that should be created in a new set. This represents the number of printers in our fictional print room described earlier. The maximum number of semaphores in a set is defined in ``linux/sem.h'' as:

        #define SEMMSL  32      /* <=512 max num of semaphores per id */

Note that the nsems argument is ignored if you are explicitly opening an existing set.

Let's create a wrapper function for opening or creating semaphore sets:

int open_semaphore_set( key_t keyval, int numsems )
        int     sid;

        if ( ! numsems )
        if((sid = semget( mykey, numsems, IPC_CREAT | 0660 )) == -1)

Note the use of the explicit permissions of 0660. This small function either returns a semaphore set identifier (int), or -1 on error. The key value must be passed to it, as well as the number of semaphores to allocate space for if creating a new set. In the example presented at the end of this section, notice the use of the IPC_EXCL flag to determine whether or not the semaphore set exists or not.

next up previous contents
Next: SYSTEM CALL: semop() Up: 6.4.3 Semaphores Previous: Kernel sem structure

Converted on:
Fri Mar 29 14:43:04 EST 1996