Skip to content

Instantly share code, notes, and snippets.

@ictlyh
Created November 30, 2016 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ictlyh/bf999d2d16435c397a5d3d5f455f241e to your computer and use it in GitHub Desktop.
Save ictlyh/bf999d2d16435c397a5d3d5f455f241e to your computer and use it in GitHub Desktop.
Demo of creating zombie process.
/*
* Demo of creating zombie process.
* Build: gcc test-zombie.c -o test-zombie
* Run: ./test-zombie
*/
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
int main()
{
pid_t pid;
pid = fork();
if (pid < 0)
{
perror("fork error:");
exit(1);
}
else if (pid == 0)
{
printf("I am child process.I am exiting.\n");
exit(0);
}
printf("I am father process.I will sleep two seconds\n");
//等待子进程先退出
sleep(2);
//输出进程信息
system("ps -o pid,ppid,state,tty,command");
printf("father process is exiting.\n");
return 0;
}
@ictlyh
Copy link
Author

ictlyh commented Nov 30, 2016

Program output:

I am father process.I will sleep two seconds
I am child process.I am exiting.
PID PPID S TT COMMAND
98698 98697 S pts/0 -bash
107301 98698 S pts/0 ./test-zombie.out
107302 107301 Z pts/0 [test-zombie.out] ---------zombie process here
107303 107301 R pts/0 ps -o pid,ppid,state,tty,command
father process is exiting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment