c - 后台进程在终止前捕获进程 ID

标签 c shell unix

我正在尝试在 C 中模拟后台和前台进程。如果最后有一个“&”符号,我会避免在父进程中等待该子进程。我还将我执行的所有后台命令存储在一个列表中,并在完成后尝试将它们从列表中删除。但是在 ls -l& 的情况下,输出会立即显示,只需按回车键,过程就会终止。如果它与列表中的现有 pid 匹配,如何捕获该进程 ID 并从我的列表中删除。

pid = fork();
//Code to add pid into a list if it is a background process
//this is done by parent as pid is always 0 for child processes
if(pid == 0){
    if(proc_state=='&'){
        setsid();           
    }
        // logic for creating command
        int ret= execvp( subcomm[0], subcomm );
    // handling error
}

//Parent will execute this
//Child will never come here if execvp executed successfully

if(proc_sate != '&'){
        for(i=0; i < count_pipe+1; i++){            
            int ret = waitpid(0, &flag ,0);
        // Code to remove procid from list if 'ret' matches with existing procid in the list.
        }
}

//Here proc_state just determines whether it is background or foreground.It is just a character. count_pipe is just a 
//variable holding number of pipes

希望我说清楚了。如有疑问请提问

最佳答案

通常,您会使用 waitpid()在一个循环中:

int status;
pid_t corpse;

while ((corpse = waitpid(0, &status, WNOHANG)) != -1)
{
    /* Process PID of child that died with given status */
    ...
}

这会收集所有死去的 child ,但当没有更多尸体可收集时返回。选项 0 表示“我的进程组中任何死掉的 child ”;另一个选项 -1 表示“任何死去的 child ”。在其他情况下,您可以指定一个特定的 PID 来查看该子进程是否已经死亡,或者一个负数将指定进程组中 PGID 等于绝对值的任何子进程。

WNOHANG 的意思是“如果当前没有尸体死亡,不要挂起等待 child 死亡”;使用 0 意味着“等到适当类别的 child 死亡”,尽管当没有这样的 child 离开时调用将返回。

如果进程组中有多个子进程,则无法保证返回尸体的顺序,就像无法保证子进程死亡的顺序一样。

目前还不完全清楚您的要求是什么。例如,您可以根据最后启动的管道是否在后台运行来选择 waitpid() 的最后一个参数。如果您之前在后台启动了一个进程,您几乎可以随时收集它的尸体(除非您正在等待一个不同的进程组,或者一个不同于该后台 PID 的特定 PID)。您可能会根据情况选择不同的 waitpid() 第一个参数。

关于c - 后台进程在终止前捕获进程 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18804486/

相关文章:

c - 'const char *' 类型的变量不能分配给 'char' 类型的实体

c - 在 AT 命令字符串中使用变量值?

regex - 这个awk语句是什么意思?

linux - 找到无限系列的总和

c - c 中局部变量的默认值是垃圾值,对吗?那为什么总是得到相同的输出呢?

c - UDP 广播在 Linux 2.6.33.9 rt 上一段时间后中断

linux - KSH - Linux - ps -ef - 返回代码与找到的进程数

linux - Bash:解析 CSV 并编辑单元格值

bash - "declare +x"与 "declare -x"是什么意思?

unix - 如何将证书 CN 名称定义为变量