我在 c 中实现 shell 时做了这个,但是在实现管道时,我想出了这个错误 ls:write error:Bad file descriptor ,我不明白为什么会出现这个错误,请帮忙。
#include<stdio.h>
#include<unistd.h>
#include <stdlib.h>
void execute(char **argv)
{
pid_t pid;
int status;
//fflush(0);
pid_t id ;
int out=0,in=0,pip=0;
int fds[2];
if ((pid = fork()) < 0) { /* fork a child process */
printf("*** ERROR: forking child process failed\n");
exit(1);
}
if (pid==0) {
close(1); /* close normal stdout */
dup2(fds[1],1); /* make stdout same as pfds[1] */
close(fds[0]); /* we don't need this */
execlp(argv[0],argv[0],argv[1],NULL);
}
else {
close(0); /* close normal stdin */
dup2(fds[0],0); /* make stdin same as pfds[0] */
close(fds[1]); /* we don't need this */
execlp(argv[3],argv[3],argv[4], NULL);
}
}