SHARE
TWEET

Untitled

a guest Jun 19th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Create two children
  2. pid_t child1;
  3. pid_t child2;
  4. child1 = fork();
  5.  
  6. //wait for child1 to finish, then kill child2
  7. waitpid() ... child1 {
  8. kill(child2) }
  9.      
  10. pid_t waitpid(pid_t pid, int *status, int options);
  11.      
  12. pid_t waitpid(pid_t pid, int *statusPtr, int options);
  13.      
  14. #include <stdio.h>
  15. #include <sys/types.h>
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <sys/wait.h>
  19.  
  20. int main (){
  21.     int pid;
  22.     int status;
  23.  
  24.     printf("Parent: %dn", getpid());
  25.  
  26.     pid = fork();
  27.     if (pid == 0){
  28.         printf("Child %dn", getpid());
  29.         sleep(2);
  30.         exit(EXIT_SUCCESS);
  31.     }
  32.  
  33. //Comment from here to...
  34.     //Parent waits proccess pid (child)
  35.     waitpid(pid, &status, 0);
  36.     //Option is 0 since I check it later
  37.  
  38.     if (WIFSIGNALED(status)){
  39.         printf("Errorn");
  40.     }
  41.     else if (WEXITSTATUS(status)){
  42.         printf("Exited Normallyn");
  43.     }
  44. //To Here and see the difference
  45.     printf("Parent: %dn", getpid());
  46.  
  47.     return 0;
  48. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top