Sticky
Sticky

Reputation: 1082

Killing and reaping process

I have a child process specified by pid. This process could be:

  1. Running
  2. Defunct/Zombie (unreaped)
  3. Already reaped (and thus no longer exists)

I would like to kill this process and ensure no zombie remains. Currently my code is

kill(pid, SIGKILL);
int temp;
waitpid(pid, &temp, 0); 

Would this work?

EDIT: The process specified by pid is a child of my program.

Upvotes: 2

Views: 2892

Answers (2)

Jens
Jens

Reputation: 72657

This looks fine so far, but I wonder why you would let case 3 happen. You should perform some bookkeeping, which of your child processes have terminated and are waiting to be reaped. One way would be to install a handler for SIGCHLD, setting a flag that a waitpid is in order. That way you guarantee that all pids are actually those of your child processes.

Upvotes: 2

G. Martinek
G. Martinek

Reputation: 276

Should work fine, but be sure to check the returnvalue of waitpid. The call may have returned due to a signal.

Upvotes: 2

Related Questions