Originally Posted by
CornedBee
Correct, and they need to be the two ends of one pipe.
Well i'm having a problem with the code. The 2nd program is not waiting for the first program to send the input.
Code:
pipe (p);
pid1 = fork();
if (pid1 == 0)
{
close (p[0]); // close the pipe input
close (1); //close stdout
dup (p[1]); //redirect stdout to output of pipe
execvp (*argv1, argv1);
printf ("Error: Could not execute the 1st program\n");
exit (1);
}
else if (pid1 != 0)
{
pid2 = fork();
if (pid2 == 0)
{
close (p[1]); // close the pipe output
close (0); //close stdin
dup (p[0]); //redirect stdin to input of pipe
execvp (*argv2, argv2);
printf ("Error: Could not execute the 2nd program\n");
fflush (stdout);
exit (1);
}
}
Both programs start execution, so there are no problems with the args.
The programs i'm using are:
Code:
prog1
#include <stdio.h>
int main ()
{
printf ("%s",system ("ls -1"));
return 0;
}
Code:
prog2
#include <stdio.h>
int main ()
{
char buff[1024];
while (fgets(buff, 1024, stdin) != NULL)
{
printf ("This is it: %s--\n", buff);
}
return 0;
}
What have i done wrong?
Thanks.