Skip to content

Instantly share code, notes, and snippets.

@quickgrid
Last active April 1, 2016 03:03
Show Gist options
  • Save quickgrid/a17e413b2773db535797 to your computer and use it in GitHub Desktop.
Save quickgrid/a17e413b2773db535797 to your computer and use it in GitHub Desktop.
This code is to create process in Linux and trace the flow
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
int main(int argc, char *argv[]){
printf("1.Process PID: %d\n", (int)getpid());
pid_t pid = fork();
printf("2.fork returned: %d\n", (int)pid);
if(pid == 0){
printf("3.Process PID: %d\n", (int)getpid());
pid_t pid1 = fork();
printf("4.fork returned: %d\n", (int)pid1);
if (pid1 == 0){
printf("5.Process exiting with PID: %d\n", (int)getpid());
exit(0);
}
printf("6.Process waiting with PID: %d\n", (int)getpid());
wait(NULL);
printf("7.Process exiting with PID: %d\n", (int)getpid());
exit(0);
}
printf("8.Process waiting with PID: %d\n", (int)getpid());
wait(NULL);
printf("9.Process exiting with PID: %d\n", (int)getpid());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment