}
static int fork_bomb(int argc, char* argv[]) {
+ int status = 0;
+
// Fork this process
pid_t pid = fork();
+ // Catch any errors
+ if (pid < 0) {
+ fprintf(stderr, "Could not fork(): %m\n");
+ exit(EXIT_FAILURE);
+
// Parent
- if (pid) {
+ } else if (pid) {
printf("Forked child process with PID %d\n", pid);
// Wait until the child process went away
- waitpid(pid, NULL, 0);
+ waitpid(pid, &status, 0);
- return 0;
+ // Pass on the exit code
+ return WEXITSTATUS(status);
// Child
} else {
// Fork again...
return fork_bomb(0, NULL);
}
-
- return 0;
}
static int lines(int argc, char* argv[]) {