Advertisement
Guest User

1

a guest
Jul 28th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7.  
  8. int main(){
  9.  
  10. pid_t c_pid, pid;
  11. int status;
  12.  
  13. c_pid = fork(); //duplicate
  14.  
  15.  
  16. if( c_pid == 0 ){
  17. //child
  18. pid = getpid();
  19.  
  20. printf("Child: %d: I'm the child\n", pid, c_pid);
  21. printf("Child: sleeping for 2-seconds, then exiting with status 12\n");
  22.  
  23. //sleep for 2 seconds
  24. sleep(2);
  25.  
  26. //exit with statys 12
  27. exit(12);
  28.  
  29. }else if (c_pid > 0){
  30. //parent
  31.  
  32. //waiting for child to terminate
  33. pid = wait(&status);
  34.  
  35. if ( WIFEXITED(status) ){
  36. printf("Parent: Child exited with status: %d\n", WEXITSTATUS(status));
  37. }
  38.  
  39.  
  40. }else{
  41. //error: The return of fork() is negative
  42.  
  43. perror("fork failed");
  44. _exit(2); //exit failure, hard
  45.  
  46. }
  47.  
  48. return 0; //success
  49.  
  50. #include <unistd.h>
  51. #include <stdlib.h>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement