我正在 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() 函数时遇到问题,我不确定它的去向。