【发布时间】:2021-11-11 02:37:19
【问题描述】:
我刚刚读到二进制信号量保证互斥并且信号量可以被抢占。
二进制信号量的等待/关闭代码(取自https://www.javatpoint.com/os-binary-semaphore-or-mutex)
Down (semaphore S)
{
if (s.value == 1) // if a slot is available in the
//critical section then let the process enter in the queue.
{
S.value = 0; // initialize the value to 0 so that no other process can read it as 1.
}
else
{
put the process (PCB) in S.L; //if no slot is available
//then let the process wait in the blocked queue.
sleep();
}
}
如果进程 P1 正在运行,它检查 down 函数中的条件并将 s.value 的值更改为 0,但在此之前它被抢占并保存其 PCB 并启动新进程 P2运行它会检查条件并将 s.value 更改为 0 并且其临界区开始。另一方面,如果 P2 进入 I/O 等待状态,则 P1 可能会开始执行,因此两者都可能进入它们的临界区。那么互斥如何保存??
【问题讨论】:
-
信号量对象不会发生这种情况。如果可能发生,则该对象不是信号量。
标签: operating-system synchronization semaphore binary-semaphore