Pipex
A C program that simulates the behavior of shell pipes, handling process creation and execution.
2021-08-01
low-levelOpen GitHub

Pipex
A C program that replicates what the shell does when you write < infile cmd1 | cmd2 > outfile. Two child processes, one pipe, file descriptor redirections, and execve calls. The result is functionally identical to the shell version.
Usage
./pipex infile "cmd1" "cmd2" outfile
# same as: < infile cmd1 | cmd2 > outfile
./pipex input.txt "grep hello" "wc -l" output.txtBonus - multiple pipes:
./pipex infile "cmd1" "cmd2" "cmd3" outfile
# same as: < infile cmd1 | cmd2 | cmd3 > outfileBonus - heredoc:
./pipex here_doc LIMITER "cmd1" "cmd2" outfile
# same as: cmd1 << LIMITER | cmd2 >> outfileHow it works
[infile] --> [child1: cmd1] --pipe--> [child2: cmd2] --> [outfile]
| |
fork() fork()
+-------- parent -------+
|
waitpid()
- Parent opens infile and outfile
- Creates a pipe (read end and write end)
- Child 1 redirects stdin from infile, stdout to the pipe write end, runs cmd1
- Child 2 redirects stdin from the pipe read end, stdout to outfile, runs cmd2
- Parent waits for both
Commands are looked up from $PATH.
Errors
| Situation | What happens |
|---|---|
| infile missing or unreadable | Prints error, exits |
| outfile can't be created | Prints error, exits |
| Command not found in PATH | Prints "command not found", child exits |
| pipe() or fork() fails | System error printed, exits |
SCREENSHOTS // 01 FRAMES

01 / 01