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

若如初见. 提交于 2020-01-02 05:04:42

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/10206501/semop-when-decreasing-a-set-of-semaphores-are-all-decremented-at-once-or-does-i

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!