Last active
February 5, 2018 11:57
-
-
Save nampereira/fa7eb706b9a8d5ed150e4f23301d5187 to your computer and use it in GitHub Desktop.
This file contains 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 <stdio.h> | |
#include <stdlib.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
#include <wait.h> | |
#define N 4 | |
void main() | |
{ | |
pid_t pid[N]; | |
int i; | |
int status; | |
for (i = 0; i < N; i++) | |
if ((pid[i] = fork()) == 0) { /* Child */ | |
exit(i+1); | |
} | |
for (i = 0; i < N; i++) { /* Parent */ | |
if (pid[i] % 2 == 0) { | |
pid_t wpid = waitpid(pid[i], &status, 0); | |
if (WIFEXITED(status)) | |
printf("Child %d terminated with exit status %d\n",wpid, WEXITSTATUS(status)); | |
else | |
printf("Child %d terminated abnormally\n", wpid); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment