This project has been created as part of the 42 curriculum by mkhandaq.
pipex is a program that recreates the pipe mechanism found in Unix shells. It executes two commands in sequence, connecting them via a pipe so that the output of the first command becomes the input of the second command, mimicking the behavior of the shell pipe operator |.
The program takes four arguments: an input file, two commands, and an output file. It behaves like the following shell command:
< file1 cmd1 | cmd2 > file2The goal of this project is to understand and implement fundamental Unix concepts including inter-process communication through pipes, process creation with fork(), file descriptor manipulation with dup2(), command execution with execve(), and process synchronization with waitpid().
To compile the project, use the provided Makefile:
make # Compiles the pipex executable
make clean # Removes object files
make fclean # Removes object files and the executable
make re # Recompiles everything from scratchThe program requires exactly 4 arguments:
./pipex file1 cmd1 cmd2 file2Where:
file1is the input filecmd1is the first command to executecmd2is the second command to executefile2is the output file
Examples:
./pipex infile "grep hello" "wc -l" outfileThis is equivalent to: < infile grep hello | wc -l > outfile
./pipex input.txt "cat" "grep 42" output.txtThis is equivalent to: < input.txt cat | grep 42 > output.txt
The program will execute cmd1 with file1 as input, pipe the output to cmd2, and write the final result to file2. Error messages are printed to stderr when files cannot be accessed or commands are not found. The program returns the exit status of the last successfully executed command.
- [pipe(2) - Linux manual page] - Create an interprocess channel
- [fork(2) - Linux manual page] - Create a child process
- [execve(2) - Linux manual page] - Execute a program
- [dup2(2) - Linux manual page] - Duplicate a file descriptor
- [waitpid(2) - Linux manual page] - Wait for process state changes
- [access(2) - Linux manual page] - Check user's permissions for a file