hi,
i have a program that needs to connect 3 programs through a pipe.
program1 stdout > stdin program2 stdout >stdin program 3.
i have so far created the following code below. iam just stuck on how to add the 3rd program and create a pipe to program 3
any suggestions to point me in the right direction would be great.
thanks heaps.
Code:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
int pid,pid2 ;
int fd[2];
pipe(fd);
if( (pid = fork() ) == -1 )
{
perror("fork");
exit(1);
}
/* child */
if ( pid == 0 )
{
close(fd[1]);
dup(fd[0]); // sets stdin to this end of pipe
close(fd[0]);
execlp( "./prog1", "prog1", NULL ); /* and run */
perror("execlp");
exit(1);
}
/* parent waits then reports */
if ( pid != 0 )
{
close(fd[0]);
dup(fd[1]); // sets stdout to this end of pipe
close(fd[1]);
execlp("./prog2", "prog2", NULL);
wait(NULL);
}
}