diff options
| author | Manuel Traut <manut@linutronix.de> | 2019-01-09 17:30:15 +0100 |
|---|---|---|
| committer | John Ogness <john.ogness@linutronix.de> | 2019-01-28 19:48:41 +0106 |
| commit | 5b9736bf793623ed5e95cde3a3e6017399ecd095 (patch) | |
| tree | 13ceeeb76aa9eac1079677ec59f8e8b8b20434ab | |
| parent | 5dfa2823a0ee23303c9e1dd7e38833e3e6e01396 (diff) | |
add a zombie example
To show that processes become zombies if you don't wait.
Also show that init becomes the father of your zombies if the original father
dies.
Signed-off-by: Manuel Traut <manut@linutronix.de>
| -rw-r--r-- | schulung_tools/zombie/Makefile | 8 | ||||
| -rw-r--r-- | schulung_tools/zombie/README | 2 | ||||
| -rw-r--r-- | schulung_tools/zombie/zombie.c | 41 |
3 files changed, 51 insertions, 0 deletions
diff --git a/schulung_tools/zombie/Makefile b/schulung_tools/zombie/Makefile new file mode 100644 index 0000000..58c8196 --- /dev/null +++ b/schulung_tools/zombie/Makefile @@ -0,0 +1,8 @@ +CFLAGS += -g + +zombie: zombie.c + +clean: + rm -f zombie + +.PHONY: clean diff --git a/schulung_tools/zombie/README b/schulung_tools/zombie/README new file mode 100644 index 0000000..8c8561a --- /dev/null +++ b/schulung_tools/zombie/README @@ -0,0 +1,2 @@ +This program demonstrates that children processes will become zombies if +they exit but the parent does not clean them up. diff --git a/schulung_tools/zombie/zombie.c b/schulung_tools/zombie/zombie.c new file mode 100644 index 0000000..cc5e145 --- /dev/null +++ b/schulung_tools/zombie/zombie.c @@ -0,0 +1,41 @@ +#include <stdio.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/wait.h> + +int main(void) +{ + pid_t pid; + int i; + + for (i = 0; i < 10; i++) { + pid = fork(); + if (pid == -1) { + printf("fork() failed\n"); + return 0; + } else if (pid == 0) { + printf("child: pid=%d\n", getpid()); + sleep(10); + printf("child: exiting pid=%d\n", getpid()); + return 0; + } else { + printf("parent: created child %d\n", pid); + } + } + +/* if we don't clean up the children, they are zombies */ +#if 0 + while (wait(&i) != -1) + /* NOP */; +#endif + +/* if we don't clean up the children and the main process terminates + * init becomes parent of the childs + */ +#if 0 + while (1) + sleep(100); +#endif + + return 0; +} |
