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

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

问题描述

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

What is the difference between Counting and binary semaphore.

我在某处看到的是,两者都可以控制 N 个请求资源的进程.两者都有采取和自由状态.

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.

区别如下:

二进制信号量是二进制的,它们只能有两个值;一个表示一个进程/线程在临界区(访问共享资源的代码),其他的应该等待,另一个表示临界区是空闲的.

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.

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

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.

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

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

编辑
计数信号量可以取的最大值是您希望同时允许进入临界区的进程数.
同样,您可能会遇到这样一种情况,即您希望排除某个资源,但您知道该资源可以被最大数量的进程(例如 X)访问,因此您设置了一个值为 X 的计数信号量.

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.

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

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天全站免登陆