← Back to projects

pipex

Reimplementation of the shell pipeline in C: fork, pipe, dup2 and process handling.

Period
29/08/2022
Category
42
Area
Systems
Grade
100
Stack
  • C
  • Unix
Links

Reproducing a shell pipeline from C.

What it does

./pipex file1 cmd1 cmd2 file2 behaves like < file1 cmd1 | cmd2 > file2: the program opens both files, feeds the output of the first command into the input of the second, and runs each one in its own process.

How it works

pipe() gives the pair of descriptors, fork() the processes, dup2() puts the pipe ends and the files where the commands expect them — on descriptors 0 and 1 — and execve() replaces each child with the command, looked up through PATH.

What it taught me

What the shell actually does when you type a |. Redirections stop being magic once you have moved the descriptors yourself.