fork download
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <stdlib.h>
  6.  
  7. int main()
  8. {
  9. pid_t pid = fork();
  10.  
  11. if (pid < 0)
  12. {
  13. perror("fork failed");
  14. exit(1);
  15. }
  16. else if (pid == 0)
  17. {
  18. printf("Child process : PID = %d\n", getpid());
  19. printf("Child process exiting...\n");
  20. exit(8);
  21. }
  22. else
  23. {
  24. int status;
  25. wait(&status);
  26.  
  27. printf("Parent process : PID = %d\n", getpid());
  28. printf("Child exited with status = %d\n", WEXITSTATUS(status));
  29. }
  30.  
  31. return 0;
  32. }
  33.  
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
Child process : PID = 1657284
Child process exiting...
Parent process : PID = 1657281
Child exited with status = 8