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

I have a function that takes an input parameter as a PID. eg,

bool KillProcessTree (int ParentPID)
{

}

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

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

+3
source share
2 answers

There is no standard Unix API for retrieving the list of child processes of this process.

You can list all the processes in the system with their parent process and build a process tree from this. A portable way to do this is to run a command

ps -e -o ppid= -o pid=

(popen, scanf("%ld %ld\n")). PPID PID, , .

- , , . , , PID , . , P Q, R, Q , R PPID 1, , R P.

: , , , .

Unix : . , , . signal_number kill(-pgid, signal_number).

, , , , , . setsid setpgid, .

+3

pids, fork(), pid_t .

, , .

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;
}

for( i = 0; i < counter; i++)
{
kill(all_pid[i], SIGTERM);
}
-1

All Articles