A 42 school project that recreates the behavior of Unix shell pipes using system calls. Pipex demonstrates inter-process communication by chaining commands through pipes, mimicking the shell's pipe operator (|).
This project showcases understanding of:
- Unix Process Management: Creating and managing child processes with
fork() - Inter-Process Communication: Using
pipe()to connect process input/output streams - Program Execution: Replacing process images with
execve() - File Descriptor Manipulation: Redirecting stdin/stdout with
dup2() - Error Handling: Managing system call failures and process exit codes
- PATH Resolution: Searching executable paths in the environment
./pipex infile "cmd1" "cmd2" outfileThis behaves like the shell command:
< infile cmd1 | cmd2 > outfileExample:
./pipex infile "grep hello" "wc -l" outfile
# Equivalent to: < infile grep hello | wc -l > outfile./pipex infile "cmd1" "cmd2" "cmd3" ... "cmdN" outfileThis behaves like:
< infile cmd1 | cmd2 | cmd3 | ... | cmdN > outfileExample:
./pipex infile "cat" "grep pattern" "sort" "uniq" outfile
# Equivalent to: < infile cat | grep pattern | sort | uniq > outfile./pipex_bonus here_doc LIMITER "cmd1" "cmd2" outfileThis behaves like:
cmd1 << LIMITER | cmd2 >> outfileExample:
./pipex_bonus here_doc EOF "grep hello" "wc -l" outfile
# Reads input until "EOF" is typed
# Pipes through grep and wc, appends to outfileThe mandatory implementation handles a two-command pipeline:
- Process Creation: Forks two child processes
- First Child: Opens
infile, redirects stdin from it, redirects stdout to pipe write end, executescmd1 - Second Child: Redirects stdin from pipe read end, opens/creates
outfile, redirects stdout to it, executescmd2 - Parent Process: Closes pipe ends and waits for both children to complete
- Exit Code: Returns the exit code of the last command in the pipeline
Supports chaining an arbitrary number of commands:
- Creates
N-1pipes forNcommands - Each command runs in its own child process
- First command reads from
infile - Last command writes to
outfile - Middle commands read from previous pipe and write to next pipe
- All pipes are closed appropriately to prevent resource leaks
Implements heredoc functionality:
- Reads input from stdin until
LIMITERis encountered - Stores input in a pipe (not a temporary file)
- Pipes through specified commands
- Appends output to
outfile(instead of truncating) - Validates that exactly 6 arguments are provided
The program handles various error scenarios:
- Invalid Arguments: Displays usage message and exits
- File Errors: Reports when infile cannot be opened, outfile cannot be created
- Command Not Found: Exits with code 127 when command doesn't exist in PATH
- System Call Failures: Uses
perror()to report pipe, fork, dup2, or execve failures - Exit Codes: Preserves child process exit codes and signals
The project includes a Makefile with the following targets:
make # Compiles mandatory part -> pipex
make bonus # Compiles bonus part -> pipex_bonus
make clean # Removes object files
make fclean # Removes object files and executables
make re # Recompiles everything from scratchCompilation flags: -Wall -Wextra -Werror (strict error checking)
The following standard C and Unix system functions are permitted:
- File Operations:
open,close,read,write,unlink,access - Memory Management:
malloc,free - Process Management:
fork,wait,waitpid,wait3,wait4 - Program Execution:
execveand its variants (execvp,execlp, etc.) - Inter-Process Communication:
pipe,dup,dup2 - Error Handling:
perror,strerror - Standard I/O:
exit - libft: Custom library functions from previous 42 projects
pipex/
├── pipex.c # Mandatory entry point and main logic
├── pipex_bonus.c # Bonus entry point (here_doc + multi-pipe)
├── proccess.c # Process handling and command execution
├── process_bonus.c # Multi-pipe process management
├── here_doc.c # Heredoc implementation
├── pipex_utils.c # PATH parsing and error utilities
├── path_utils.c # Environment variable extraction
├── utils.c # String manipulation helpers
├── utils_bonus.c # Bonus utility functions
├── pipex.h # Header file with function prototypes
├── Makefile # Build automation
└── libft/ # Custom C library (from previous projects)
- Validate argument count (must be 5)
- Create pipe with
pipe() - Fork first child:
- Open
infilefor reading - Redirect stdin from
infile - Redirect stdout to pipe write end
- Execute
cmd1
- Open
- Fork second child:
- Redirect stdin from pipe read end
- Create/truncate
outfile - Redirect stdout to
outfile - Execute
cmd2
- Parent closes pipe and waits for children
- Extracts PATH from environment variables
- Splits PATH by ':' delimiter
- Tests each directory with
access()for executable permission - Returns full path to command or NULL if not found
- All unused file descriptors are closed in each process
- Prevents descriptor leaks and blocking
- Ensures proper cleanup on error paths
Note: This project is part of the 42 school curriculum and implements Unix pipe behavior from scratch using only allowed system calls. It does not use shell execution or high-level piping functions.