如何在 exec 期间处理 C shell 程序中的输入
我目前正在编写自己的 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;
}
}
} 〜
谢谢,
I am currently writing my own shell program. This simple shell can just execute commands.
When executing commands like vi or calc which require input from the terminal , the command is getting executed and is waiting for the input from the user. But I am unable to give any input on the screen.
How should the input be handled during the fork and exec.
Here is the piece of code which is executing commands:
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;
}
}
}
~
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WNOHANG
导致父进程不等待,因此(取决于平台)子进程将与终端 IO 分离或死亡。删除
WNOHANG
。The
WNOHANG
is causing the parent process not to wait and therefore (depending on platform) the child process will be detached from terminal IO or die.Remove the
WNOHANG
.