名称和未命名信号量 [英] Name and Unnamed Semaphore

查看:80
本文介绍了名称和未命名信号量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解已命名和未命名信号量之间的异同,所以我的Google搜索使我

I'm trying to understand the similarities and differences between named and unnamed semaphore so my google searches yielded me this. I had a question about the wording on the page though, it says:

  • 未命名的信号灯可能通过多个过程可用
  • 命名信号量可以通过多个过程共享
  • Unnamed semaphores might be usable by more than one process
  • Named semaphores are sharable by several processes

这两个词在这两种信号量之间产生了重要区别还是不相关?

Do those two words create any important distinction between those two types of semaphores or are they irrelevant?

到目前为止,这是我所拥有的:

So so far here's what I have:

Similarities
    -Several processes can do something with the semaphore

Difference
    -Named are referenced with pathname and unnamed are referenced by pshared value

这就是我从定义中可以收集到的全部信息.这一切都正确吗?还是我错过了一些重要的概念?

That's all I could glean from that definition. Is that everything and are they correct? Or am I missing some significant concept?

推荐答案

可以访问信号量的角度考虑.

Think in terms of who can access the semaphore.

未命名的信号灯(缺少任何名称或句柄来定位它们)必须存在于一些预先存在的,商定的内存位置中.通常,这是(1)在子进程的情况下,共享内存(由fork之后的子进程继承);或(2)共享内存,全局变量或堆(如果它们在单个进程的线程之间共享).这里最重要的是,父代,子代或线程中的代码已经知道信号量的地址.

Unnamed semaphores (lacking any name or handle to locate them) must exist in some pre-existing, agreed upon memory location. Usually that is (1) shared memory (inherited by children after fork) in the case of child processes; or (2) shared memory, global variable or the heap in the case where they are shared between threads of a single process. The essential thing here is that the code in parent, child, or threads already knows the address of the semaphore.

命名的信号量对于不相关的进程是必需的.例如,一个生产者和一个消费者可能是由两个不同的开发人员编写的,并作为完全不相关的流程运行.但是他们必须共享一些需要使用信号灯保护的资源.命名的信号量为他们提供了通往信号量的路径.

Named semaphores are necessary for unrelated processes. For example a producer and consumer might be written by two different developers and run as completely unrelated processes. But they have to share some resource that needs to be protected by a semaphore. The named semaphore gives them a path to the semaphore.

实际上,您可以在所有情况下使用命名信号量,但是它们带有一些额外的负担,因为您必须处理路径和权限,如果程序相关并且已经知道如何访问未命名的信号和权限,则这是不必要的信号.例如,使用命名信号量在线程之间共享资源有点愚蠢.线程已经可以访问未命名信号可能驻留的同一内存.

In reality you can use a named semaphore in all scenarios but they come with a little extra baggage because you have to deal with the paths and permissions and such that are unnecessary if the programs are related and already know how to access an unnamed semaphore. It's a little silly, for instance, to use a named semaphore to share a resource between threads. The threads already have access to the same memory where an unnamed semaphore could reside.

这篇关于名称和未命名信号量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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