oreilly.comSafari Books Online.Conferences.


Semaphores in Linux
Pages: 1, 2, 3, 4, 5, 6

Once a semaphore is created, System V semaphores require that those semaphores that were created in a set be initialized. Creation and initialization of semaphore is not an atomic operation, and initialization needs to be done separately by the user. The function that does this is:

 int semctl(int  semid, int semnum, int cmd, ...);
The semaphore identifier.
The n'th semaphore in the set identified by semid.
This defines the type of operation performed on the semaphore. For initialization purposes, the flag used is SETVAL . For other values please refer to the man pages.

Depending on what the chosen "cmd" requires, a fourth argument could be also passed--type union semun--but this is an optional argument. To use this structure, the user must explicitly define it as follows:

union semun {
 int  val;
 struct  semid_ds *buf;
 ushort  *array;
} arg;

The following code snippet sets the first semaphore of the set identified by the semaphore semid to value 1.

 semun init;
 init.val = 1;
   int i = semctl(semid, 1, SETVAL, init);

Once a semaphore is created and initialized, the user is ready to perform operations on this set of semaphores. Again, the interface used for performing operations, like locking or unlocking of semaphores, is not straight-forward. It requires a certain amount of instructions to be written before calling the following function, which will carry out the desired operation on a semaphore.

int semop(int  semid, struct sembuf *sops, size_t nsops);
The semaphore identifier.
Is a pointer to a user-defined array of semaphore structure. However, the documentation suggests that this structure shouldn't be extended. The structure is:
struct  sembuf {
   ushort sem_num; /* identifies which semaphore in the set */
   short sem_op; /* could be positive,negative or zero */
   short sem_flg; /*coud be IPC_NOWAIT ,SEM_UNDO*/
Determines how many sembuf you are passing. The nsops argument is provided in case the operation needs to be performed on bunch of semaphores at one time.

NOTE: If the SEM_UNDO flag is specified, the kernel resets the value of the semaphore by reversing all effects of previous operations when the program terminates.

The sem_op member of struct sembuf is an important variable that locks and unlocks the semaphore. It can take the following values, which determines the kind of operation to be performed on the semaphore.

If sem_op is zero:
Then it waits for the semaphore value to drop to zero.
If sem_op is positive value
Then it increases the semaphore value by absolute sem_op value.
If sem_op is negative value
Then it decreases the semaphore value by absolute sem_op value.

You can see from the above sem_op values that positive values correspond to releasing a semaphore and negative values correspond to a locking semaphore. NOTE: The value of the semaphore never goes below zero.

The most important operation, after creating, initializing, and operating on semaphores, is the cleaning up the semaphores and their memories. This should happen in cleanup handlers or in destructors, while exiting out of code normally, or while exiting the application abnormally. If this is not done, then the application would, at some point, not find enough semaphores to work with because they are defined system-wide in System V-based implementations.

The following snippet cleans up a semaphore set identified by semid

   int ret =  semctl(semid,0,IPC_RMID); //removing the 0th semaphore in the set

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow

Linux Online Certification

Linux/Unix System Administration Certificate Series
Linux/Unix System Administration Certificate Series — This course series targets both beginning and intermediate Linux/Unix users who want to acquire advanced system administration skills, and to back those skills up with a Certificate from the University of Illinois Office of Continuing Education.

Enroll today!

Linux Resources
  • Linux Online
  • The Linux FAQ
  • Linux Kernel Archives
  • Kernel Traffic

  • Sponsored by: