[ACCEPTED]-Is it possible to have pipe between two child processes created by same parent (LINUX, POSIX)-pipe

Accepted answer
Score: 24

This is essentially what a shell does if 39 build a redirection chain, i.e. something 38 like

ls | grep foo | sort | uniq

There are some excellent introducionary 37 texts on Unix programming, in which a simple 36 shell is implemented through the book. And 35 one of the tasks of a shell is redirection. One 34 of these books is "Linux Application 33 Programming" by Michael K. Johnson 32 and Erik W. Troan.

The book's homepage: http://ladweb.net/

To 31 build a chain of redirections for N processes 30 you need N-1 pipes. For each redirection 29 you create a pipe using the pipe(int fds[2]) system call. After 28 fork()ing, but before execving use dup2(int from, int to) to "connect" a 27 pipe's end to the standard input (0) or 26 standard output of each process. Here's 25 a overly simplified code, without error 24 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(...);
}

Take note that the pipe's array 23 indices have been choosen in a way that 22 they reflect the standard input/output file 21 descriptors if they're used for stdio redirection. This 20 choice was not arbitrary.

Of course you can 19 connect pipes to any file descriptors (e.g. there 18 are some applications, which expect their 17 parent to open, say fd 3 and 4, connected 16 to pipes) and most shells directly support 15 this, too (for example 1>&3 will 14 redirect stdout into fd 3). However the 13 array indices for pipe(int fds[2]) are 0 and 1 of course. I'm 12 just telling this, because I had some cargo-cult-programming 11 students, which mindlessly took the target 10 fds also for the pipe syscall array.

To wait 9 for all the children to finish use waitpid(-1, NULL, 0) – I 8 think that's the -1 my pre-answerer meant, which 7 means: Wait for all child processes to finish. The 6 other option was calling wait() in a loop which 5 will return the pid of the just terminated 4 child. If called again and there's still 3 a child running, it will block again. If 2 there's no child left, it will return -1; I 1 prefer the waitpid solution.

Score: 5

Yes, this is fairly easy, you just need 8 to create all the pipes in the parent, and 7 remember to close the pipes / ends of pipes 6 in the child(ren) that you don't need them 5 in.

Leaving FDs of the pipes open in children 4 which aren't using them is a FAIL as it 3 can make others wait forever for the end 2 of the pipe. All writers must close before 1 the reader gets an EOF.

Score: 2

Create all the pipes first, then spawn all 3 the children with the appropriate pipe ends 2 in FDs 0 and 1.

As for waiting, just keep 1 waiting until it returns -1.

More Related questions