Semaphore
- 1965 E.W.Dijkstra
PV Operations
- Atomic operations: very quick and uninterruptable(原子语句,不能被打断)
- Also known as down/up or wait/signal operations(P/V操作)
Semaphore can be operated by PV operation only(绝不可以直接读写访问,只能PV语句中操作)
- The only exception is the initialization.
- Even read the value is prohibited
What ′ <script type="math/tex" id="MathJax-Element-1">'</script>s is semaphore?
- An integer variable(整数部分)
- In sleep and wake up, with a process waiting queue (usu. first in first out)(等待队列)
Variable range has different definitions:
- Whole integer,即取便所有整数 集(用于sleep and wake up)
• Positive(正数): the number of resources available(可用资源个数)
•Zero: no resource available, no waiting process(这个资源没有可用的,也没有进程使用)
•Negative(负数): the number of processes waiting to use the resources(没有资源可用,且有进程等待使用该资源,信号量的绝对值就是等待使用该资源的进程数) - Zero and positive integer ,只用0和正数,不记录用没有进程在等待(用于busy waiting)
• Positive: the number of resources available
• Zero: no resource available
PV in Sleep and Wakeup
- (新的进程得不到满足,就去睡觉,正在占用资源的进行负责唤醒。)
PV in Busy Waiting
- (新的进程得不到满足,也不睡觉,不停地测试访问,直到资源被解除占用。相关进程永远是runnable,一个在running,其他的ready。)