fork download
  1. #include<stdio.h>
  2. #include<sys/types.h>
  3. #include<sys/wait.h>
  4. #include<unistd.h>
  5. #include<stdlib.h>
  6.  
  7. int main()
  8. {
  9. int child=fork();
  10. int exitStatus;//0 if success
  11. int childPid;
  12. if(child==0)
  13. {
  14. printf("child: iam running\n");
  15. printf("child: i have PD :%d\n",getpid());//return the process id
  16. sleep(4);
  17. exit(100);
  18. }//code for child process
  19. else
  20. {
  21. printf("Parent : iam running and waiting for child to finish\n");
  22. childPid=wait(&exitStatus);
  23. printf("parent : Child finished execution,it had the PID:%d,Exit Status%d\n",childPid,WEXITSTATUS(exitStatus));
  24. }
  25. return 0;
  26. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
child: iam running
child: i have PD :384827
Parent : iam running and waiting for child to finish
parent : Child finished execution,it had the PID:384827,Exit Status100