Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.82 KB | None | 0 0
  1. /*
  2.  * Comm Lab - puzzle3.c
  3.  *
  4.  * Ecole polytechnique de Montreal, 2018
  5.  */
  6.  
  7. // TODO
  8. // Si besoin, ajouter ici les directives d'inclusion
  9. // -------------------------------------------------
  10. #include "libcommlab.h"
  11. #include "stdio.h"
  12. #include "stdlib.h"
  13. #include "wait.h"
  14. #include "string.h"
  15. #include "sys/stat.h"
  16. #include "unistd.h"
  17. #include "sys/types.h"
  18. #include "signal.h"
  19. #include "fcntl.h"
  20. // -------------------------------------------------
  21.  
  22. void puzzle3() {
  23.     // TODO
  24.     // ./exc1 2>&1 > tmpfile; ./exc2 2>&1 | ./exc3 ; ./exc4 < tmpfile2>&1
  25.     // Output ordinaire: Never trust a computer you can't throw out a window
  26.     // Puzzle 3 output:
  27.     // ----------------
  28.     // Never trust a computer
  29.     // Timeout! Killing question...
  30.  
  31.  
  32.     int p[2];
  33.     if (pipe(p) < 0) {
  34.         exit(1);
  35.     }  
  36.  
  37.     if (!fork()) {
  38.         // ./exc1 2>&1 > tmpfile;
  39.         int file = open("./puzzle3/tmpfile", O_WRONLY);
  40.         dup2(STDOUT_FILENO, STDERR_FILENO);     // 2 > &1
  41.         dup2(file, STDOUT_FILENO);     // &1 > ./puzzle3/tmpfile
  42.         execl("./puzzle3/exc1", "exc1", NULL);
  43.     }
  44.     while(wait(NULL) > 0);  //Wait for child process to end.
  45.  
  46.     if(!fork()) {
  47.         // ./exc2 2>&1 |e
  48.         dup2(STDOUT_FILENO, STDERR_FILENO);
  49.         dup2(p[1], STDERR_FILENO);
  50.         dup2(p[1], STDOUT_FILENO);
  51.         execl("./puzzle3/exc2", "exc2", NULL);
  52.     }
  53.  
  54.     if(!fork()) {
  55.         // | ./exc3
  56.         dup2(p[0], STDIN_FILENO);
  57.         execl("./puzzle3/exc3", "exc3", NULL);
  58.     }
  59.     while(wait(NULL) > 0);
  60.    
  61.     if (!fork()) {
  62.         //./exc4 < tmpfile 2>&1
  63.         int file = open("./puzzle3/tmpfile", O_RDONLY);
  64.         dup2(file, STDIN_FILENO);
  65.         dup2(STDOUT_FILENO, STDERR_FILENO);
  66.         execl("./puzzle3/exc4", "exc4", NULL);
  67.     }
  68.     while(wait(NULL) > 0);
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement