semaphore信号量多进程及undo标志的系统回收资源问题

新手上路,请多包涵

1、创建semaphore信号量集(仅创建一个信号量),初始值为0
2、进程a释放掉n个信号量,进程b获取了m个信号量(n >m)均使用undo标志, 此时在进程a中获取信号量值,为n - m
3,杀掉进程a, 再次在进程b中获取信号量值,为0

问题是为什么此时的信号量值不为-m,还是说系统当前的信号量值只能为非负,才会导致这种结果?

阅读 4.2k
1 个回答

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.

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题