计数和二进制信号量之间的区别 [英] Difference between Counting and Binary Semaphores

查看:105
本文介绍了计数和二进制信号量之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

计数和二进制信号量有什么区别.

我在某处看到的是,两者都可以控制N个请求资源的进程. 两者都处于空闲"状态.

二进制信号量和计数信号量可以保护多少资源?

两者都只允许一个进程一次使用资源...

还有其他区别吗?上述属性正确吗?

解决方案

实际上,无论尝试访问的实体是进程还是线程,两种类型都用于同步对共享资源的访问.

区别如下:

二进制信号量是二进制的,它们只能有两个值.一个表示进程/线程在关键部分(访问共享资源的代码)中,另一个表示应等待,另一个表示关键部分是空闲的.

另一方面,计数信号量需要两个以上的值,它们可以具有您想要的任何值. 它们取的最大值X允许X进程/线程同时访问共享资源.

有关更多信息,请查看此链接.
http://www.chibios.org/dokuwiki/doku.php?id=chibios: article:semaphores_mutexes

编辑
计数信号量可以采用的最大值是您希望同时允许进入关键部分的进程数.
再次,您可能会希望排除某些资源,但是您知道可以通过最大数量的进程(例如X)来访问此资源,因此您可以将计数信号量设置为X. /p>

这将允许X进程同时访问该资源;但是X + 1进程必须等到关键部分中的一个进程退出.

What is the difference between Counting and binary semaphore.

What I have seen somewhere is that both can control N number of processes which have requested for a resource. Both have taken and Free states.

Is there any restriction on how many Resources a Binary semaphore and Counting semaphore can protect?

Both allow only One process to use a resource at a time...

Is there any other difference? Are the above mentioned properties correct?

解决方案

Actually, both types are used to synchronize access to a shared resource, whether the entity which is trying to access is a process or even a thread.

The difference is as follows:

Binary semaphores are binary, they can have two values only; one to represent that a process/thread is in the critical section(code that access the shared resource) and others should wait, the other indicating the critical section is free.

On the other hand, counting semaphores take more than two values, they can have any value you want. The max value X they take allows X process/threads to access the shared resource simultaneously.

For further information, take a look at this link.
http://www.chibios.org/dokuwiki/doku.php?id=chibios:articles:semaphores_mutexes

EDIT
The max value that a counting semaphore can take is the the number of processes you want to allow into the critical section at the same time.
Again, you might have a case where you want exclusion over a certain resource, yet you know this resource can be accessed by a max number of processes (say X), so you set a counting semaphore with the value X.

This would allow the X processes to access that resource at the same time; yet the process X+1 would have to wait till one of the processes in the critical section gets out.

这篇关于计数和二进制信号量之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆