#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include "assa/Assure.h"
Include dependency graph for Semaphore.h:
This graph shows which files directly or indirectly include this file:
Go to the source code of this file.
Namespaces | |
namespace | ASSA |
Classes | |
class | ASSA::Semaphore |
The very typical situation for the test programs is when main 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 book "dvanced programming in the UNIX Environment" (Prentice Hall, 1992, ISBN 0-201-56317-7).
Definition in file Semaphore.h.