Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- void print_usage_info(const char *program);
- int main(int argc, char **argv)
- {
- if (argc != 2 + 1)
- print_usage_info(argv[0]);
- int pip[2][2]; /* Pipe 1: program1 -> program2 */
- /* Pipe 2: program1 <- program2 */
- for (int i = 0; i < 2; ++i)
- pipe(pip[i]);
- /* Original process will execute 1st program, while other will take care about 2nd */
- if (fork()) {
- close(pip[0][0]);
- close(pip[1][1]);
- dup2(pip[1][0], 0);
- dup2(pip[0][1], 1);
- execl(argv[1], argv[1], NULL);
- } else {
- close(pip[0][1]);
- close(pip[1][0]);
- dup2(pip[0][0], 0);
- dup2(pip[1][1], 1);
- execl(argv[2], argv[2], NULL);
- }
- fprintf(stderr, "How did I got here: file %s, line %d\n", __FILE__, __LINE__);
- return -1;
- }
- void print_usage_info(const char *program)
- {
- fprintf(stderr, "%s: incorrect arguments. Usage:\n%s program1 program2\n", program, program);
- exit(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement