Skip to content

The goal of this project is the re-implementation of the PIPE that we already know and use.

Notifications You must be signed in to change notification settings

Matsadura/pipex

Repository files navigation

pipex 🛠️ Unix Pipeline Emulator

Project Description

Core Purpose

Recreate shell piping behavior (cmd1 | cmd2) with input/output redirection from files. Handle command execution and process management like:

< infile cmd1 | cmd2 > outfile

Key Technical Concepts

  • Process management: Forking and process hierarchy
  • Inter-process communication: Pipe system calls
  • File descriptor manipulation: dup2/close operations
  • PATH resolution: Finding executable binaries

Why This Project Matters

Pipex provides crucial understanding of:

  • Unix process creation and management
  • Real-world pipe/redirection mechanics
  • System programming fundamentals
  • Error handling in concurrent execution

Learning Objectives 🎯

Key Programming Concepts

  1. Process Forking:
    pid = fork();
    if (pid == -1)
        error_exit("Fork failed");
  2. Pipe Management:
    int fd[2];
    pipe(fd);  // fd[0] read end, fd[1] write end
  3. Redirection Mechanics:
    dup2(input_fd, STDIN_FILENO);
    dup2(output_fd, STDOUT_FILENO);
  4. Command Execution:
    execve("/bin/ls", args, envp);

Skills Developed

  • System call error handling
  • Resource management (file descriptors)
  • Understanding Unix process trees
  • PATH environment variable manipulation

Technical Standards

/* Typical process structure */
pid_t pid = fork();
if (pid == 0)
{ /* Child process */
    handle_io_redirection();
    execute_command();
}
waitpid(pid, &status, 0);
  • 42 Requirements:
    • Handle absolute vs relative paths
    • Support environment variables
    • No memory leaks
    • Program name must be pipex
  • Error Cases:
    • Invalid arguments
    • Command not found
    • Permission denied
    • Pipe/fork failures

Algorithm Overview 🔄

Execution Flow

graph TD
    A[Parse Arguments] --> B[Open Input File]
    B --> C[Create Pipe]
    C --> D[Fork Process 1]
    D --> E[Execute cmd1]
    C --> F[Fork Process 2]
    F --> G[Execute cmd2]
    G --> H[Wait for Children]
    H --> I[Close File Descriptors]
Loading

Shell Comparison

Shell Command pipex Equivalent
ls -l pipe wc -l ./pipex infile "ls -l" "wc -l" outfile
< input grep foo > output ./pipex input "grep foo" "cat" output

Installation & Usage 🛠️

  1. Compilation

    make  # Creates pipex executable
  2. Basic Usage

    ./pipex infile "cmd1 args" "cmd2 args" outfile
  3. Test Commands

    # Compare with shell
    ./pipex infile "grep 'foo'" "wc -w" outfile
    < infile grep 'foo' | wc -w > outfile
    
    # Test error handling
    ./pipex invalid.txt "invalidcmd" "ls" outfile
  4. Bonus Features (Optional)

    # Support multiple pipes
    ./pipex infile "cmd1" "cmd2" "cmd3" ... outfile
    
    # Here document support
    ./pipex here_doc LIMITER "cmd" "cmd1" outfile

Error Handling 🚨

Mandatory Error Messages:

// Example error management
void error_exit(char *msg) {
    perror(msg);
    exit(EXIT_FAILURE);
}
  • Handle all system call errors (fork, pipe, open)
  • Detect command not found (ENOENT)
  • Verify file permissions
  • Prevent zombie processes with proper waiting

About

The goal of this project is the re-implementation of the PIPE that we already know and use.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors