Hi all,
I'm trying to write a program that has some data it wants to send through a filter program(in this case tr), and then recieve the output from that filter program. The way I'm trying to do it is by setting up two pipes between the programs and piping the data in through one pipe and back out through the other one. However all it does at the minute is hang. I think waiting for input from stdin but I can't figure out why. I'm guessing that the child process must not be getting sent any data but I don't how know how to fix it.
Here is the code (sorry if its a little long)
Code:
int main (void)
{
pid_t pid;
int pipe_in[2];
int pipe_out[2];
char buffer[800];
/* Create the pipe. */
pipe (pipe_in);
pipe (pipe_out);
/* Create the child process. */
pid = vfork ();
if (pid == (pid_t) 0) /* child 1 */
{
/*close unneeded*/
close(pipe_in[1]);
close(pipe_out[0]);
/*Make pipes the stdin and stdout*/
dup2(pipe_in[0], 0);
close(pipe_in[0]);
dup2(pipe_out[1], 1);
close(pipe_out[1]);
/*change a to b*/
execl("/bin/tr", "tr", "a", "b" , 0);
perror("exec prog1");
exit(1);
}
else /*parent*/
{
/*close unneeded*/
close(pipe_in[0]);
close(pipe_out[1]);
/*write to and then read from child*/
write(pipe_in[1], "This is the data\n", 14);
read(pipe_out[0], buffer, sizeof(buffer));
/*send EOF to child*/
close(pipe_in[1]);
/*wait for end then close other end of pipe*/
waitpid(pid, NULL, 0);
close(pipe_out[0]);
return EXIT_SUCCESS;
}
}
Any help is greatly appreciated
Ben Goudey