nidHi
nidHi

Reputation: 833

Removing a zombie process

I have a use case where I have a process which is parent process and it spawns various child processes which monitors an ssh connection each. This parent process should run endlessly as a part of a service. But, when one of these ssh connection is closed, then that child process becomes a zombie process. This way there are many zombie processes being created each time a ssh connection is closed.

I want to keep the number of zombie processes as minimal as possible. As far as I know, SIGTERM or SIGKILL does remove a zombie process. Since the parent process runs endlessly the zombie process cannot be removed by waiting for the parent to terminate.

Is there any way to remove the zombie process? Or something that I have understood wrong?

Upvotes: 0

Views: 2762

Answers (1)

user5076313
user5076313

Reputation:

In C programming, You can use the waitpid system call in parent process.

In linux command line or shell script, you can use the wait command to remove the zombies.

waitpid manpage,

The waitpid() system call suspends execution of the calling process until a child specified by pid argument has changed state. By default, waitpid() waits only for terminated children, but this behavior is modifiable via the options arguments.

Syntax:-

pid_t waitpid(pid_t pid, int *status, int options);

If you don't want to pause the parent, then you have use the bellow system calling to clear the zombie process.

int status;
waitpid(0, &status, WNOHANG);

When the parent process does receives the child exit status, then only the zombie process is created. So, using the waitpid with WNOHANG option you can receive the child exit staus, If WNOHANG option is provided it will not block the parent. Is there any child is exited it store the exit status in the status variable, and return. Otherwise it will not wait, it simply return.

Try the waitpid man page to read about the waitpid system call.

Upvotes: 1

Related Questions