实现在C壳

扫码查看
本文介绍了实现在C壳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯目前实施C.壳
我的问题就出现了,当我尝试运行这样的命令:

  SHELL $:排序< txtfile注册表| grep的关键

进出口运行排序< txtfile注册表在一个进程(子),并在父即如果别人(PID大于0) IM正在运行的其他命令的权利该管道。

该程序运行正常,但其退出无限循环,我设置了主让接收来自用户的输入。

我怎么能解决这个问题?

这是code我迄今对付管,我没有包括code,我必须处理重定向:

C2P是管我的设置这一点。

 如果(PID == 0)
{
  如果(PIPE_FLAG)
    {
        关闭(C2P [0]);
        如果(dup2(C2P [1],STDOUT_FILENO)== - 1){
            PERROR(dup2()失败);
            出口(2);
        }
    }    / *执行命令* /
    execvp(cmd_args [0],cmd_args);
    PERROR(EXEC失败1); / *只返回时的exec失败* /
    出口(-1);}
否则如果(PID大于0)
{
  如果(PIPE_FLAG)
    {
        关闭(C2P [1]);
        如果(dup2(C2P [0],STDIN_FILENO)== - 1){
            PERROR(dup2()失败);
            出口(-1);
        }
        execvp(nxt_args [0],nxt_args);
        PERROR(EXEC失败2);
        出口(-1);
    }
}
其他
{
    /* 发生了错误 */
    PERROR(叉失败);
    出口(1);
}


解决方案

What happens to your shell process, then? The parent process is the shell. By running the right-side command in the parent process you're having it take over the shell's process. Remember that exec() replaces the current process.

You'll need to fork() twice, and execute the two sides of the pipe in the child processes. The parent must remain the shell, which will then wait() for the children to exit before presenting the next command prompt.

这篇关于实现在C壳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:52
查看更多