Created
May 20, 2022 07:01
-
-
Save pwnwriter/405d96d6d6bde127ea5f74a9022da2d3 to your computer and use it in GitHub Desktop.
c program to demonstrate / simulating pipes in c .
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
//Demonstration of pipes in c | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/wait.h> | |
#include <unistd.h> | |
int main(int argc, char* argv){ | |
int fd[2]; | |
if(pipe(fd) == -1){ | |
return 1; | |
} | |
int pid1 = fork(); | |
if (pid1 < 0){ | |
return 2; | |
} | |
if (pid1 == 0){ | |
dup2(fd[1],STDOUT_FILENO); | |
close(fd[0]); | |
close(fd[1]); | |
execlp("ping","ping","-c","5","google.com",NULL); | |
} | |
int pid2 = fork(); | |
if(pid2<0){ | |
return 3; | |
} | |
if (pid2 == 0){ | |
dup2(fd[0], STDIN_FILENO); | |
close(fd[0]); | |
close(fd[1]); | |
execlp("grep","grep","rtt",NULL); | |
} | |
waitpid(pid1, NULL, 0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment