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?
The semop() function performs operations on semaphores in a semaphore set. These operations are supplied in a user-defined array of operations. Each semaphore operation specified by the sops array is performed on the semaphore set specified by semid.
Returned value. If successful, semop() returns 0. Also the semid parameter value for each semaphore that is operated upon is set to the process ID of the calling process. If unsuccessful, semop() returns -1 and sets errno to one of the following values: Error Code.
The semctl() function performs control operations in semaphore set semid as specified by the argument cmd. Depending on the value of argument cmd, argument semnum may be ignored or identify one specific semaphore number. The fourth argument is optional and depends upon the operation requested.
A semaphore set is a structure that stores a group of semaphores together, and possibly allows the process to commit a transaction on part or all of the semaphores in the set together. Here, a transaction means that we are guaranteed that either all operations are done successfully, or none is done at all.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With