Could someone suggest me an easy way to create a zombie process that cannot be reaped for a few minutes. The purpose of this is to test parent process for being able to reap zombies processes after they become reapable again.
One case for non-reapable zombies can be found here . I guess there might be easier ways to do so.
OS: Linux
Preferable languages: C/C++
Here's a little program that will cause a zombie to show up in your "ps aux" output for a few minutes. Note the commented-out waitpid() call; if you un-comment that call, the zombie will not show up, as the waitpid() call causes the parent process to claim its dead child.
(Disclaimer: I only tested this under MacOS/X since that's the computer I'm at, but it should work the same under Linux)
#include <stdio.h>
#include <signal.h>
#include <pthread.h>
#include <sys/wait.h>
#include <unistd.h>
int main()
{
printf("Starting Program!\n");
int pid = fork();
if (pid == 0)
{
printf("Child process %i is running!\n", getpid());
sleep(300); // wait 5 minutes
printf("Child process %i is exiting!\n", getpid());
return 0;
}
else if (pid > 0)
{
printf("Parent process is continuing child's pid is %i...\n", pid);
sleep(5); // just for clarity
printf("Parent process is sending SIGKILL to child process...\n");
if (kill(pid, SIGKILL) != 0) perror("kill");
// waitpid(pid, NULL, 0); // uncomment this to avoid zombie child process
printf("Parent process is sleeping... ps aux should show process #%i is a zombie now\n", pid);
sleep(500); // just for clarity
printf("Parent process is exiting!\n");
}
else perror("fork()");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.