I am interested in creating a zombie process. To my understanding, zombie process happens when the parent process exits before the children process. However, I tried to recreate the zombie process using the following code:
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main ()
{
pid_t child_pid;
child_pid = fork ();
if (child_pid > 0) {
exit(0);
}
else {
sleep(100);
exit (0);
}
return 0;
}
However, this code exits right after execute which is expected. However, as I do
ps aux | grep a.out
I found a.out is just running as a normal process, rather than a zombie process as I expected.
The OS I am using is ubuntu 14.04 64 bit
Quoting:
This is wrong. According to
man 2 wait
(see NOTES) :So, if you want to create a zombie process, after the
fork(2)
, the child-process shouldexit()
, and the parent-process shouldsleep()
before exiting, giving you time to observe the output ofps(1)
.For instance, you can use the code below instead of yours, and use
ps(1)
whilesleep()
ing: