|
CS 456 - Systems Programming
Spring 2024
|
Displaying ./code/feb08/fork4.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main(int argc, char **argv){
pid_t child_id = fork();
if (child_id == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (child_id > 0) {
// We have a child! Get their exit code
int status;
int rt;
rt = waitpid( child_id, &status, 0 );
printf("waitpid return: %d\n", rt);
// code not shown to get exit status from child
} else { // In child ...
// start calculation
printf("In child: %d\n", child_id);
exit(123);
}
}
|