fork download
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <sys/wait.h>
  5.  
  6. int main()
  7. {
  8. pid_t pid = fork();
  9.  
  10. if (pid == 0)
  11. {
  12. printf("Child Process: PID = %d, PPID = %d\n", getpid(), getppid());
  13. sleep(2);
  14. printf("Child completed\n");
  15. }
  16. else if (pid > 0)
  17. {
  18. wait(NULL);
  19. printf("Parent Process: PID = %d, Child completed\n", getpid());
  20. }
  21. else
  22. {
  23. printf("Fork failed\n");
  24. }
  25.  
  26. return 0;
  27. }
  28.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Child Process: PID = 3277394, PPID = 3277391
Child completed
Parent Process: PID = 3277391, Child completed