管道、分支和执行程序的链接,bash 类似于 C 中的行为

Chaining of pipes, forks and execs, bash like behaviour in C

我正在尝试模拟这个 bash 命令:ls -la | cut -c 20-30 | grep -v echelinho 在 C 中使用 forks、pipes 和 execs,这是我目前拥有的代码,在执行后什么都不显示:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>

int main (int argc , char**argv){

    int **pipes;
    int i;
    int pid1, pid2, pid3;

    pipes = malloc(2*sizeof(int*));
    for(i=0;i<3;i++){
        pipes[i] = malloc (2*sizeof(int));
        pipe(pipes[i]);
    }

    pid1=fork();
    if(!pid1){
        close(pipes[0][0]);
        dup2(pipes[0][1],1);
        execlp("ls","ls","-la",NULL);
    }

    pid2=fork();
    if(!pid2){
        waitpid(pid1, NULL, 0);
        close(pipes[0][1]);
        close(pipes[1][0]);
        dup2(pipes[0][0],0);
        dup2(pipes[1][1],1);
        execlp("cut","cut","-c", "20-30", NULL);
    }

    pid3=fork();
    if(!pid3){
        waitpid(pid2, NULL, 0);
        close(pipes[1][1]);
        dup2(pipes[1][0],0);
        execlp("grep","grep","-v", "echelinho", NULL);
    }
}

有人可以指出我的错误或让这段代码正常工作吗?我知道我不是错误处理,这不是养成的好习惯,但我这样做只是为了更好地理解概念,而不是为了 pratical/real 世界应用程序。

我自己设法改正了错误。

两个影响最大的错误是:

  • 不关闭涉及的每个进程的部分管道;
  • 等待子进程结束的代码位置有误。

固定代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>

int main (int argc , char**argv){

    int fdfst[2];
    int fdsnd[2];

    pipe(fdfst);
    pipe(fdsnd);

    switch(fork()){
        case -1: 
            perror("Error on fork.");
        case 0:
            close(fdfst[0]);
            close(fdsnd[0]);
            close(fdsnd[1]);
            dup2(fdfst[1],1);
            close(fdfst[1]);
            execlp("ls","ls","-la",NULL);
    }

    switch(fork()){
        case -1:
            perror("Error on fork.");
        case 0:
            close(fdfst[1]);
            close(fdsnd[0]);
            dup2(fdfst[0],0);
            close(fdfst[0]);
            dup2(fdsnd[1],1);
            close(fdsnd[1]);
            execlp("cut","cut","-c", "20-30", NULL);
    }

    switch(fork()){
        case -1:
            perror("Error on fork.");
        case 0:
            close(fdfst[0]);
            close(fdfst[1]);
            close(fdsnd[1]);
            dup2(fdsnd[0],0);
            close(fdsnd[0]);
            execlp("grep","grep","-v", "echelinho", NULL);
    }

    close(fdfst[0]);
    close(fdfst[1]);
    close(fdsnd[0]);
    close(fdsnd[1]);

    wait(NULL);
    wait(NULL);
    wait(NULL);

    exit(EXIT_SUCCESS);
}