在c中为shell创建后台进程

时间:2013-12-06 20:02:47

标签: c linux bash shell exec

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

void call_exec(char *cmd) 
{
pid_t cpid;
is_Background();
if(index(cmd, '/') == NULL) {
    int i;
printf("cmd is %s\n", cmd);
cpid = fork();
if(cpid == 0) {
    i = execvp(cmd, my_argv);
    if(i < 0) {
        printf("%s: %s\n", 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&amp;”时在后台运行gedit,它工作正常,gedit窗口打开,shell立即提示我输入新命令而无需等待关闭gedit窗口。问题是,在我使用“&amp;”之后任何命令只有一次,shell永远不会等待任何前台进程结束/关闭。例如,如果我输入“gedit”或“firefox”而没有“&amp;” ,shell不会等待它们关闭。

我希望我能够正确解释我的问题,我的英语不是那么好,所以对不起错误。如果我需要提供更多信息,请告诉我。感谢。

1 个答案:

答案 0 :(得分:2)

这里有两个问题:

首先,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)调用会等待任何进程关闭,而不一定是最后一个进程。在你的shell中,你可能会看到这个:

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)等待正确的过程。

相关问题