0

我在 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);
    }


  }
4

1 回答 1

0

pipe(fds);在你打电话之前,你似乎错过了一个电话fork()。你也错过了一些close()电话。如果将管道的一端(使用dup()dup2())复制到标准输入或标准输出,则应关闭来自 的两个原始描述符pipe()

还有一个强有力的论点是您应该检查系统调用的返回值。你可以使用这样的函数:

#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>

extern void err_exit(const char *fmt, ...);

void err_exit(const char *fmt, ...)
{
    int errnum = errno;
    va_list args;
    va_start(args, fmt);
    vfprintf(stderr, fmt, args);
    va_end(args);
    if (errnum != 0)
        fprintf(stderr, " (%d: %s)", errnum, strerror(errnum));
    fputc('\n', stderr);
    exit(1);
}

你可以像这样使用它:

if (dup2(fds[2], 1) != 0)
    err_exit("dup2(%d, %d) failed", fds[2], 1);
于 2012-11-17T06:26:42.827 回答