This project was created as part of the 42 curriculum by chgomez.
pipex is a C project that recreates the behavior of a Unix shell pipeline.
The program takes an input file, executes two commands connected by a pipe, and redirects the final output to an output file.
It reproduces the following shell behavior:
< infile cmd1 | cmd2 > outfileUsing the program:
./pipex infile "cmd1" "cmd2" outfileThe goal of pipex is to understand how Unix processes communicate through pipes and how command execution works at a low level.
The project uses system calls such as fork(), pipe(), dup2(), execve(), open(), close() and waitpid() to create two child processes, redirect file descriptors and execute external commands.
The first child process reads from the input file and sends its output through the pipe. The second child process reads from the pipe and writes the final result into the output file.
Shell behavior:
< infile grep "hello" | wc -l > outfileEquivalent pipex usage:
./pipex infile "grep hello" "wc -l" outfile- Recreates a basic Unix pipeline.
- Executes two commands using
execve(). - Searches command paths through the environment variable
PATH. - Redirects input and output using file descriptors.
- Connects processes with
pipe(). - Creates child processes with
fork(). - Handles command arguments with a custom
ft_split. - Includes error handling for files, commands and system calls.
- Builds the executable using a custom
Makefile.
- C language
- Unix system calls
- File descriptors
- Process creation
- Pipes
- Environment variables
- Makefile
- Compilation with
cc - Flags:
-Wall -Wextra -Werror
.
├── Makefile
├── en.subject.pdf
├── include/
│ └── pipex.h
└── src/
├── main.c
├── child1.c
├── child2.c
├── execute.c
├── errors.c
├── ft_split.c
└── utils.c
Compile the project:
makeThis generates the executable:
pipex
Remove object files:
make cleanRemove object files and the executable:
make fcleanRebuild the project:
make reGeneral syntax:
./pipex infile "cmd1" "cmd2" outfileExample:
./pipex infile "grep hello" "wc -l" outfileThis should behave like:
< infile grep hello | wc -l > outfileCreate a simple input file:
echo -e "hello\nworld\nhello 42" > infileRun pipex:
./pipex infile "grep hello" "wc -l" outfileCheck the result:
cat outfileExpected output:
2
You can compare it with the shell behavior:
< infile grep hello | wc -l > expected
diff outfile expectedIf diff produces no output, both results are the same.
main.cvalidates arguments, creates the pipe and manages child processes.child1.chandles the first command and redirects the input file to the pipe.child2.chandles the second command and redirects the pipe output to the output file.execute.cresolves command paths and executes commands withexecve().errors.ccentralizes error handling.utils.ccontains helper functions used across the project.ft_split.cprovides custom string splitting for command parsing.
- How Unix pipes work internally.
- How to create and manage child processes with
fork(). - How to redirect standard input and output with
dup2(). - How to execute external programs with
execve(). - How to work with environment variables such as
PATH. - How file descriptors are inherited by child processes.
- How to close unused file descriptors to avoid leaks or blocked processes.
- How to structure a small Unix system programming project in C.
man 2 pipeman 2 forkman 2 dup2man 2 execveman 2 openman 2 waitpid- 42 pipex subject
- Unix process and file descriptor documentation
This project follows the constraints and style expected in the 42 curriculum. The implementation is intentionally written in C and focuses on Unix process management, pipes, command execution and file descriptor redirection.
Christian Gómez
Junior Software Developer in training at 42 Barcelona
GitHub: github.com/chgomez04