是否有可能通过具有相同的父创建了两个子进程之间的管道(LINUX,POSIX) [英] Is it possible to have pipe between two child processes created by same parent (LINUX, POSIX)
问题描述
我有多个子分叉受同一母公司和我试图建立所有这些子进程就像一个链表结构之间的管道
连接。儿童1发送数据的child2,儿童2儿童3 ....孩子N至1.子女是否有做任何正确的方法?
I have multiple child "forked" by the same parent and I try to construct pipe
connection between all these child processes like a linked list structure. Child 1 sends data to child2, child 2 to child 3.... child N to child 1. Is there any proper way to do it?
另外,如果我创建和流程我如何强制父等待的所有过程,因为来完成他们的工作等待()之间进行通信
或 waitpid函数()
等待第一个完成的过程,但我需要等待他们。这是另外一个问题,即出现了。
In addition, if I create and communicate between processes how I force the parent to "wait" all the process to finish their job since wait()
or waitpid()
waits for first finished process but I need to wait them all. It is the another question that arises.
谢谢...
推荐答案
这基本上是什么外壳程序,如果建立一个重定向链,即像
This is essentially what a shell does if build a redirection chain, i.e. something like
ls | grep foo | sort | uniq
有在Unix编程的一些优秀introducionary文本,其中一个简单的外壳是通过这本书来实现。与外壳的任务之一,就是重定向。其中这些书是Linux应用程序编程由Michael K. Johnson和埃里克W. Troan。
There are some excellent introducionary texts on Unix programming, in which a simple shell is implemented through the book. And one of the tasks of a shell is redirection. One of these books is "Linux Application Programming" by Michael K. Johnson and Erik W. Troan.
这本书的主页: http://ladweb.net/
要建立重定向链N个流程需要N-1的管道。对于每个重定向创建使用管(INT FDS [2])
系统调用的管道。后叉()
荷兰国际集团,但在此之前 execv
ING使用 dup2(为INT从,INT )
到管道的末端连接到标准输入(0)或每个过程的标准输出。这里是一个过于简单化code,没有错误检查:
To build a chain of redirections for N processes you need N-1 pipes. For each redirection you create a pipe using the pipe(int fds[2])
system call. After fork()
ing, but before execv
ing use dup2(int from, int to)
to "connect" a pipe's end to the standard input (0) or standard output of each process. Here's a overly simplified code, without error checking:
int pipe_A[2];
int pipe_B[2];
pipe(pipe_A);
pipe(pipe_B);
pid_t pid_A, pid_B, pid_C;
if( !(pid_A = fork()) ) {
dup2(pipe_A[1], 1); /* redirect standard output to pipe_A write end */
execv(...);
}
if( !(pid_B = fork()) ) {
dup2(pipe_A[0], 0); /* redirect standard input to pipe_A read end */
dup2(pipe_B[1], 1); /* redirect standard output to pipe_B write end */
execv(...);
}
if( !(pid_C = fork()) ) {
dup2(pipe_B[0], 0); /* redirect standard input to pipe_B read end */
execv(...);
}
请注意该管的数组索引已经在某种程度上被choosen它们反映标准输入/输出的文件描述符,如果他们使用标准输入输出重定向。这种选择不是随意的。
Take note that the pipe's array indices have been choosen in a way that they reflect the standard input/output file descriptors if they're used for stdio redirection. This choice was not arbitrary.
当然,你可以管连接到任何文件描述符(例如有一些应用,其中期望它们的父开,说的fd 3和4,连接到管道)和最壳直接支持此,太(例如1>和3将标准输出重定向到FD 3)。然而,对于管数组索引(INT FDS [2])
0,当然1。我只是告诉这一点,因为我有一些货物邪教编程的学生,其无意识地把目标FDS还为管道系统调用数组。
Of course you can connect pipes to any file descriptors (e.g. there are some applications, which expect their parent to open, say fd 3 and 4, connected to pipes) and most shells directly support this, too (for example 1>&3 will redirect stdout into fd 3). However the array indices for pipe(int fds[2])
are 0 and 1 of course. I'm just telling this, because I had some cargo-cult-programming students, which mindlessly took the target fds also for the pipe syscall array.
要等待所有孩子完成使用 waitpid函数(-1,NULL,0)
- 我认为这是我的-1 pre-回答者的意思,这意味着:等待所有子进程结束。另一种选择是调用的wait()
在一个循环将返回刚刚终止的子进程的PID。如果又一次打来电话,还是有奔跑的小孩,它会再次阻塞。如果有没有留下孩子,它会返回-1;我preFER的 waitpid函数
解决方案。
To wait for all the children to finish use waitpid(-1, NULL, 0)
– I think that's the -1 my pre-answerer meant, which means: Wait for all child processes to finish. The other option was calling wait()
in a loop which will return the pid of the just terminated child. If called again and there's still a child running, it will block again. If there's no child left, it will return -1; I prefer the waitpid
solution.
这篇关于是否有可能通过具有相同的父创建了两个子进程之间的管道(LINUX,POSIX)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!