I have found quite a few questions similar to this one (e.g. https://www.quora.com/How-can-I-write-a-code-for-PIPE-in-C-shell-script-python), which work in the cases described by those questions, but not mine for some reason. The answers I've found so far deal with either piping output from one child to another (the aforementioned parenthesized link) or from child to parent/parent to child. What I want to do is have the parent be able to write to the child's STDIN and read from its STDOUT. Or, in a more general case, given some number of children, have the parent write to the first child's STDIN, which will pipe its output to the second child, which will pipe its output to the third, and so on, until the last child pipes its output back to the parent. That is, I would like to be able to do something like prog0 | prog1 | prog 2 | ...
from C/C++ code. My current half-attempt (half because I'm only trying to read from an echo
's STDOUT before attempting a full input/output attempt using something simple like cat -
) looks something like this (some error checking omitted for brevity):
void test() {
int pipe_fds[2];
pid_t cpid, wpid;
int status;
char buf[16] = {0};
status = pipe(pipe_fds);
cpid = fork();
if (cpid == 0) {
dup2(pipe_fds[1], STDOUT_FILENO);
close(pipe_fds[0]);
execlp("echo", "Hello world!", NULL);
printf("Child returned abnormally: %d\n", errno);
_exit(3);
}
close(pipe_fds[1]);
wpid = waitpid(cpid, &status, 0);
printf("Waited on pid %d, which exited with %d\n", wpid, WEXITSTATUS(status));
read(pipe_fds[0], buf, 16);
printf("Child said: %s\n", buf);
close(pipe_fds[0]);
exit(0);
}
This runs, but the parent gets no visible output from the child. How would I fix this, and, more importantly, how would I go about piping to [and from] a chain of processes as I had mentioned earlier?
nullptr
is C++. Either change it to0
orNULL
(which I'd recommend, this code really is just C) -- or change the tag to C++. Don't tag both. – user2371524int pipes[2];
very misleading. A single pipe has two ends, so this should be e.g.int pipefds[2];
. If you want TWO pipes, you could useint pipes[2][2];
. – user2371524