简体   繁体   English

在C中在后台运行进程

[英]running a process in background in c

I want to run a child process in background from main function in c. 我想从C中的主要功能在后台运行子进程。 I have used fork and execv functions to do so. 我已经使用fork和execv函数来做到这一点。 But I also want to kill the child background process, at the end of the parent process, in case the child process has not exited yet. 但是我也想在父进程结束时杀死子后台进程,以防子进程尚未退出。 I will be using kill(pChildPid) function to do so. 我将使用kill(pChildPid)函数来执行此操作。 So my question is 所以我的问题是

Suppose the child process has exited before the parent process, can linux OS can allocated the same pid as that of child to some other process? 假设子进程在父进程之前退出,Linux OS能否将与子进程相同的pid分配给其他进程? If yes, then I will be unintentionally killing that process? 如果是,那么我将无意中杀死这个过程?

Yes, in theory, it can, and yes you can. 是的,从理论上讲,它可以,是的,你可以。

However, if you OWN the process, it will be a "zombie" process until your waitpid or a similar function has received the message that the process died [unless the forked process uses detach to disconnect from the owning process]. 但是,如果您拥有该进程,那么它将是一个“僵尸”进程,直到您的waitpid或类似函数收到进程死亡的消息[除非分叉进程使用detach断开与拥有进程的连接]。

To demonstrate: 展示:

#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
#include <iostream>

int main()
{
    int pid = fork();
    if (pid)
    {
        int p = 0;
        int status;
        sleep(60);
        p = wait(&status);
        std::cout << "Pid " << p << " exited..." << std::endl;
    }
    else
    {
        for(int i = 0; i < 20; i++)
        {
            std::cout << "Child is sleeping..." << std::endl;
            sleep(1);
        }
        std::cout << "Child exited..." << std::endl;
    }
    return 0;
}

If you run this, and use ps a to view the processes, you'll see a.out twice, with PID's close to each other. 如果你运行它,并使用ps a来查看进程,你会看到a.out两次,其中PID彼此接近。 Once it prints child exited , you'll see that the status of the second process goes to something like: 一旦它打印了child exited ,您将看到第二个进程的状态变为:

12362 pts/0    Z+     0:00 [a.out] <defunct>

Here Z means that it's a "zombie" process. Z表示这是一个“僵尸”进程。 When the 60 seconds of the first process is over, it then disappears. 当第一个过程的60秒结束时,它就会消失。

If you fork, you must also do a wait for your child. 如果你分叉,你也必须wait你的孩子。 If the child exited before you do this, it will stay as a zombie. 如果孩子在你这样做之前就退出了,它会像僵尸一样留下来。 So if you want to kill the child and you have NOT waited for it's return status, it will stick around and the PID is not taken. 因此,如果您想杀死孩子并且没有等待它的返回状态,它将一直存在并且不使用PID。 In this case you can safely kill it, even though it wouldn't have an effect anymore, if it is not running anymore. 在这种情况下,即使它不再运行,即使它不再起作用,您也可以安全地将其杀死。 If you do a wait, and later on send the kill, it could happen that the PID was taken by another process in the meantime, if you don't look at the status of your child. 如果您等待,然后发送终止请求,则可能是因为如果您不查看孩子的状态,则PID被其他进程占用。

如果子进程退出,它将变成一个僵尸进程 - 死进程仍然在运行进程的表中,因此其父进程可以检索其退出代码。

Yes, the PIDs are incremented and eventually cycle back to zero - so a process can exit and have another running at a later time with it's PID. 是的,PID会增加,并最终循环回到零-因此一个进程可以退出并在稍后的时间使用它的PID运行另一个进程。 But you can always check it's PPID to make sure your it's parent. 但是您始终可以检查它的PPID以确保它是父项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM