Skip to content
nuunnuunPublic

About

A 42 Bangkok C project implementing a UNIX pipeline with pipe, fork, dup2, and execve.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This project has been created as part of the 42 curriculum by kraksana.

pipex

My implementation of pipex for 42 Bangkok.

The goal of this project is to reproduce the behavior of a simple UNIX pipeline using C system calls such as pipe(), fork(), dup2(), and execve().

Overview

pipex takes an input file, two commands, and an output file:

./pipex infile "cmd1" "cmd2" outfile

It behaves like the following shell command:

< infile cmd1 | cmd2 > outfile

The program creates two child processes and connects them through a pipe.

  • The first command reads from the input file.
  • Its output is sent into the pipe.
  • The second command reads from the pipe.
  • Its output is written to the output file.
  • Executables are searched using the PATH environment variable.
  • The final exit status is taken from the second command.

What I Practiced

Through this project, I practiced working with:

  • UNIX processes with fork()
  • Pipes and inter-process communication
  • File descriptors
  • Input and output redirection with dup2()
  • Running programs with execve()
  • Environment variables and PATH
  • Process status with waitpid()
  • File handling with open()
  • Error handling and exit codes
  • Parsing command arguments and quoted strings

Build

Compile the project with:

make

This creates the pipex executable.

Usage

Example:

printf "hello\nworld\n" > infile

./pipex infile "cat" "wc -l" outfile

cat outfile

Output:

2

You can compare the result with the shell:

< infile cat | wc -l > expected
cmp outfile expected

Makefile Commands

make

Compiles the project.

make clean

Removes object files.

make fclean

Removes object files and the executable.

make re

Rebuilds the project from scratch.

Project Structure

pipex/
├── Makefile
├── README.md
├── include/
│   └── pipex.h
└── src/
    ├── main.c
    ├── pipeline.c
    ├── process.c
    ├── execute.c
    ├── path.c
    ├── split_command.c
    ├── split_utils.c
    ├── string_utils.c
    ├── memory.c
    └── error.c

Project Status

Mandatory part completed.

The implementation supports a two-command pipeline with input/output redirection, PATH resolution, command parsing, error handling, and exit status propagation.

The project compiles with:

-Wall -Wextra -Werror

and passes Norminette.

Resources

  • man 2 pipe
  • man 2 fork
  • man 2 dup2
  • man 2 execve
  • man 2 waitpid
  • man 2 open
  • Linux man-pages
  • POSIX documentation

AI Usage

AI was used to help review edge cases, organise tests, and explain UNIX process and file descriptor concepts. All code and behavior were reviewed and tested before finalising the project.

Author

Kullatida Raksanaves — nuunnuun

About

A 42 Bangkok C project implementing a UNIX pipeline with pipe, fork, dup2, and execve.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages