后台进程终止前追赶进程id [英] background process catching process id before termination

查看:210
本文介绍了后台进程终止前追赶进程id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图仿效背景和前景过程C.虽然这样做,如果有一个'和;'在结束符号,我避免在等待父进程的子进程。我还保存在我的列表执行所有后台命令,并尝试当他们完成从列表中删除。但是,在LS的情况下,-l和放大器;输出立即显示并只需pressing进入进程终止。如何赶上进程ID,如果匹配从我的列表中删除与列表中现有的PID。

I am trying to emulate background and foreground process in C. While doing that if there is a '&' symbol in end, I am avoiding waiting for that child process in parent process. I am also storing all background commands I execute in a list and try to remove them from the list when they are done. But in case of ls -l& the output is shown immediately and by just pressing enter the process terminates. How to catch that process id and remove from my list if it matches with the existing pids in the list.

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

希望我是清楚的。请提问,如果有任何疑问

Hope I am clear. Please ask questions if any doubt

推荐答案

通常情况下,你会使用的 waitpid函数() 在这样一个循环:

Normally, you'd use waitpid() in a loop like:

int status;
pid_t corpse;

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

当没有更多的尸体收集这些收集起来的已经死亡的孩子,但回报。选项​​0表示任何死去的孩子在我的进程组';另一种选择-1表示在所有的任何死去的孩子。在其他情况下,您可以指定一个特定的PID,看是否孩子已经死了,还是一个负数将与PGID等于绝对值的进程组中指定任何孩子。

This gathers up any children that have died, but returns when there are no more corpses to collect. The option 0 means 'any dead children in my process group'; an alternative option -1 means 'any dead children at all'. In other circumstances, you might specify a specific PID to see if that child has died, or a negative number would specify any child in the process group with the PGID equal to the absolute value.

该WNOHANG表示不挂在等待一个孩子,如果目前有没有死人尸体死';用0表示等待,直到周围的相应类别的孩子没有死',但通话将返回时,有没有这样的留守儿童。

The WNOHANG means 'do not hang waiting for a child to die if there are no bodies currently dead'; using 0 means 'wait around until a child of the appropriate category does die', though the call will return when there are no such children left.

如果有进程组中有多个孩子,有没有关于中,尸体将被退回,只是因为没有关于这孩子会死顺序保证顺序保证。

If there are multiple children in the process group, there's no guarantee about the order in which the corpses will be returned, just as there's no guarantee about the order in which the children will die.

这是不完全清楚你的要求是什么。您可以选择的最后一个参数 waitpid函数()根据是否推出最后一个流水线是在后台运行,例如。如果previously在后台启动的进程,你可以收集它的尸体几乎在任何时候(除非你在等待一个不同的进程组,或者比背景PID一个具体PID等)。你可能会选择第一个参数 waitpid函数()不同视情况而定。

It isn't entirely clear what your requirements are. You might choose the last argument to waitpid() based on whether the last pipeline launched was run in the background, for example. If you previously launched a process in the background, you might collect its corpse at almost any time (unless you're waiting for a different process group, or a specific PID other than that background PID). You might well choose the first argument to waitpid() differently depending on the circumstances.

这篇关于后台进程终止前追赶进程id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆