Created
March 31, 2013 15:39
-
-
Save vtols/5281036 to your computer and use it in GitHub Desktop.
This file contains 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 <unistd.h> | |
#include <string.h> | |
#define READ_END 0 | |
#define WRITE_END 1 | |
char buf[256]; | |
char text[] = "hello!!\n"; | |
int main() | |
{ | |
pid_t pid; | |
int in_pipe[2], out_pipe[2]; | |
//Create pipe for input and output | |
pipe(in_pipe); | |
pipe(out_pipe); | |
//Fork | |
pid = fork(); | |
if (pid == 0) { | |
//Child | |
//Close unused ends | |
close(in_pipe[WRITE_END]); | |
close(out_pipe[READ_END]); | |
dup2(in_pipe[READ_END], STDIN_FILENO); | |
dup2(out_pipe[WRITE_END], STDOUT_FILENO); | |
//Execute command | |
execlp("tr", "tr", "[a-z]", "[A-Z]", NULL); | |
} else { | |
//Parent | |
//Write to child input | |
write(in_pipe[WRITE_END], text, strlen(text)); | |
close(in_pipe[WRITE_END]); | |
//Wait child process | |
wait(NULL); | |
//Read from child ouput | |
read(out_pipe[READ_END], buf, sizeof(buf)); | |
close(out_pipe[READ_END]); | |
printf("%s", buf); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment