Operating system 在c中实现多管道 我尝试在C++中实现多个管道。也就是说,我想编写一个程序来模拟执行,例如ls-l | head-n10 | wc-l

Operating system 在c中实现多管道 我尝试在C++中实现多个管道。也就是说,我想编写一个程序来模拟执行,例如ls-l | head-n10 | wc-l,operating-system,pipe,Operating System,Pipe,代码运行良好。但是在所有命令都正确执行之后,我需要按enter键返回命令行。我想我得在什么地方“等着” 这是我现在的代码 using namespace std; #include <stdio.h> #include <iostream> #include <unistd.h> #include <fcntl.h> #include <string.h> int main(){ int pid; int fd[4]; pipe(

代码运行良好。但是在所有命令都正确执行之后,我需要按enter键返回命令行。我想我得在什么地方“等着”

这是我现在的代码

using namespace std; 
#include <stdio.h>
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
int main(){

int pid;
int fd[4];

pipe(fd + 0); // pipe between the 1st and 2nd command
pipe(fd + 2); // pipe between the 2nd and 3rd command

for( int i = 0; i < 3; i++){ // 3 commands

    pid = fork();

    if(pid == 0){// child process

        if( i == 0 ){// first command

            char *arg[10];
            arg[0] = "ls";
            arg[1] = NULL;

            close(fd[0]);
            dup2(fd[1], 1);
            execvp(arg[0], arg);
        }
        else if( i == 1){// second command

            char *arg[10];
            arg[0] = "head";
            arg[1] = "-n1";
            arg[2] = NULL;

            dup2(fd[0], 0);
            dup2(fd[3], 1);
            execvp(arg[0], arg);
        }
        else if( i== 2){// third command

            char *arg[10];
            arg[0] = "wc";
            arg[1] = "-l";
            arg[2] = NULL;

            close(fd[3]);
            dup2(fd[2], 0);
            execvp(arg[0], arg);

        }

    }

    else{// parent
    }

}

}
使用名称空间std;
#包括
#包括
#包括
#包括
#包括
int main(){
int-pid;
int-fd[4];
管道(fd+0);//第一个和第二个命令之间的管道
管道(fd+2);//第二个和第三个命令之间的管道
对于(int i=0;i<3;i++){//3命令
pid=fork();
如果(pid==0){//子进程
如果(i==0){//第一个命令
char*arg[10];
arg[0]=“ls”;
arg[1]=NULL;
关闭(fd[0]);
dup2(fd[1],1);
execvp(arg[0],arg);
}
如果(i==1){//第二个命令
char*arg[10];
arg[0]=“头”;
arg[1]=“-n1”;
arg[2]=NULL;
dup2(fd[0],0);
dup2(fd[3],1);
execvp(arg[0],arg);
}
如果(i==2){//第三个命令
char*arg[10];
arg[0]=“wc”;
arg[1]=“-l”;
arg[2]=NULL;
关闭(fd[3]);
dup2(fd[2],0);
execvp(arg[0],arg);
}
}
else{//parent
}
}
}
我想我已经浏览了所有类似的帖子,但仍然无法理解这一点

有人能帮忙吗