Both pipes, then the fork, then the closes
Both pipes, then the fork, then the closes
Answer
int a[2], b[2], c1; if (pipe(a) == -1 || pipe(b) == -1) _exit(1); if ((c1 = fork()) == -1) _exit(1); if (c1 == 0) { close(a[0]); close(b[0]); close(b[1]); /* child 1 now holds a[1] and nothing else */ }
Both pipes exist before the fork, so the child inherits both. Its first act is to close the three ends it will never use — that is the discipline that makes the whole program work, and it must be repeated in child 2 and in the parent.
S&K 3e ch20 §20.4.2.3; write(2), stat(2), mkdir(2), wait(2) per POSIX.1-2024