fork download
  1. #include<stdio.h> // printf()
  2. #include<stdlib.h> // exit()
  3. #include<sys/types.h> // pid_t
  4. #include<sys/wait.h> // wait()
  5. #include<unistd.h> // fork
  6. int main(int argc, char **argv)
  7. {
  8. pid_t pid;
  9. pid = fork();
  10. if(pid==0)
  11. {
  12. printf("It is the child process and pid is %d\n",getpid());
  13. int i=0;
  14. for(i=0;i<8;i++)
  15. {
  16. printf("%d\n",i);
  17. }
  18. exit(0);
  19. }
  20. else if(pid > 0)
  21. {
  22. printf("It is the parent process and pid is %d\n",getpid());
  23. int status;
  24. wait(&status);
  25. printf("Child is reaped\n");
  26. }
  27. else
  28. {
  29. printf("Error in forking..\n");
  30. exit(EXIT_FAILURE);
  31. }
  32. return 0;
  33. }
Success #stdin #stdout 0s 5280KB
stdin
 
stdout
It is the child process and pid is 704220
0
1
2
3
4
5
6
7
It is the parent process and pid is 704217
Child is reaped