summaryrefslogtreecommitdiff
path: root/schulung_tools/zombie/zombie.c
blob: cc5e145eac1f35da71f6cff5c687677a96f6f9fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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;
}