分叉的子进程是否使用相同的信号量?
假设我创建了一个信号量。如果我分叉了一堆子进程,它们仍然会使用相同的信号量吗?
另外,假设我创建一个内部有信号量并分叉的结构。所有子进程是否仍然使用相同的信号量?如果不是,将结构+信号量存储在共享内存中是否允许子进程使用相同的信号量?
我真的很困惑我的分叉子进程如何使用相同的信号量。
Let's say I create a semaphore. If I fork a bunch of child processes, will they all still use that same semaphore?
Also, suppose I create a struct with semaphores inside and forked. Do all the child processes still use that same semaphore? If not, would storing that struct+semaphores in shared memory allow the child processes to use the same semaphores?
I'm really confused about how my forked child processes can use the same semaphores.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 SysV IPC 信号量 (
semctl
),那么可以。如果您使用 POSIX 信号量 (sem_init
),那么可以,但是 仅当您在创建时为 pshared 参数传递一个真值并将其放置在共享内存中时。“内部信号量”是什么意思?对 SysV IPC 信号量的引用将被共享,因为信号量不属于任何进程。如果您使用 POSIX 信号量,或者使用 pthreads 互斥体和 condvars 构建某些内容,则需要使用共享内存和 pshared 属性(pthreads 有一个 pshared 属性 condvars 和 互斥体)
请注意,使用 MAP_SHARED 标志创建的匿名 mmap出于这些目的,算作(匿名)共享内存,因此无需实际创建命名共享内存段。 分叉后普通堆内存将不会被共享。
If you are using a SysV IPC semaphore (
semctl
), then yes. If you are using POSIX semaphores (sem_init
), then yes, but only if you pass a true value for the pshared argument on creation and place it in shared memory.What do you mean be 'semaphores inside'? References to SysV IPC semaphores will be shared, because the semaphores don't belong to any process. If you're using POSIX semaphores, or constructing something out of pthreads mutexes and condvars, you will need to use shared memory, and the pshared attribute (pthreads has a pshared attribute for condvars and mutexes as well)
Note that anonymous mmaps created with the
MAP_SHARED
flag count as (anonymous) shared memory for these purposes, so it's not necessary to actually create a named shared memory segment. Ordinary heap memory will not be shared after a fork.这取决于您如何创建信号量,要使用 IPC 信号量来执行此操作,请参阅 semaphore.c:简单信号量传递示例的说明。
为此,您的信号量需要存储在父进程和子进程之间共享的区域(如共享内存)中,而不仅仅是在堆栈或堆上创建,因为当进程分叉时它将被复制。
信号量可以在线程或进程之间共享。跨进程共享是在操作系统层面实现的。两个或多个不同的进程可以共享相同的信号量,即使这些进程不是通过分叉单个父进程创建的。
请参阅此示例,了解在父进程及其子进程之间共享未命名的 UNIX 信号量(要使用 gcc 进行编译,您将需要
-pthread
标志):输出将是:
您可能想要读取 Linux 中的信号量同样,但请注意,给出的跨 fork 的 UNIX 信号量示例不起作用,因为作者忘记在
mmap
中使用MAP_ANONYMOUS
标志。It depends how you created the semaphore, to do that with an IPC semaphore see semaphore.c: Illustration of simple semaphore passing for an example.
For that to work your semaphore needs to be stored in an area shared between the parent and the child process like shared memory, and not just created on the stack or on the heap because it will be copied when the process forks.
The semaphore can be shared across threads or processes. Cross-process sharing is implemented on the operating system level. Two or more different processes can share the same semaphore even if those processes were not created by forking a single parent process.
See this example for sharing an unnamed UNIX semaphore between a parent process and its child (to compile with gcc you'll need the
-pthread
flag):The output will be:
You may want to read Semaphores in Linux as well, but be aware that the example of UNIX semaphores across fork given doesn't work because the author forgot to use the
MAP_ANONYMOUS
flag inmmap
.尝试这个
孩子和父母交替增加共享变量
Try this
child and parent would increment the shared variable alternatively