做派生的子进程使用同一个信号? [英] Do forked child processes use the same semaphore?

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

问题描述

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

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

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


解决方案

  

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


如果您使用的是SysV的IPC信号量(了semctl ),然后是。如果您正在使用POSIX信号量( sem_init ),那么是的,但的只有当你传递一个真正的价值就创造pshared的参数,并把它放在共享内存中。


  

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


你是什么意思是'内信号灯'?引用SysV的IPC信号灯将被共享的,因为信号量不属于任何处理。如果您正在使用POSIX信号,或建造出来的东西pthreads的互斥体和条件变量,你需要使用共享内存,并pshared的属性(pthreads的对的条件变量互斥以及)

请注意,与 MAP_SHARED 标志创建匿名mmaps算作(匿名)共同为这些目的的内存,因此它不必实际创建一个名为共享内存段。的普通堆内存不会被叉后共享

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.

解决方案

Let's say I create a semaphore. If I fork a bunch of child processes, will they all still use that same semaphore?

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.

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?

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.

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

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