Skip to content

vjan-nie/Pipex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pipex - 42 Project

A Unix IPC (Inter-Process Communication) project that replicates the shell pipe functionality using system calls.

Overview

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 > file2

How It Works

Architecture

The program uses two child processes that communicate via a pipe:

[INFILE] → dup2 → [CHILD_1: cmd1] → [PIPE] → [CHILD_2: cmd2] → dup2 → [OUTFILE]

Process Flow

  1. 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
  2. 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])
  3. 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])

Usage

./pipex <infile> <cmd1> <cmd2> <outfile>

Example

./pipex input.txt "cat" "wc -l" output.txt

This is equivalent to:

cat input.txt | wc -l > output.txt

Key System Calls

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

Project Structure

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

Key Functions

  • pipex(): Main orchestration function that forks and manages both child processes
  • ft_first_command(): Handles first child - reads from infile, writes to pipe
  • ft_second_command(): Handles second child - reads from pipe, writes to outfile
  • ft_check_path(): Locates command executable in PATH environment variable
  • ft_free_split(): Memory cleanup for allocated strings

Error Handling

  • 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

Exit Status

  • Returns the exit status of the second command
  • Exit code 1 on critical errors
  • Exit code 129 for pipe creation failure

About

A program that recreates the behaviour of the pipe in shell "|"

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors