|0.1.4||Sep 20, 2020|
|0.1.3||Sep 20, 2020|
|0.1.2||Sep 20, 2020|
|0.1.1||Sep 20, 2020|
|0.1.0||Sep 20, 2020|
#292 in Unix APIs
unix_exec_piper: A library written in Rust that execs multiple commands and connects them with pipes.
My library basically does the functionality that happens when you type
cat file.txt | grep -i | wc -l
into a shell like bash. unix_exec_piper does no parsing, but only the actual execution and connection
between the (child) processes via unix pipes.
Important main parts of the library are
lib.rs :: execute_piped_cmd_chain().
The main purpose of this library is educational and to show people who are interested in this how it's done.
You might build a shell around this library (if it gets more functionality in the future).
Please make yourself familiar with the
file descriptorsand "Everything is a file"
- Creating pipes between processes where
STDOUTof one process gets connected to
STDINof the next process.
$ cat file.txt | grep -i | wc -l)
- I/O redirection into files
$ cat < file.txt | grep -i | wc -l > out.txt
- I/O redirection with
- a lot of other redirection primitives listed here: https://tldp.org/LDP/abs/html/io-redirection.html
The parent process loops
n times (for
n commands) and creates
n child processes
are created through
fork(). Each child process has two variables in its address space:
let mut pipe_to_current: Option<Pipe>; let mut pipe_to_next: Option<Pipe>;
Pipes communicates across process boundaries in the following way:
child process 0 child process 1 child process n _______________ _______________ _________ | cat foo.txt | | grep -i abc | | wc -l | --------------- --------------- --------- ^ ^ ^ ^ WRITE |--------| R / W |--------| READ END E E END (current child) -Pipe to Current- -Pipe to Next-
Each process uses
pipe_to_current (if present) as "read end" (as it's
pipe_to_current (if present) as "write end"
file descriptor into the write end of the pipe).