Rumah > Soal Jawab > teks badan
1、创建semaphore信号量集(仅创建一个信号量),初始值为0
2、进程a释放掉n个信号量,进程b获取了m个信号量(n >m)均使用undo标志, 此时在进程a中获取信号量值,为n - m
3,杀掉进程a, 再次在进程b中获取信号量值,为0
问题是为什么此时的信号量值不为-m,还是说系统当前的信号量值只能为非负,才会导致这种结果?
巴扎黑2017-04-17 11:37:17
Semaphore值最小为0,不可能为负数。
根据《操作系统设计与实现》(Operating Systems - Design and Implementation by Tanenbaum etc.)第三版 2.2.5 Semaphores:
The down operation on a semaphore checks to see if the value is greater than 0...If the value is 0, the process is put to sleep without completing the down for the moment...after an up on a semaphore with process sleeping on it, the semaphore will still be 0, but there will be one fewer process sleeping on it.