chained pipes in linux using pipe()

时光毁灭记忆、已成空白 提交于 2019-12-12 18:03:57

问题


I know how to create one pipe in Linux with C that looks like cat /tmp/txt |grep foo, but I have problems implementing multiple chained pipes like this cat /tmp/1.txt | uniq -c | sort. How to do it with C using pipe() in Linux?

UPDATE: I've figured it out. Here's the code if anybody ever have the same question:

enum PIPES {
    READ, WRITE
};


 int filedes[2];
    int filedes2[2];
    pipe(filedes);
    pipe(filedes2); 

    pid_t pid = fork();
    if (pid == 0) {
        dup2(filedes[WRITE], 1);
    char *argv[] = {"cat", "/tmp/1.txt", NULL};
        execv("/bin/cat", argv); 
        exit(0);
    } 
    else {
        close(filedes[1]);
    }

    pid = fork();
    if (pid == 0) {
    dup2(filedes[READ], 0);
    dup2(filedes2[WRITE], 1);
        char *argv[] = {"uniq", "-c", NULL};
        execv("/usr/bin/uniq", argv);       
    }
    else {
        close(filedes2[1]);
    }

    pid = fork();
    if (pid == 0) {
        dup2(filedes2[READ], 0);
        char *argv1[] = {"sort", NULL};
            execv("/usr/bin/sort", argv1);
    }

    waitpid(pid);

回答1:


A pipe has two ends (read and write) and pipe() accordingly puts two file descriptors in the array you specify. The first one is the read end, and the second one the write end.

So, in your example, you would create two pipes and:

  • connect the standard output of cat to the write end of the first pipe,
  • connect the standard input of uniq to the read end of the first pipe,
  • connect the standard output of uniq to the write end of the second pipe,
  • connect the standard input of sort to the read end of the second pipe.



回答2:


You get a pair of file descriptors - let's call them a and b. A subsequent call to pipe gives you file descriptors c and d.

So you connect b to c in order to chain those two processes. Create yet another pair of file descriptors and connect d to e, for example, and so on.



来源:https://stackoverflow.com/questions/8300301/chained-pipes-in-linux-using-pipe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!