我正在编写一个假外壳,在其中创建一个子进程,然后调用 execvp()。在普通 shell 中,当我输入未知命令(例如“hello”)时,它会返回“hello: Command not found”。但是,当我将 hello 传递给 execvp() 时,默认情况下它不会返回任何错误,而是继续运行我的程序的其余部分,就像什么都没发生一样。找出是否实际上没有运行的最简单方法是什么?这是我的代码:
if(fork() == 0)
{
execvp(cmd, args);
}
else
{
int status = 0;
int corpse = wait(&status);
printf(Child %d exited with a status of %d\n", corpse, status);
}
我知道如果 corpse < 0,那么它是一个未知命令,但是我的代码中还有其他条件未列出,我不想等待(例如,如果在命令末尾输入 &)。有什么建议么?