Skip to content

Instantly share code, notes, and snippets.

@ammarfaizi2
Last active October 12, 2018 06:31
Show Gist options
  • Save ammarfaizi2/206b22a83ef766b64edea307b8e8a5ba to your computer and use it in GitHub Desktop.
Save ammarfaizi2/206b22a83ef766b64edea307b8e8a5ba to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
int main(int argc, char const *argv[])
{
int pid;
for (int i = 0; i < 5; i++) {
pid = fork();
if (pid == 0)
{
printf("I am child %d\n", i);
printf("Child %d is sleeping for 7 seconds...\n", i);
printf("Let wait child %d with WNOHANG\n", i);
sleep(7);
printf("Child %d has finished its execution\nExiting child %d\n", i, i);
if (i == 4)
{
printf("Now check up the process tree while the parent is sleeping!\n");
}
exit(0);
}
sleep(1);
int status;
waitpid(pid, &status, WNOHANG);
printf("pid: %d\n", status);
printf("wexitstatus: %d\n\n", WEXITSTATUS(status));
}
printf("I am the parent\n");
printf("The parent is now sleeping for 100 seconds\n\n");
sleep(100);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment