Skip to content

Commit 078b68f

Browse files
committed
Add an example of zombie process
1 parent ba73932 commit 078b68f

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ course in Bits, Goa.
1010
- Fork this shit: examples of forking
1111
- Kernel Modules: kernel modules
1212
- Process communication: process communication
13+
- Zombie: make a zombie
1314

1415
## Author
1516
Utkarsh Maheshwari

zombie/Makefile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
# Add all targets here
3+
BINARIES=zombie
4+
5+
all: $(BINARIES)
6+
7+
8+
zombie: zombie.o
9+
gcc -o zombie zombie.o
10+
zombie.o: zombie.c
11+
gcc zombie.c -c
12+
13+
14+
.PHONY: clean
15+
clean:
16+
rm -f *.o $(BINARIES)

zombie/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Zombie
2+
Makes a zombie process
3+
4+
#### Compile:
5+
- all:
6+
```bash
7+
make all
8+
```
9+
10+
- selectively, 'package':
11+
```bash
12+
make package
13+
```

zombie/zombie.c

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <sys/types.h>
2+
#include <sys/wait.h>
3+
#include <stdio.h>
4+
#include <unistd.h>
5+
6+
int main(int argc, char *argv[])
7+
{
8+
pid_t pid, me;
9+
pid = fork();
10+
11+
if (pid == 0) {
12+
me = getpid();
13+
printf("%d: I love eating brains\n", me);
14+
printf("%d: I am a zombie!\n", me);
15+
} else {
16+
me = getpid();
17+
18+
printf("%d: Killing my child\n", me);
19+
printf("%d: Damn! He's a zombie now!\n", me);
20+
21+
/* sleep so that my child becomes a zombie */
22+
sleep(20);
23+
wait(NULL);
24+
25+
printf("%d: Not anymore\n", me);
26+
}
27+
28+
return 0;
29+
}

0 commit comments

Comments
 (0)