fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/wait.h>
  4. #include <unistd.h>
  5.  
  6. int main(void){
  7.  
  8. if(fork()==0){
  9. printf("Child process started. pid = %d\n",getpid());
  10. if (fork() ==0) {
  11. printf("Grand child process started. pid = %d\n", getpid());
  12. sleep(5);
  13. printf("Grandchild process end.\n");
  14. }
  15. sleep(1);
  16. printf("Child process end.\n");
  17. }
  18.  
  19. pid_t pid = wait(0);
  20.  
  21. printf("%d is waiting child. pid = %d\n", getpid(), pid);
  22. return 0;
  23. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Child process started. pid = 2588638
Grand child process started. pid = 2588639
Grandchild process end.
Child process end.
2588639 is waiting child. pid = -1
Child process started. pid = 2588638
Child process end.
2588638 is waiting child. pid = 2588639
2588635 is waiting child. pid = 2588638