fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <unistd.h>
  6.  
  7.  
  8. int main(){
  9.  
  10. printf("I 'am : %d\n", (int) getpid());
  11.  
  12. pid_t pid = fork();
  13. sleep(2);
  14.  
  15. printf("for returend %d \n",(int) pid);
  16.  
  17. if(pid < 0){ //erorr occurend.
  18.  
  19. perror("fork failed");
  20.  
  21. }else if(pid == 0){ //child process
  22.  
  23. printf("I am the child with pid %d\n",(int) getpid());
  24. printf("Child process is exiting \n");
  25. exit(1);
  26. }
  27.  
  28. // parent
  29. printf("I'am the parent waiting for the child process to end\n");
  30. wait(NULL);
  31. printf("Parent process is exiting\n");
  32.  
  33. return 0;
  34.  
  35. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
I 'am : 3054170
for returend 0 
I am the child with pid 3054173
Child process is exiting 
I 'am : 3054170
for returend 3054173 
I'am the parent waiting for the child process to end
Parent process is exiting