SHARE
TWEET

Untitled

Sorcker Oct 18th, 2019 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <signal.h>
  6.  
  7. #define DIE(msg) \
  8. do { \
  9.   printf("%s\n", msg); \
  10.   exit(EXIT_FAILURE); \
  11. } while(0)
  12.  
  13. void busy_wait()
  14. {
  15.   int i;
  16.   for (i = 0; i >= 0; i++) {
  17.     ;
  18.   }
  19. }
  20.  
  21. void batch_wait(pid_t pid)
  22. {
  23.   int batch_ret, unbatch_ret;
  24.   if ((batch_ret = batch(pid)) != 0) {
  25.     printf("batch retornou %d\n", batch_ret);
  26.   }
  27.   busy_wait();
  28.   waitpid(pid, NULL, 0);
  29. }
  30.  
  31. int main()
  32. {
  33.   pid_t child_pid = fork();
  34.   if (child_pid < 0) {
  35.     DIE("fork falhou");
  36.   }
  37.   if (child_pid == 0) {
  38.     pid_t grandchild_pid = fork();
  39.     if (grandchild_pid < 0) {
  40.       DIE("fork do grandchild falhou");
  41.     }
  42.     if (grandchild_pid == 0) {
  43.       pid_t last_pid = fork();
  44.       printf("last_pid = %d\n", last_pid);
  45.       if (last_pid < 0) {
  46.         DIE("fork do last_child falhou");
  47.       }
  48.       if (last_pid == 0) {
  49.         busy_wait();
  50.       } else {
  51.         printf("grandchild para last\n");
  52.         batch_wait(last_pid);
  53.       }
  54.     } else {
  55.       printf("child para grandchild\n");
  56.       batch_wait(grandchild_pid);
  57.     }
  58.   } else {
  59.     printf("parent para child\n");
  60.     batch_wait(child_pid);
  61.   }
  62.   return 0;
  63. }
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