libassa  3.5.1
Classes | Namespaces
Semaphore.h File Reference

Semaphore class provides a simpler and easier interface to System V semaphore system calls. More...

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include "assa/Assure.h"

Go to the source code of this file.

Classes

class  ASSA::Semaphore
 

Namespaces

 ASSA
 

Detailed Description

Semaphore class provides a simpler and easier interface to System V semaphore system calls.

The very typical situation for the test programs is when the parent process forks a child and then sends a signal to it to start some simulation process. Depending on the system load and other external factors, it can take a variable amount of time for the child process to initialize its internal structures and install the appropriate signal handler. Semaphore is used sychronize the parent and child processes, by letting them complete their initialization sections.

Implementation Details:

We create and use a 3-member set for the requested semaphore. The first member, [0], is the actual semaphore value, and the second member, [1], is a counter used to know when all processes have finished with the semaphore. The counter is initialized to a large number, decremented on every Semaphore::create () or Semaphore::open (), and incremented on every Semaphore::close ().

This way we can use "adjust" feature provided by System V so that any process that exit's without calling Semaphore::close () is accounted for. It doesn't help us if the last process does this (as we have no way of getting control to remove the semaphore) but it will work if any process other than the last does an exit (intentional or unintentional).

The third member, [2], of the semaphore set is used as a lock variable to avoid any race conditions in the Semaphore::create () and Semaphore::close () functions.

Note:

This class is a port to C++, inspired by: W. Richard Stevens from his wonderful book, "Advanced programming in the UNIX Environment" (Prentice Hall, 1992, ISBN 0-201-56317-7).

Definition in file Semaphore.h.