C/C++ program to kill all child processes of a parent process in UNIX?

11,000

Solution 1

There is no standard Unix API to retrieve the list of child processes of a given process.

You can list all the processes on the system with their parent process, and build the process tree from that. The portable way to do this is to run the command

ps -e -o ppid= -o pid=

and parse the output (popen followed by a loop of scanf("%ld %ld\n") will do). Store the data as a finite map from PPID to PID, then walk the tree to collect the list of descendants of the process you're interested in.

If any of the processes concerned forks or exits during your processing, you may miss it. If you're unlucky, a process may exit and its PID may get reused while you're doing all this. Also, if process P forks a child process Q which forks a grandchild R, and then Q exits, R's PPID will be set to 1, so you won't detect that R was originally a descendant of P.

In a nutshell: whatever your problem is, this is very probably the wrong approach.

Unix has a feature to deal with this: process groups. There's a good chance that process groups are the answer to the problem you're trying to solve. You can atomically send signal signal_number to all the processes in a process group with kill(-pgid, signal_number).

You should arrange for all the processes you want to kill to belong to the same process group, and for the processes you don't want to kill not to belong to that process group. Make the parent process call setsid or setpgid, then kill the process group.

Solution 2

I would simply store all the child pids when fork(), in an array of pid_t in the parent process.

after then, kill all by looping that array.

pid_t all_child[10]; // you should not define a fixed length array, you can use pointer.
if((pid = fork()) == 0)
{
     //child processes
}
else
{
//parent process
all_pid[counter] = pid;
}

When killing,

for( i = 0; i < counter; i++)
{
kill(all_pid[i], SIGTERM);
}
Share:
11,000
user3253461
Author by

user3253461

Updated on June 05, 2022

Comments

  • user3253461
    user3253461 almost 2 years

    I have a function which takes input parameter as PID. e.g .

    bool KillProcessTree (int ParentPID)
    {
    
    }
    

    Now I want to write the definition of the above function, which will 1st get all the child processes and then kill them.

    Is there any API in Unix which will take parent PID and will return the number of child processes created by the parent process?

  • WGH
    WGH about 10 years
    It only works if your code runs as the parent process. The question didn't include that assumption.
  • pmverma
    pmverma about 10 years
    @WGH of course I agree with you, In the case of killing all child from parent, I answer. but I am not sure if there is a function which will collect all the child process pids by just giving a parent pid. I am glad to hear that.
  • WGH
    WGH about 10 years
    There is none. You can possibly loop through OS-dependent tables, such as procfs on Linux, to find children, but, again, that's not portable and suspectible to race conditions.
  • user3253461
    user3253461 about 10 years
    Thanks that's really helpful answer, but with popen() and the cmd "ps -e -o ppid= -o pid=" will i be able to get all child and grand child processes as well?
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 10 years
    @user3253461 You get all the processes on the system, and you need to filter that list to extract the ones you want.
  • lindhe
    lindhe over 7 years
    I'm not familiar with signum. Just shorthand for "signal number", or is it some fancy signal I can not find in man signal.h?
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 7 years
    @lindhe Just shorthand for “signal number”.