实现在C壳 [英] implementing a shell in C

查看:159
本文介绍了实现在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);
}


解决方案

  

我跑排序< txtfile注册表的子进程,并在父我运行命令管道的权利。


恰好您的shell进程是什么呢?父进程是shell。通过在父进程中运行右侧命令你让它接管壳的过程。请记住,执行exec()替换当前的进程。

您需要到餐桌()两次,并在子进程执行管道的两侧。家长必须保持外壳,然后将wait()方法为孩子$ P $之前psenting下一个命令提示符退出。

im currently implementing a shell in C. My problem arises when i try to run a command like this:

SHELL$: sort < txtFile | grep key

im running sort < txtFile in a process (child), and in the parent i.e else if(pid > 0) im running the other command to the right of the pipe.

The program runs fine, but it exits the infinite loop that i set up in main to keep receiving input from the user.

How could i solve this problem?

this is the code i have so far to deal with the pipe, i didnt include the code that i have to deal with the redirects:

c2p is the pipe i setup for this.

if(pid == 0)
{
  if( PIPE_FLAG )
    {   
        close(c2p[0]);
        if(dup2(c2p[1], STDOUT_FILENO) == -1){
            perror("dup2() failed");
            exit(2);
        }
    }

    /* Execute command */
    execvp(cmd_args[0], cmd_args);
    perror("exec failed 1. ");          /* return only when exec fails */
    exit(-1);

} 
else if(pid > 0)
{
  if(PIPE_FLAG)
    {
        close(c2p[1]);
        if(dup2(c2p[0], STDIN_FILENO) == -1){
            perror("dup2() failed");
            exit(-1);
        }
        execvp(nxt_args[0], nxt_args);
        perror("exec failed 2. ");          
        exit(-1);    
    }
}
else 
{ 
    /* error occurred */
    perror("fork failed");
    exit(1);
}

解决方案

I'm running sort < txtFile in the child process, and in the parent I'm running the command to the right of the pipe.

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壳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆