Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在exec期间处理C shell程序中的输入_C_Unix_Exec_Fork - Fatal编程技术网

如何在exec期间处理C shell程序中的输入

如何在exec期间处理C shell程序中的输入,c,unix,exec,fork,C,Unix,Exec,Fork,我目前正在编写自己的shell程序。这个简单的shell只能执行命令 当执行需要从终端输入的vi或calc等命令时,该命令正在执行并等待用户的输入。但我无法在屏幕上提供任何输入 在fork和exec期间应该如何处理输入 下面是执行命令的一段代码: if((pid = fork()) < 0) { perror("Fork failed"); exit(errno); } if(pid == 0) {

我目前正在编写自己的shell程序。这个简单的shell只能执行命令

当执行需要从终端输入的vi或calc等命令时,该命令正在执行并等待用户的输入。但我无法在屏幕上提供任何输入

在fork和exec期间应该如何处理输入

下面是执行命令的一段代码:

    if((pid = fork()) < 0)
    {
            perror("Fork failed");
            exit(errno);
    }
    if(pid == 0)
    {
            // Child process
            if(execvp(arguments[0], arguments) == -1)
            {
                    child_status = errno;
                    switch(child_status)
                    {
                            case ENOENT:
                                    printf(" command not found \n");
                                    break;
                    }
                    exit(errno);
            }
    }
    else
    {
            // parent process
            int wait_stat;
            if(waitpid(pid , &wait_stat, WNOHANG) == -1)
            {
                    printf(" waitpid failed \n");
                    return;
            }
    }
if((pid=fork())<0)
{
perror(“Fork失败”);
出口(errno);
}
如果(pid==0)
{
//子进程
if(execvp(参数[0],参数)=-1)
{
child_status=errno;
开关(儿童状态)
{
案例说明:
printf(“未找到命令\n”);
打破
}
出口(errno);
}
}
其他的
{
//父进程
int wait_stat;
如果(等待pid(pid和等待状态,WNOHANG)=-1)
{
printf(“waitpid失败\n”);
返回;
}
}
} ~


谢谢,

WNOHANG导致父进程不等待,因此(取决于平台)子进程将从终端IO分离或死亡


移除
WNOHANG

WNOHANG
导致父进程不等待,因此(取决于平台)子进程将从终端IO分离或死亡

卸下
WNOHANG