Here's An Example Of How To Pipe Three Commands Together Using C ...
Maybe your like
Instantly share code, notes, and snippets.
- Download ZIP
- Star (44) You must be signed in to star a gist
- Fork (3) You must be signed in to fork a gist
- Embed Select an option
- Embed Embed this gist in your website.
- Share Copy sharable link for this gist.
- Clone via HTTPS Clone using the web URL.
No results found
Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/mplewis/5279108.js"></script> - Save mplewis/5279108 to your computer and use it in GitHub Desktop.
- Embed Embed this gist in your website.
- Share Copy sharable link for this gist.
- Clone via HTTPS Clone using the web URL.
No results found
Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/mplewis/5279108.js"></script> Save mplewis/5279108 to your computer and use it in GitHub Desktop. Download ZIP Here's an example of how to pipe three commands together using C. This one uses `ps aux | grep root | grep sbin`. This topic is horribly documented online so hopefully this'll help someone else out. Raw threePipeDemo.c This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters| // This program is an example of how to run a command such as |
| // ps aux | grep root | grep sbin |
| // using C and Unix. |
| #include <stdlib.h> |
| #include <unistd.h> |
| int pid; |
| int pipe1[2]; |
| int pipe2[2]; |
| void main() { |
| // create pipe1 |
| if (pipe(pipe1) == -1) { |
| perror("bad pipe1"); |
| exit(1); |
| } |
| // fork (ps aux) |
| if ((pid = fork()) == -1) { |
| perror("bad fork1"); |
| exit(1); |
| } else if (pid == 0) { |
| // stdin --> ps --> pipe1 |
| exec1(); |
| } |
| // parent |
| // create pipe2 |
| if (pipe(pipe2) == -1) { |
| perror("bad pipe2"); |
| exit(1); |
| } |
| // fork (grep root) |
| if ((pid = fork()) == -1) { |
| perror("bad fork2"); |
| exit(1); |
| } else if (pid == 0) { |
| // pipe1 --> grep --> pipe2 |
| exec2(); |
| } |
| // parent |
| // close unused fds |
| close(pipe1[0]); |
| close(pipe1[1]); |
| // fork (grep sbin) |
| if ((pid = fork()) == -1) { |
| perror("bad fork3"); |
| exit(1); |
| } else if (pid == 0) { |
| // pipe2 --> grep --> stdout |
| exec3(); |
| } |
| // parent |
| } |
| void exec1() { |
| // input from stdin (already done) |
| // output to pipe1 |
| dup2(pipe1[1], 1); |
| // close fds |
| close(pipe1[0]); |
| close(pipe1[1]); |
| // exec |
| execlp("ps", "ps", "aux", NULL); |
| // exec didn't work, exit |
| perror("bad exec ps"); |
| _exit(1); |
| } |
| void exec2() { |
| // input from pipe1 |
| dup2(pipe1[0], 0); |
| // output to pipe2 |
| dup2(pipe2[1], 1); |
| // close fds |
| close(pipe1[0]); |
| close(pipe1[1]); |
| close(pipe2[0]); |
| close(pipe2[1]); |
| // exec |
| execlp("grep", "grep", "root", NULL); |
| // exec didn't work, exit |
| perror("bad exec grep root"); |
| _exit(1); |
| } |
| void exec3() { |
| // input from pipe2 |
| dup2(pipe2[0], 0); |
| // output to stdout (already done) |
| // close fds |
| close(pipe2[0]); |
| close(pipe2[1]); |
| // exec |
| execlp("grep", "grep", "sbin", NULL); |
| // exec didn't work, exit |
| perror("bad exec grep sbin"); |
| _exit(1); |
| } |
aidanrh7 commented Oct 20, 2017
Could you please explain why it is necessary to close the stdin and stdout files within the exec calls, meaning lines 70, 84, and 87. It seems as if these would need to be open to read from and write to, but I must be misunderstanding something. Thanks! the code is very helpful.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
twalenczyk commented Feb 13, 2018
aidanrh7, you raise a good question: Won't closing the pipe preemptively (i.e. in the child process) prevent the child processes from communicating? In short, the answer is no. I'm unfamiliar with the inner mechanisms of dup2; however, with regard to this program, it effectively makes the pipe access points in the child processes unnecessary. This is because the pipe resides in the kernel's address space. So, for example, when you redirect the standard output of ps in child process 1 using dup2, the pipe access points in the child's address space are no longer needed; the binary executed by execlp will direct its standard output to the write-end of the pipe.
It's worth noting that my understanding of this topic is limited; this is simply how it makes sense to me. Hopefully, someone more knowledgeable can come along and offer a proper explanation. I figured I'd offer a simple explanation that might be helpful to someone else who comes along wondering the same thing as you.
More about pipes in Linux: https://unix.stackexchange.com/questions/148401/how-pipes-work-in-linux More examples of redirection: http://www.cs.loyola.edu/~jglenn/702/S2005/Examples/dup2.html More about dup2: http://codewiki.wikidot.com/c:system-calls:dup2 Why you should close pipes: https://stackoverflow.com/questions/19265191/why-should-you-close-a-pipe-in-linux.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
drayarms commented Oct 12, 2018
@aidanrh7 The answer to your question is that inside each process, only one end of one pipe will be written to or read from. The side that will be written to/ read from is duped, everything else is closed. Closing simply means that the fd that was once associated with a given object(in this case the read or write end of a pipe), will be released so that it no longer points to that object in question. The end that was duped, will already have been assigned, either 0 (for stdin) or 1(for stdout), so that either 0 or 1 will be pointing to that end. Hence, the need to release the original fd that was pointing to the end in question. Note that instead of using dup2(), you could also use close() and dup() as and alternative So dup2(pipe2[0], 0); can also be achieved by close(0); dup(pipe2[0]);
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
bosdhill commented Oct 21, 2018
thz...
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
nskoro commented Dec 8, 2018
Appreciate the help, but not sure why you would post something that doesn't compile.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
msabwat commented Jan 5, 2021
Thank you, this helped a lot :)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
wilschmidtt commented Jan 17, 2021
Thank you for this, it really makes the concept simple and understandable
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
ElectricRCAircraftGuy commented Mar 15, 2022 • edited Loading Uh oh!
There was an error while loading. Please reload this page.
For additional help, an excellent example of pipes in C in Linux is here under the "EXAMPLES" section: https://man7.org/linux/man-pages/man2/pipe.2.html
Also, I can confirm that threePipeDemo.c does compile, contrary to what @nskoro says. Just make sure you do NOT use -Werror in your compile command, as there are a variety of warnings.
Here is my compile command. I tested this on Linux Ubuntu 18.04.
# compile mkdir -p bin && gcc -Wall -Wextra -O3 -std=c17 threePipeDemo.c -o bin/a # then run the program with: bin/aHere's my compile command and the output with a bunch of warnings:
eRCaGuy_hello_world/c/todo$ mkdir -p bin && gcc -Wall -Wextra -O3 -std=c17 threePipeDemo.c -o bin/a threePipeDemo.c:52:6: warning: return type of ‘main’ is not ‘int’ [-Wmain] void main() { ^~~~ threePipeDemo.c: In function ‘main’: threePipeDemo.c:56:5: warning: implicit declaration of function ‘perror’ [-Wimplicit-function-declaration] perror("bad pipe1"); ^~~~~~ threePipeDemo.c:66:5: warning: implicit declaration of function ‘exec1’; did you mean ‘execl’? [-Wimplicit-function-declaration] exec1(); ^~~~~ execl threePipeDemo.c:82:5: warning: implicit declaration of function ‘exec2’; did you mean ‘execl’? [-Wimplicit-function-declaration] exec2(); ^~~~~ execl threePipeDemo.c:96:5: warning: implicit declaration of function ‘exec3’; did you mean ‘execl’? [-Wimplicit-function-declaration] exec3(); ^~~~~ execl threePipeDemo.c: At top level: threePipeDemo.c:104:6: warning: conflicting types for ‘exec1’ void exec1() { ^~~~~ threePipeDemo.c:66:5: note: previous implicit declaration of ‘exec1’ was here exec1(); ^~~~~ threePipeDemo.c:118:6: warning: conflicting types for ‘exec2’ void exec2() { ^~~~~ threePipeDemo.c:82:5: note: previous implicit declaration of ‘exec2’ was here exec2(); ^~~~~ threePipeDemo.c:135:6: warning: conflicting types for ‘exec3’ void exec3() { ^~~~~ threePipeDemo.c:96:5: note: previous implicit declaration of ‘exec3’ was here exec3(); ^~~~~This is incredibly helpful for anyone trying to understand the super confusing execlp() cmd: I do not understand how execlp() works in Linux
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment You can’t perform that action at this time.Tag » How To Pipe Commands In C
-
Connecting N Commands With Pipes In A Shell? - Stack Overflow
-
6.2.2 Creating Pipes In C
-
Simulating The Pipe "|" Operator In C - YouTube
-
How To Use Pipe Function In C Language - Linux Hint
-
Example Of Using The PIPE Command (C) - IBM
-
Introduction To Pipes | C For Dummies Blog
-
C Tutorial: Pipes - CS 416 Documents
-
C Program To Demonstrate Fork() And Pipe() - GeeksforGeeks
-
Understanding Pipes In Unix With A Sample Implementation - Medium
-
Cannot Correctly Connect Multi-stage C Command Pipe (among ...
-
Implement Ls | Less In C | CSCI3150 - IPC-Pipe - CUHK CSE
-
[PDF] Assignment Two
-
Piping In Unix Or Linux - GeeksforGeeks
-
Exercises: Shell – CS 61 2020