0
到目前为止,我有一个函数execute_command(command_t),可以执行简单的命令,如“ls -l”,“cat test.txt”等。但是,当我尝试实现管道,它不会产生任何东西。这里的代码:管道之间的2子进程不产生任何输出
int thePipe[2];
pid_t child = fork();
int status1=0;
pipe(thePipe);
if(child==(pid_t)-1){
fprintf(stderr, "error when fork the process\n");
} else if(child==0){
close one pipe and make duplicate
execute command[0]
} else{
waitpid(child, &status1, 0);
}
int status2=0;
pid_t child2 = fork();
pipe(thePipe);
if(child2==(pid_t)-1){
fprintf(stderr, "error when fork the process\n");
} else if(child2==0){
close one pipe and make duplicate
execute command[1];
} else {
waitpid(child2, &status2, 0);
}
如果输入命令是:
cat test.txt | grep aaa
命令 “猫的test.txt” 将被存储在 “C-> u.command [0]” 和命令“ grep aaa“将存储在”c-> u.command [1]“中。
我做错了什么?谢谢
这是因为你叉使用管道功能的前 – Alexis