Created
September 24, 2019 07:50
-
-
Save guanhui07/99f8520568ea1fb971723e025d6dba02 to your computer and use it in GitHub Desktop.
wait.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/wait.h> | |
#include <unistd.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
int main() | |
{ | |
pid_t pid; | |
char *message; | |
int n; | |
int exit_code; | |
printf("fork program starting\n"); | |
pid = fork(); | |
switch (pid) { | |
case -1: | |
perror("fork failed"); | |
exit(1); | |
case 0: | |
message = "This is the child"; | |
n = 5; | |
exit_code = 37; | |
break; | |
default: | |
message = "This is the parent"; | |
n = 3; | |
exit_code = 0; | |
break; | |
} | |
for ( ; n > 0; n--) { | |
puts(message); | |
sleep(1); | |
} | |
if (pid != 0) { | |
int stat_val; | |
pid_t child_pid; | |
child_pid = wait(&stat_val); | |
printf("Child has finished: PID = %d\n", child_pid); | |
if (WIFEXITED(stat_val)) { | |
printf("Child exited with code %d\n", WEXITSTATUS(stat_val)); | |
} else { | |
printf("Child terminated abnormally\n"); | |
} | |
} | |
exit(exit_code); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment