问题描述
我正在尝试使用C语言中的管道和fork编写程序.我对如何实现这一点感到困惑.
I am trying to write a program using pipes and forks in C. I am a bit confused as to how to accomplish this.
我需要根据用户指定的输入创建n个进程(也就是说,如果用户输入2,我将通过fork创建2个进程).然后,通过fork创建的每个新进程都会做一些事情.
I need to create an n number of processes based on user-specified input (that is, if the user enters 2, I will create 2 processses via a fork). Each new process created via fork, then, does some stuff.
但是,现在管道中出现了图片.我有管道可以从每个货叉中获取信息.我的问题是-如何为每个叉子创建多个管道?如果我有多个叉子,从父母的角度来看我该如何处理多个管道?
However, now pipes come into the picture. I have pipes to get information back from each fork. My question is - how do I create multiple pipes, for each fork? If I have multiple forks, how can I handle multiple pipes from the parents perspective?
此外,假设我在父进程中有一个字符指针数组,当一个新进程通过fork产生时,该进程可以访问该数组,还是不存在?
Furthermore, suppose I had an array of character pointers in the parent process, when a new process has spawned via fork, can this process access the array, or will it not exist?
谢谢!
推荐答案
使用pipe(),无需处理任何文件系统命名.
Use pipe(), no need to have to handle any filesystem naming.
您可以在 http://上找到一个pipe()示例.用于Linux的man7.org/linux/man-pages/man2/pipe.2.html ,但对于任何* nix,它应该几乎相同.
You can find a pipe() example there http://man7.org/linux/man-pages/man2/pipe.2.html for Linux, but it should be almost the same for any *nix.
基本上,您可以使用pipe()创建一对已连接的fd.然后,您使用fork()并继承了管道对的分支之后的每个进程都关闭了管道的一侧(不需要的一侧).现在,您已经连接了两个进程,每个进程都在管道的一侧(其余的fd).
Basically, you use pipe() to create a pair of connected fd. Then you fork() and each process after the fork, which inherited the pipes pair, closes one (uneeded) side of the pipe. You now have the two processes connected, each having one side of the pipe (the remaining fd).
如果您以后需要处理大量此类操作,则父级主"进程应使用诸如select()或poll()之类的方法,并等待任何子级获得可用数据(意味着子级写了一些东西).还有一个 select()示例.
If you have to handle a lot of them later, the parent "master" process should use something like select() or poll() and wait for any child to have data available (meaning a child wrote something). There's also a select() example.
还请记住,管道是单向的:如果需要在fork()之后向子节点发送数据,则应创建两个管道对.您还可以更改工具并使用socketpair():通用用法相同,但是语义更接近于网络用法,并且是双向的.
Keep also in mind that a pipe is unidirectional: if you need to send data to the child after the fork(), you should create two pipe-pairs. You could also change tools and use socketpair(): same general usage, but semantic is closer to network usage, and it's bi-directional.
没有看到关于数组的最后一部分.就像说另一个答案一样,分叉的进程将具有该数组的副本.如果使用它来传递数据来告诉用户要做什么,那很好,但是ff父级必须在fork()之后提供更多信息,该数组不再是父级的.
Didn't see last part about arrays. As is saying an other answer, the forked process will have a copy of the array. If it was used to pass data telling what work to do it's fine, but ff the parent has to give further informations after the fork() this array isn't the parent's anymore.
如果确实需要在fork()之后在父子之间传递大量数据,则还可以使用一些共享内存机制. mmap()的示例:如何在流程fork()之间共享内存?
If you really need to pass large amounts of data between parent and child after the fork(), you can also use some shared memory mechanism. Example here with mmap():How to share memory between process fork()?
这篇关于C中的管道和叉子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!