Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Process 关于信号量的逻辑,我说的对吗?_Process_Operating System_Semaphore - Fatal编程技术网

Process 关于信号量的逻辑,我说的对吗?

Process 关于信号量的逻辑,我说的对吗?,process,operating-system,semaphore,Process,Operating System,Semaphore,这就是我对计算信号量的理解 P1 enters critical state calls wait() wait() { semaphore--; if (semaphore<=0) block; else execute the c.s after execution in c.s calls signal(); } signal() { semaphore++; If(blocked process exists) allow first process that is waitin

这就是我对计算信号量的理解

P1 enters critical state 
calls wait()
wait()
{
semaphore--;
if (semaphore<=0)
block;
else
execute the c.s
after execution in c.s calls signal();
}
signal()
{
semaphore++;
If(blocked process exists)
allow first process that is waiting and P1 leaves c.s
}
P1进入临界状态
呼叫等待()
等等
{
信号量--;
如果(信号量这里有一些东西:

首先试着理解信号量是用来做什么的。像信号量这样的构造是用来帮助实现程序中的并发性的。问问你自己-你想实现什么?同步?互斥?两者都有

根据这个问题的答案,您使用信号量的方式将完全改变。理解信号量的基本功能对于理解二进制信号量和计数信号量之间的区别至关重要

我已经在这里详细解释了二进制/计数信号量之间的区别,这对您很有用: 完整、缓慢、一步一步地完成这一过程,并确保您理解基本原理。您上面的代码虽然告诉我您已经理解(在一定程度上)信号量内部发生的事情,但并不一定告诉我您知道如何使用信号量

例如,在此位伪代码中:

else
execute the c.s
after execution in c.s calls signal();
你在等待中发出信号——这并不是你想要的方式

再一次,这里:
允许等待且P1离开c.s的第一个进程
我认为您不太了解信号量在哪里结束,它在线程中的使用从哪里开始。我建议从上面的链接开始。然后继续尝试在一些简单的程序中使用信号量,以实现同步和互斥