3

I want to run a child process in background from main function in c. I have used fork and execv functions to do so. 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. 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? If yes, then I will be unintentionally killing that process?

quartz
  • 747
  • 9
  • 26
  • Read about it here: [http://stackoverflow.com/questions/3446727/how-does-linux-determine-the-next-pid](http://stackoverflow.com/questions/3446727/how-does-linux-determine-the-next-pid) It is nice explanation. – Dusan Plavak Sep 20 '13 at 12:27

4 Answers4

5

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].

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. Once it prints child exited, you'll see that the status of the second process goes to something like:

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

Here Z means that it's a "zombie" process. When the 60 seconds of the first process is over, it then disappears.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
2

If you fork, you must also do a wait for your child. 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. 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.

Devolus
  • 21,661
  • 13
  • 66
  • 113
1

If child process exited it becomes a zombie process - dead process which is still in the table of running processes, so its parent process can retrieve its exit code.

Johny
  • 1,947
  • 13
  • 23
0

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. But you can always check it's PPID to make sure your it's parent.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54