<分区>
对于一项任务,我们需要实现一个带有重定向和管道的基本 shell。我的管道代码退出程序并且不再要求用户输入。管道是在一个函数中执行的,所以基本上控制权不会返回给main。我如何让它这样做?这是我的代码:
void execute_pipe(char **argv,char **args)
{
int pfds[2];
pid_t pid;
int status;
pipe(pfds);
if ((pid = fork()) < 0) {
printf("*** ERROR: forking child process failed\n");
exit(1);
}
printf("here");
if (pid==0) {
close(1);
dup(pfds[1]);
close(pfds[0]);
if(execvp(argv[0],argv)<0){
printf("**error in exec");
}
}
else {
while (wait(&status) != pid) ;
close(0);
dup(pfds[0]);
close(pfds[1]);
if(execvp(args[0],args)<0){
printf("**error in exec");
}
}
}