A Unix IPC (Inter-Process Communication) project that replicates the shell pipe functionality using system calls.
Pipex mimics how the shell executes command pipelines. It takes two commands and connects their input/output streams using pipes and process forking, similar to running:
< file1 cmd1 | cmd2 > file2The program uses two child processes that communicate via a pipe:
[INFILE] → dup2 → [CHILD_1: cmd1] → [PIPE] → [CHILD_2: cmd2] → dup2 → [OUTFILE]
-
Parent Process (
main):- Creates a pipe with
pipe() - Spawns two child processes via
fork() - Waits for both children to complete
- Returns the exit status of the second command
- Creates a pipe with
-
Child Process 1 (
ft_first_command):- Opens the input file (argv[1])
- Redirects stdin to the input file using
dup2(infile, 0) - Redirects stdout to the pipe using
dup2(pipe_fd[1], 1) - Executes the first command (
argv[2])
-
Child Process 2 (
ft_second_command):- Opens/creates the output file (argv[4])
- Redirects stdout to the output file using
dup2(outfile, 1) - Redirects stdin to the pipe using
dup2(pipe_fd[0], 0) - Executes the second command (
argv[3])
./pipex <infile> <cmd1> <cmd2> <outfile>./pipex input.txt "cat" "wc -l" output.txtThis is equivalent to:
cat input.txt | wc -l > output.txt| Call | Purpose |
|---|---|
pipe() |
Creates unidirectional communication channel |
fork() |
Creates child processes |
dup2() |
Redirects file descriptors |
execve() |
Replaces process with new command |
waitpid() |
Parent waits for child completion |
open() |
Opens/creates files |
Pipex/
├── includes/
│ └── pipex.h # Header with function declarations
├── libft/ # Custom C library (string, memory, I/O functions)
├── src/
│ ├── main.c # Entry point, argument validation, pipe creation
│ ├── pipex.c # Core logic: fork, dup2, execve orchestration
│ └── pipex_utils.c # Utility functions: path checking, memory cleanup
└── Makefile # Build configuration
pipex(): Main orchestration function that forks and manages both child processesft_first_command(): Handles first child - reads from infile, writes to pipeft_second_command(): Handles second child - reads from pipe, writes to outfileft_check_path(): Locates command executable in PATH environment variableft_free_split(): Memory cleanup for allocated strings
- Invalid argument count: usage message to stderr
- File access errors: perror output with proper exit codes
- Command not found: error message via
cmd_not_found() - Process/pipe errors: perror output with exit code 129
- Returns the exit status of the second command
- Exit code 1 on critical errors
- Exit code 129 for pipe creation failure