本文介绍了做派生的子进程使用同一个信号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我创建了一个信号量。如果我叉了一堆的子进程,将它们都仍然使用同一信号?

此外,假设我创建内部和分叉信号灯一个结构。是否所有的子进程仍然使用相同的信号?如果不是这样,将存储该结构+在共享存储器信号灯允许子进程使用相同的信号量

我真搞不清楚我的派生的子进程如何可以使用相同的信号量。


解决方案

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.

这篇关于做派生的子进程使用同一个信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:40