Skip to content

Commit f6a22fd

Browse files
committed
Add example of child status
1 parent 05a35e9 commit f6a22fd

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

fork_this_shit/Makefile

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11

22
# Add all targets here
3-
all: simple_fork
3+
BINARIES=simple_fork child_status
4+
5+
all: $(BINARIES)
46

57

6-
# A simple fork example
78
simple_fork: simple_fork.o
89
gcc -o simple_fork simple_fork.o
9-
1010
simple_fork.o: simple_fork.c
1111
gcc simple_fork.c -c
1212

1313

14+
child_status: child_status.o
15+
gcc -o child_status child_status.o
16+
child_status.o: child_status.c
17+
gcc child_status.c -c
18+
19+
1420
.PHONY: clean
1521
clean:
16-
rm -f *.o simple_fork
22+
rm -f *.o $(BINARIES)

fork_this_shit/child_status.c

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <sys/types.h>
2+
#include <sys/wait.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <unistd.h>
6+
7+
int main(int argc, char *argv[])
8+
{
9+
pid_t pid;
10+
pid = fork();
11+
12+
/* child gets a pid of zero */
13+
if (pid == 0) {
14+
printf("%d: we're in the child\n", getpid());
15+
exit(2);
16+
} else {
17+
pid = wait(NULL);
18+
printf("%d: we're in the parent\n", getpid());
19+
printf("%d: my child is: %d\n", getpid(), pid);
20+
printf("%d: my child is dead now and wait returns %d\n",
21+
getpid(), wait(NULL));
22+
}
23+
24+
return 0;
25+
}

0 commit comments

Comments
 (0)