分娩后获得无限循环

时间:2017-10-05 18:24:38

标签: c

我正在用C构建一个终端,我不知道为什么在分娩后从文件描述符中读取流的无限循环

 void normal_excute(char** command, bool back){
   // intializing file descriptor
  int fds[2];
  pid_t pid;
  char buf[256];

  // opening pipes
  pipe(fds);
  // fork a child
  pid = fork();

  if(pid == 0) {
    // close read end for child
    close(fds[0]);
    // dupe write terminal with stdout
    dup2 (fds[1], STDOUT_FILENO);
    execv(command[0],command);
    exit(0);
  } else {
    // close read end for parent
    close(fds[1]);
    // opening stream on read end of pipe
    FILE* stream = fdopen(fds[0],"r") ;
    // reading from stream
    // infinite loop ------------------------------------------------
    while( fgets(buf,sizeof(buf) ,stream) !=NULL){}
    // -------------------------------------------------------------------
    close(fds[1]);
    waitpid(pid,NULL,0);
  } }

0 个答案:

没有答案