简体   繁体   English

为什么在任何知道其名称的进程中,线程都可以使用命名信号量?

[英]Why are named semaphores usable by threads in any processes that know their names?

From APUE 从APUE

POSIX semaphores are available in two flavors: named and unnamed. POSIX信号灯有两种样式:已命名和未命名。 They differ in how they are created and destroyed, but otherwise work the same. 它们的创建和销毁方式不同,但其他方面却相同。 Unnamed semaphores exist in memory only and require that processes have access to the memory to be able to use the semaphores. 未命名的信号量仅存在于内存中,并且要求进程有权访问内存才能使用这些信号量。 This means they can be used only by threads in the same process or threads in different processes that have mapped the same memory extent into their address spaces. 这意味着它们只能由相同进程中的线程或将相同内存范围映射到其地址空间中的不同进程中的线程使用。 Named semaphores, in contrast, are accessed by name and can be used by threads in any processes that know their names. 相反, 命名信号量是通过名称访问的,并且可以在任何知道其名称的进程中由线程使用。

Unnamed semaphores "can be used only by threads in the same process or threads in different processes that have mapped the same memory extent into their address spaces", because "Unnamed semaphores exist in memory only". 未命名信号量“只能由同一进程中的线程或已将相同内存范围映射到其地址空间的不同进程中的线程使用”,因为“未命名信号量仅存在于内存中”。

What is the reason that named semaphores are usable by threads in any processes that know their names? 已知信号量在任何知道其名称的进程中可被线程使用的原因是什么?

Thanks. 谢谢。

From man page of sem_overview : sem_overview的手册页中:

On Linux, named semaphores are created in a virtual file system, normally mounted under /dev/shm, with names of the form sem.somename 在Linux上,命名信号量是在虚拟文件系统中创建的,通常安装在/ dev / shm下,其名称格式为sem.somename。

So those are accessible for 'threads in any processes' similar way than normal files are. 因此,可以用与普通文件类似的方式访问“任何进程中的线程”。

pthread library may then map those files to memory. 然后,pthread库可以将那些文件映射到内存。

You're thinking about this backwards. 您正在考虑这个倒退。 The question is: "if I need to synchronize use of a shared resource between teo unrelated processes, hiw do I do it?" 问题是:“如果我需要在不相关的进程之间同步使用共享资源,我该怎么办?” And the answer is "you can give a semaphore a name, and then it's not restricted to use in processes which share memory." 答案是“您可以给一个信号灯起一个名字,然后它就不限于在共享内存的进程中使用。”

Why is that even useful? 为什么这样有用? Well, the use cases may not be common -- perhaps you've never run into one -- but they certainly exist. 好吧,用例可能并不常见-也许您从未遇到过-但它们确实存在。 There are lots of resources which are shared between unrelated processes: databases, configuration files, serial ports, printer queues, and many more. 在不相关的进程之间共享许多资源:数据库,配置文件,串行端口,打印机队列等等。 You can mediate between shared uses of these resources with lock files, but it's clunky and you end up reinventing the wheel on every project. 您可以在带有锁定文件的这些资源的共享使用之间进行调解,但是这很笨拙,最终您需要在每个项目上重新设计轮子。 Semaphores, on the other hand, are easy to use and have well-defined documented semantics. 另一方面,信号量易于使用,并且具有定义明确的文档化语义。

However, most uses of semaphores are indeed between related processes which share memory. 但是,信号量的大多数使用确实是在共享内存的相关进程之间进行的。 And you wouldn't want to unnecessarily pay the overhead for maintaining a name in a filesystem. 而且您也不需要不必要地支付在文件系统中维护名称的开销。

So we end up with two kinds of semaphores: cheap low-overhead ones which serve the most frequent use cases, and heavier higher-overhead ones which can be used more generally. 因此,我们最终得到两种信号量:便宜的低开销的信号量最大的用例,以及较重的较高开销的量信号,可以更广泛地使用。 The nice thing is that the semantics and API are very similar, so you don't need to learn a whole new set of concepts when you start using named semaphores. 令人高兴的是,语义和API非常相似,因此在开始使用命名信号量时,您无需学习全新的概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM