在c中为shell创建一个后台进程



我试图用C制作自己的shell,但在处理后台和前台进程时遇到了问题。这是我创建流程的地方:

void call_exec(char *cmd) 
{
pid_t cpid;
is_Background();
if(index(cmd, '/') == NULL) {
    int i;
printf("cmd is %sn", cmd);
cpid = fork();
if(cpid == 0) {
    i = execvp(cmd, my_argv);
    if(i < 0) {
        printf("%s: %sn", cmd, "command not found");
        exit(1);        
    }   
} 
else {
    if(!is_BG ) {
        wait(NULL);
    }
    is_BG = 0;
}
}

is_Background:

void is_Background() {
if(strcmp(my_argv[arg_index], "&") == 0) {
    is_BG = 1;
    my_argv[arg_index] = NULL;
}
}

当我运行代码并在命令行中输入"gedit"时,shell会等待,直到我关闭gedit窗口,然后提示我输入新命令。当我输入"gedit&"在后台运行gedit时,它运行良好,gedit窗口打开,shell立即提示我输入新命令,而无需等待关闭gedit窗口。问题是,在我对任何命令只使用"&"一次之后,shell就不会等待任何前台进程结束/关闭。例如,如果我输入不带"&"的"gedit"或"firefox",shell不会等待它们关闭。

我希望我能正确地解释我的问题,我的英语不是很好,所以很抱歉犯了错误。如果我需要提供更多信息,请告诉我。谢谢。

这里有两个问题:

首先,gedit和firefox是单实例程序。任何其他调用都只会重用现有实例。你在bash中看到了同样的东西:

bash$ gedit &   # Starts gedit and returns immediately
bash$ gedit     # Opens a new tab in the existing window and returns immediately

您应该使用多个实例程序(如xtermxeyes)进行测试。

其次,您的wait(NULL)调用等待任何进程关闭,而不一定是最后一个进程。在你的壳里,你可能会看到这个:

yourshell$ xterm &  # starts xterms and returns immediately. 
# Now close the xterm before running the next command
yourshell$ xeyes    # starts xeyes, waits on xterm, returns immediately

您可以使用waitpid(cpid, NULL, 0)来等待正确的进程。

最新更新