System V semaphores

Semaphores in System V are not just a single counter as you might think, but rather a set of counters. This implies that a semaphore set can contain single or multiple counters (0 to n) with an identical semaphore ID. Each counter in the set can protect a shared resource, and a single semaphore set can protect multiple resources. The system call that helps create this kind of semaphore is as follows:

int semget(key_t key, int nsems, int semflg)
  • key is used to identify the semaphore. If the key value is IPC_PRIVATE, a new set of semaphores is created.
  • nsems indicates the semaphore set with the number of counters needed in the set
  • semflg dictates how the semaphore should be created. It can contain two values:
    • IPC_CREATE: If the key does not exist, it creates a new semaphore
    • IPC_EXCL: If the key exists, it throws an error and fails

On success, the call returns the semaphore set identifier (a positive value).

A semaphore thus created contains uninitialized values and requires the initialization to be carried out using the semctl() function. After initialization, the semaphore set can be used by the processes:

int semop(int semid, struct sembuf *sops, unsigned nsops);

The Semop() function lets the process initiate operations on the semaphore set. This function offers a facility unique to the SysV semaphore implementation called undoable operations through a special flag called SEM_UNDO. When this flag is set, the kernel allows a semaphore to be restored to a consistent state if a process aborts before completing the relevant shared data access operation. For instance, consider a case where one of the processes locks the semaphore and begins its access operations on shared data; during this time if the process aborts before completion of shared data access, the semaphore will be left in an inconsistent state, making it unavailable for other contending processes. However, if the process had acquired a lock on the semaphore by setting the SEM_UNDO flag with semop(), its termination would allow the kernel to revert the semaphore to a consistent state (unlocked state) making it available for other contending processes in wait.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.124.244