I fork and set up a command like this:
pid_t pid;
pid = fork();
if (pid == 0)
{ // I am the child
freopen("/input_pipe","r",stdin);
freopen("/output_pip","w",stdout);
execl("/bin/sh", "sh", "-c", command, (char *)NULL); // using execv is probably faster
// Should never get here
perror("execl");
exit(1);
}
The /input_pipe has been created and filled with data before the processs is forked.
This works in almost all cases absolutely fine. The command reads (with read()
) from its stdin and gets the data that was written to the pipe by another process.
But sometimes the command cannot read from its stdin stream and gets the "Bad file descriptor" error when trying to do so.
What could cause this error?
Edit: I have changed the freopen parts to this:
pipe_in = open(pipename_in, O_RDONLY);
pipe_out = open(pipename_out, O_WRONLY);
dup2(pipe_in, 0);
dup2(pipe_out, 1);
I will test this for a couple of days though as the error was only appearing very seldom.