Skip to content

Instantly share code, notes, and snippets.

@nampereira
Last active February 5, 2018 11:53
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/56b6e97ff02a8b0e1624725912630601 to your computer and use it in GitHub Desktop.
Save nampereira/56b6e97ff02a8b0e1624725912630601 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>
void main()
{
int i;
int status;
for (i = 0; i < 2; i++)
if (fork() == 0) { /* Child */
sleep(i+1);
exit(i+1);
}
for (i = 0; i < 2; i++) { /* Parent */
pid_t wpid = wait(&status);
if (WIFEXITED(status))
printf("Child with PID %d terminated with exit status %d\n", wpid, WEXITSTATUS(status));
else
printf("Child with PID %d terminated abnormally\n", wpid);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment