pipex takes the project aimed to better understand shell redirection and pipes, by handling them in C.
the program takes an input file, performs a command on it, pipes the result to another command which then writes its result to an output file.
program's arguments are processed the same as < infile cmd1 | cmd2 > outfile in shell.
the result copiesthe output of this shell command: $ < input_file command1 | command2 > output file
here are some resources i used:
• creating and killing child processes in c
• pipe: an inter-process communication method
• handling a file by its descriptor in c
• a comprehensive guide to pipex
# ./pipex infile cmd1 cmd2 outfile
pipe()
|
|-- fork()
|
|-- child // cmd1
: |--dup2()
: |--close end[0]
: |--execve(cmd1)
:
|-- parent // cmd2
|--dup2()
|--close end[1]
|--execve(cmd2)
# pipe() sends the output of the first execve() as input to the second execve()
# fork() runs two processes (i.e. two commands) in one single program
# dup2() swaps our files with stdin and stdout
-
clone this repository:
git clone [email protected]:idleira/pipex.git pipex && cd pipex
-
use
make
to compile the project -
run the program with shell commands in this order:
./pipex <infile> <cmd1> <cmd2> <outfile>