fork download
  1. #include<stdio.h>
  2. #include<sys/wait.h>
  3. #include<stdlib.h>
  4. #include <unistd.h>
  5.  
  6. int main()
  7. {
  8. pid_t pid;
  9. if(pid = fork() < 0){
  10. printf("Fork Error.!!!\n");
  11. }else {
  12. if(pid == 0){
  13. if((pid = fork()) < 0){
  14. printf("Fork2 Error.!!!\n");
  15. }else if(pid > 0){
  16. return 0;
  17. }
  18. printf("Second Child, parent id: %d\n", getppid());
  19. return 0;
  20. }
  21. }
  22. if(waitpid(pid, NULL, 0) != pid){
  23. printf("Waitpid Error.!!!\n");
  24. }
  25. sleep(2);
  26. return 0;
  27. }
Success #stdin #stdout 0.01s 2680KB
stdin
Standard input is empty
stdout
Second Child, parent id: 1
Second Child, parent id: 1