Skip to content
Advertisement

Kill child process spawned with execl without making it zombie

I need to spawn a long-running child process and then kill it from the parent code. At the moment I do like this:

int PID = fork();
if (PID == 0) {
  execl("myexec", "myexec", nullptr);
  perror("ERROR");
  exit(1);
}
// do something in between and then:
kill(PID, SIGKILL);

This does the job in the sense that the child process is stopped, but then it remains as a zombie. I tried to completely remove it by adding:

kill(getpid(), SIGCHLD);

to no avail. I must be doing something wrong but I can’t figure out what, so any help would be greatly appreciated. Thanks.

Advertisement

Answer

signal(SIGCHLD, SIG_IGN);
kill(getpid(), SIGCHLD);

Presto. No zombie.

By ignoring SIGCHLD we tell the kernel we don’t care about exit codes so the zombies just go away immediately.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement