【发布时间】:2013-02-28 21:33:33
【问题描述】:
我正在 linux 中创建一个简单的 shell,但我无法让命令在后台运行。
到目前为止,这是我的代码:
create command,argv and check if it is to be ran in the background &
enter this code:
if(strcmp(command,"cd")==0)
{
chdir(argv[1]);
}
else if(strcmp(command,"clr") == 0)
{
if ((pid = fork()) == 0)
{
execvp("clear",argv);
}
wait(&status);
}
else
{
if ((pid = fork()) == 0)
{
execvp( prog, argv );
}
wait(&status);
}
command 和 argv 是我从用户那里得到的。
如果 '&' 是命令的最后一部分,我需要在后台运行命令。我已经用 bg 的布尔变量检查了这一点。但我在使用 WAITPID() 函数时遇到问题,我不确定它的去向。
【问题讨论】:
-
你看过waitpid(2)手册页中的例子吗?
-
waitpid在接受用户的一行输入之前进入 shell 的读取循环。 -
所以你说它应该看起来像这样
code while(true) cout<<"enter command"<<endl; waitpid(,,); getline(cin,command); parse command then my if statements?