Shell Pipe Implementation单级

时间:2015-10-11 20:57:21

标签: c++ shell

我正在编写我的shell,这是一段代码。任何人都可以解释为什么我没有达到以下功能的最后,即OUT没有打印。我已阅读shell相关的文章,但他们似乎没有帮助。 commandargs()函数返回格式化的字符串,即删除空格和存储参数。我的两个晚上只是浪费在做调试。

它工作正常并且输出正确但是在此功能之后程序执行停止。

int shellpipe(char **arg)
{
//  pid_t childpid;
  int status;
  int pfd[2];
  pid_t cpid;
  char c;
  char **p=commandargs(arg[0]);
  char **q=commandargs(arg[1]);
cout<<"q0";//not printed

  if ( pipe(pfd) < 0){
    perror("pipe");
    return 1;
  }

  cpid = fork();

  if( cpid == 0 )
  {
    /* CHILD 1*/
    close(0);
    dup2(pfd[0], 0);
    close(pfd[1]);
    if (execvp(q[0],q)==-1)
            perror("Executing Error");


    exit(0);
  } else if ( cpid > 0){
    /* PARENT */
    close(1);
    dup2(pfd[1], 1);
    close(pfd[0]);
    if (execvp(p[0],p)==-1)
            perror("Executing Error");
    close(pfd[1]);
    close(1);
    wait(&status);

  }else{
    /* ERROR */
    perror("fork");
    return 1;
  }
  cout<<"Out";//control don't reach here

}

1 个答案:

答案 0 :(得分:0)

“打印”未打印,因为输出流已关闭。也可能是子进程永远不会完成,但这需要仔细查看参数字符串。

相关问题