1. 程式人生 > >[development][semaphore] 信號量/信號燈/信號標/旗語

[development][semaphore] 信號量/信號燈/信號標/旗語

是否 moni href 釋放 available 管程 semget clas emctl

前言:

接續前節

[development][C] 條件變量(condition variables)的應用場景是什麽

之前討論了條件變量的問題, 已經知道在邏輯上, 條件變量(管程)(moniter) 與信號量 邏輯等價. 可以相互實現.

又知: 二元信號量可以用來實現互斥量.

那麽是不是說進程間交互, 僅使用信號量便已經足夠了能?

另一個問題: 在API層面上, 我們知道互斥量的使用原則是: 誰加鎖誰釋放. 但是二元信號量的用法卻可以是線程A做V操作, 線程B做P操作.

這是否只是存在於API層面上的區別呢?

不知道以上這些內容, 是否存在一個專門的知識分支?

一本書, 可以深入這個問題, 還沒有來的及讀: <<The little book of Semaphores>> http://greenteapress.com/wp/semaphores/

其他參考內容:

https://zh.wikipedia.org/wiki/%E4%BF%A1%E8%99%9F%E6%A8%99

https://en.wikipedia.org/wiki/Semaphore_(programming)

https://wizardforcel.gitbooks.io/think-os/content/ch11.html

API:

LInux C下有兩套信號量的API

Posix:

http://man7.org/linux/man-pages/man7/sem_overview.7.html

System V:

http://man7.org/linux/man-pages/man2/semget.2.html

http://man7.org/linux/man-pages/man2/semop.2.html

http://man7.org/linux/man-pages/man2/semctl.2.html

關於二者的選用問題, Posix裏邊如是說:

       System V semaphores (semget(2), semop(2), etc.) are an older
       semaphore API.  POSIX semaphores provide a simpler, and better
       designed interface than System V semaphores; on the other hand POSIX
       semaphores are less widely available (especially on older systems)
       than System V semaphores.

就是說, 請選用POSIX API !

[development][semaphore] 信號量/信號燈/信號標/旗語