Last active
April 30, 2016 00:58
-
-
Save charles-l/167af725fd82ef3b664643661f0da508 to your computer and use it in GitHub Desktop.
Simple C program to send data through one end of a pipe, to a command, then read it back in.
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
| #include <stdio.h> | |
| #include <string.h> | |
| #include <unistd.h> | |
| #include <stdlib.h> | |
| // p pp | |
| // (input - sent to first pipe) | tr a b | (output - read back in) | |
| int main (void) { | |
| int p[2]; | |
| int pp[2]; | |
| pipe(p); | |
| pipe(pp); | |
| char buf[256]; | |
| if(fork() == 0) { | |
| if(fork() == 0) { | |
| close(pp[0]); | |
| close(pp[1]); | |
| close(p[0]); | |
| write(p[1], "staff", strlen("staff")); | |
| } else { | |
| close(pp[0]); | |
| close(p[1]); | |
| dup2(p[0], STDIN_FILENO); | |
| dup2(pp[1], STDOUT_FILENO); | |
| execl("/bin/sh", "sh", "-c", "tr a b", NULL); | |
| } | |
| } else { | |
| close(pp[1]); | |
| close(p[0]); | |
| close(p[1]); | |
| read(pp[0], buf, 32); | |
| printf("%s\n", buf); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment