fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include<sys/wait.h>
  5.  
  6. int main()
  7. {
  8. int pid, pid1;
  9.  
  10. pid = fork();
  11.  
  12. if (pid == 0) {
  13.  
  14. printf("child 1 id = %d and parent id = %d\n",
  15. getpid(), getppid());
  16. }
  17.  
  18. else {
  19. pid1 = fork();
  20. if (pid1 == 0) {
  21. printf("child 2 id = %d and parent id = %d\n",
  22. getpid(), getppid());
  23. }
  24. else {
  25. wait(NULL);
  26. printf("parent id = %d\n", getpid());
  27. }
  28. }
  29.  
  30. }
Success #stdin #stdout 0s 5436KB
stdin
Standard input is empty
stdout
child 2 id = 10926 and parent id = 10879
parent id = 10879
child 1 id = 10925 and parent id = 1