I'm trying to kill an entire process tree in C. I am able to do this, however when I run the code, it prints "Killed". I don't want this. Is there any way I can achieve the same result without printing "Killed"?
This is the code I am using:
#define _POSIX_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
int pid = fork();
if (pid == 0) {
sleep(2);
printf("child printf\n");
} else {
sleep(1);
kill(-getpid(), SIGKILL);
}
return EXIT_SUCCESS;
}
I am compiling this with gcc -std=c99 -Wall test.c
. When I run ./a.out
, this is the output:
Killed
If I change the sleep(1)
to a sleep(3)
, this is the output:
child printf
Killed
In both cases I want the Killed
line removed.
Note: In the real code, the child process will be execve
'd with another. There will be multiple child procceses. Because they can have children themself, doing kill(pid, SIGKILL)
won't work.