Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中等待后台作业_C_Shell_Background_Jobs - Fatal编程技术网

在C中等待后台作业

在C中等待后台作业,c,shell,background,jobs,C,Shell,Background,Jobs,我正在尝试实现自己的shell,我希望将暂停的后台作业置于其运行状态,但我希望等待该作业完成,而不是后台作业 到目前为止,我可以得到,这继续工作,但不等到它完成 以下是我的过程a命令函数: pid_t pid=fork(); if (pid==0) // child { ... if(strcmp(command->name, "myfg") == 0){ FILE *sp; char proc[100];

我正在尝试实现自己的shell,我希望将暂停的后台作业置于其运行状态,但我希望等待该作业完成,而不是后台作业

到目前为止,我可以得到,这继续工作,但不等到它完成

以下是我的过程a命令函数:

pid_t pid=fork();
if (pid==0) // child
{
      ...
      if(strcmp(command->name, "myfg") == 0){

            FILE *sp;
            char  proc[100];
            char *pid_comm = command->args[0];

            int PID = atoi(pid_comm);

            sprintf(proc, "/bin/kill -CONT %s", pid_comm);
            sp = popen(proc, "r");
            if (sp == NULL) {
                printf("Failed to run kill\n");
                exit(1);
            }
            pclose(sp);

            int status;
            waitpid(PID, &status, 0);
            // to check if it is waiting
            printf("%d\n", PID);

        } 
         ...
 }
 else
 {
    if (!command->background)
        wait(0); // wait for child process to finish
    return SUCCESS;
 }
我用
sleep 200&
测试了这个命令,这个作业有自己的PID,我可以从作业列表中得到它。但是,我不能让程序等待


我的命令看起来像:
myfg

您在子进程中杀死了什么?你杀了父进程?如果是这样的话,你的孩子就会变成一个僵尸进程。您在子进程中等待什么?kill-CONT使一个暂停的进程继续?kill-CONT所涉及的进程已被:kill-stop停止?你确定他处于停止状态吗?哦,是的,当然,我有另一个命令是“暂停”,它叫kill-STOP。我可以成功地停止和恢复命令。请小心使用POSIX。不要忽略
wait()
返回的值;你需要
int状态;int carbody=等待(&status)然后测试尸体(死去孩子的PID)及其退出状态。请注意,使用
wait()
,无法保证尸体将属于最近启动的孩子;可能还有其他在后台运行的进程首先完成。您在子进程中杀死了什么?你杀了父进程?如果是这样的话,你的孩子就会变成一个僵尸进程。您在子进程中等待什么?kill-CONT使一个暂停的进程继续?kill-CONT所涉及的进程已被:kill-stop停止?你确定他处于停止状态吗?哦,是的,当然,我有另一个命令是“暂停”,它叫kill-STOP。我可以成功地停止和恢复命令。请小心使用POSIX。不要忽略
wait()
返回的值;你需要
int状态;int carbody=等待(&status)然后测试尸体(死去孩子的PID)及其退出状态。请注意,使用
wait()
,无法保证尸体将属于最近启动的孩子;在后台运行的其他进程可能会首先完成。