-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfork-test.c
45 lines (41 loc) · 1.46 KB
/
fork-test.c
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
42
43
44
45
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <sys/wait.h>
#include <sys/types.h>
int
main(int argc, char *argv[]) {
pid_t cpid, w;
int status;
cpid = fork();
if (-1 == cpid) {
perror("fork");
exit(EXIT_FAILURE);
}
if (0 == cpid) {
printf("Child PID is %ld\n", (long)getpid());
if (argc == 1)
pause();
_exit(atoi(argv[1]));
} else {
do {
w = waitpid(cpid, &status, WUNTRACED | WCONTINUED);
if (-1 == w) {
perror("waitpid");
exit(EXIT_FAILURE);
}
if (WIFEXITED(status)) {
printf("exited, status=%d\n", WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
printf("killed by signal %d\n", WTERMSIG(status));
} else if (WIFSTOPPED(status)) {
printf("stopped by signal %d\n", WSTOPSIG(status));
} else if (WIFCONTINUED(status)) {
printf("continued\n");
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
exit(EXIT_SUCCESS);
}
return 0;
}