Reputation: 181
int main() {
pid_t pid;
printf("Parent: %d\n", getpid());
pid = fork();
if (pid == -1) {
perror("fork");
goto clean_up;
} else if (pid > 0) {
sleep(3);
} else {
printf("Child Parent: %d\n", getppid());
printf("Child: %d\n", getpid());
printf("Exiting...\n");
}
clean_up:
return 0;
}
I wanted to create zombie process on purpose (of course for experimenting/learning). After the child exits, the parent doesn't wait()
s for the child. So, I'm expecting zombie to show up, in ps -ef | grep zombie.o
. But for some reason it is not showing up. What could be the possible reasons?
Upvotes: 1
Views: 45
Reputation: 57922
When the parent exits, all its children (alive or zombie) are assigned PID 1 as their new parent. See the _exit(2)
man page: " any children of the process are inherited by process 1".
PID 1 is normally the init
daemon, and if it's working properly then it should always wait()
for its children. So zombie children will be reaped immediately, and children that are still running will be reaped as soon as they exit.
If you want to create a long-lived zombie, the parent needs to remain alive but not calling wait()
.
Upvotes: 1