Semop: When decreasing a set of semaphores are all decremented at once or does it block on first failure?

c, semaphore, unix

Solution

No updates happen until all updates can proceed as a unit.

The POSIX specification could be clearer about this point, although it does say that `semop` is atomic.

On Linux, `semop(3)` in glibc is a simple wrapper around `semop(2)`. The `semop(2)` manpage in turn says

The set of operations contained in `sops` is performed in array order, and atomically, that is, the operations are performed either as a complete unit, or not at all.

The HP-UX `semop(2)` manpage is even clearer:

Semaphore array operations are atomic in that none of the semaphore operations are performed until blocking conditions on all of the semaphores in the array have been removed.

Problem

So if I have a semaphore set `semid` with `num_of_sems` semaphores and a sembuf `*deleter_searchers_down` ``` struct sembuf *deleter_searchers_down = malloc(sizeof (*deleter_searchers_down) * num_of_sems); for (i = 0; i < num_of_sems; ++i) { (deleter_searchers_down + i)->sem_op = -1; (deleter_searchers_down + i)->sem_num = i; (deleter_searchers_down + i)->sem_flg = SEM_UNDO; } semop(semid, deleter_searchers_down, num_of_sems); ``` The call to semop will attempt to lower all semaphores in the set at once or will it block once it attempts to lower the first semaphore that is 0 and continue after some other process ups that particular semaphore?

Original source