File tree 2 files changed +35
-4
lines changed
2 files changed +35
-4
lines changed Original file line number Diff line number Diff line change 1
1
2
2
# Add all targets here
3
- all : simple_fork
3
+ BINARIES =simple_fork child_status
4
+
5
+ all : $(BINARIES )
4
6
5
7
6
- # A simple fork example
7
8
simple_fork : simple_fork.o
8
9
gcc -o simple_fork simple_fork.o
9
-
10
10
simple_fork.o : simple_fork.c
11
11
gcc simple_fork.c -c
12
12
13
13
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
+
14
20
.PHONY : clean
15
21
clean :
16
- rm -f * .o simple_fork
22
+ rm -f * .o $( BINARIES )
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments