Skip to content

Instantly share code, notes, and snippets.

@nampereira
Last active February 5, 2018 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nampereira/fa7eb706b9a8d5ed150e4f23301d5187 to your computer and use it in GitHub Desktop.
Save nampereira/fa7eb706b9a8d5ed150e4f23301d5187 to your computer and use it in GitHub Desktop.
#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