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. // Child process
  13. printf("Child Process: PID = %d, PPID = %d\n", getpid(), getppid());
  14. sleep(2); // Simulate some work
  15. printf("Child completed\n");
  16. }
  17. else if (pid > 0)
  18. {
  19. // Parent process waits for child to finish
  20. wait(NULL);
  21. printf("Parent Process: PID = %d, Child completed\n", getpid());
  22. }
  23. else
  24. {
  25. printf("Fork failed\n");
  26. }
  27.  
  28. return 0;
  29. }
  30.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Child Process: PID = 3278088, PPID = 3278085
Child completed
Parent Process: PID = 3278085, Child completed