gpt4 book ai didi

semaphore - 了解信号量

转载 作者:行者123 更新时间:2023-12-01 10:06:06 28 4
gpt4 key购买 nike

我正在阅读“操作系统概念”中有关信号量的内容(对于那些了解它的人),并且我认为我完全理解了信号量,直到我阅读了这段话:

The critical aspect of semaphores is that they are executed atomically. We must guarantee that no two processes can execute wait and signal operations on the same semaphore at the same time.

还有:

If the hardware does not provide any special atomic instructions, we can employ any of the software solutions for the critical section problem, where critical sections consist of the wait and signal procedures.

这段话是指面对SignalWait操作必须是原子的。我认为信号量的全部目的是在任何给定时间只让一个进程进入关键部分 - 如果我必须使用另一种算法(如面包店算法),为什么我还需要信号量?

我意识到我的问题可能令人困惑。如果是,那只是因为主题对我来说还很模糊,所以即使问一个问题也有点困难。

希望阅读任何说明...

最佳答案

我认为您在区分信号量和互斥量时遇到了麻烦。二进制信号量可以以与互斥锁相同的方式实现,但它们实际上用于不同的目的。信号量保护资源,而互斥锁严格保护代码块。区别往往是微妙的。

使用信号量,您会遇到各种变化,例如计算信号量,因此只有一个进程可以访问资源的想法并不总是正确的。您可能希望阻止对一个进程或线程的写入,但允许从多个(读取器/写入器锁)读取。

我建议您查看有关该主题的维基百科文章。其实还不错。
http://en.wikipedia.org/wiki/Semaphore_(programming)

关于semaphore - 了解信号量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10437428/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com