educative.io

How to use Pipe()

I feel like question 8 came out of nowhere. Unless I missed a slide, there’s essentially no discussion of pipe() as system call (versus bash pipes). Also, the man page for pipe() doesn’t explain how to modify stdout for a different process. Here’s my best guess at a solution:

  1. The parent creates a pipe. This is, it calls pipe(pipefd), where pipefd is an array of size 2.
  2. The parent creates two children that thus inherit the pipe.
  3. The first child somehow sets stdin to pipefd[0], and the second child sets stdout to pipefd[1].

Although, I’m not sure how step 3 would be done. For instance, I know how the second child could use write(pipefd[1], …) to write to the pipe. But that’s not what was asked for, which is to set stdout to one end of the pipe.

Trying to google this leads to suggestions like “just use bash: ./a > ./b” Which certainly is the best way to connect one program’s stdout to another’s stdin … but it’s not what the exercise asks for.


Course: Operating Systems: Virtualization, Concurrency & Persistence - Learn Interactively
Lesson: Exercise - Operating Systems: Virtualization, Concurrency & Persistence