如何从单个父进程创建两个进程
我知道我需要使用 fork(),但这只会创建一个子进程。我是否只是从子进程中再次调用 fork ?另外,我需要它们通过信号或管道进行通信,这更容易实现,并且我需要知道什么才能做到这一点(函数等......)
I know I'm going to need to use fork(), but this just creates a single child process. Do i simply call fork again from within the child process? Also, I need them to communicate through a signal or pipe, which is easier to implement and what do i need to know for doing that (functions, etc..)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
要创建第二个进程,请再次调用
fork()
- 在父进程或子进程中(但不能同时调用!)。您选择哪个取决于您希望该进程成为原始父进程的子进程还是第一个子进程的子进程(通常它是原始父进程的子进程)。通过管道进行通信比使用信号更简单、更可靠。
pipe()
、close()
、read()
、write()
和select()
是这里的关键函数。例如,要让父进程创建两个子进程,您可以执行以下操作:
To create a second process, call
fork()
again - either within the parent or the child (but not both!). Which you choose depends on whether you want this process to be a child of the original parent or a child of the first child process (it is usual for it to be a child of the original parent).Communicating through a pipe is much simpler and more reliable than using signals.
pipe()
,close()
,read()
,write()
andselect()
are the key functions here.For example, to have the parent create two child processes, you would do something like:
另一个使用
&&
运算符的奇特代码:Another fancy code using
&&
operator:输出 :
output :
您可以将 fork 放入循环中并根据需要生成任意数量的子进程。
我最近在一个项目中这样做了。
Log_Print() 和 Err_Print() 是内部函数,但非常明显,所以我让它们保持原样。
变量有一个方面需要解释。
nSon
和nSonAsked
应声明为全局变量而不是堆栈变量。这样,它们的价值就会在分叉过程中持续存在。这意味着nSon
变量在每个子项中将具有不同的值。这使得它具有比ownpid()
数字更简单的编号方案。为了完全正确,有很多细节需要正确处理。您必须在父进程中设置信号处理程序来检测子进程的死亡,反之亦然(仅在 Linux 上可行,其他 Unix(至少 Solaris)不支持父进程死亡信号)。
您必须注意,在 fork 后,父进程中打开的文件描述符也会在子进程中打开,并且将是相同。如果您没有意识到,这会带来很多并发问题(解决方案是在正确的位置使用 dup() 和 close() )。
You can put the fork in a loop and generate as many child processes as you need.
I did that on a project recently.
Log_Print() and Err_Print() are internal functions but quite obvious so I let them like they are.
There is one aspect with the variables that has to be explained.
nSon
andnSonAsked
should be declared as globals not as stack variables. This way, their value persists in the forked process. This means that thenSon
variable will have a different value in each of the children. This allows it to have a simpler numbering scheme than theownpid()
number.To get it completely right, there are a lot of details to get right. You will have to set signal handlers in the father process to detect the death of a child, likewise the other way round (only possible on Linux, other Unix (at least Solaris) do not support parent death signals).
You have to be aware that open file descriptors in the father process will be also open in the child after fork and it will be the same one. This opens a lot of concurrency problems if you're not aware of it (the solution is using
dup()
andclose()
in the right places).在这个例子中
they are just sleeping for a few random sec. It also has all the pid, so we can send SIGNAL to communicate...
Most of the #includes are commented cause they were useless where I compiled.
In this example
they are just sleeping for a few random sec. It also has all the pid, so we can send SIGNAL to communicate...
Most of the #includes are commented cause they were useless where I compiled.
只是一点点贡献,如果您想从同一个父级创建 2 个子级,您可以使用下面的代码。其中一个父亲创建 2 个子进程(惰性进程和活动进程)。
如果运行此代码,您应该得到类似的输出:
Just a little contribution, if you want to create 2 childs from the same parent you could use this code below. In which one father create 2 child processes (lazy and active).
If you run this code, you should get a similar output: