Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Comm Lab - puzzle3.c
- *
- * Ecole polytechnique de Montreal, 2018
- */
- // TODO
- // Si besoin, ajouter ici les directives d'inclusion
- // -------------------------------------------------
- #include "libcommlab.h"
- #include "stdio.h"
- #include "stdlib.h"
- #include "wait.h"
- #include "string.h"
- #include "sys/stat.h"
- #include "unistd.h"
- #include "sys/types.h"
- #include "signal.h"
- #include "fcntl.h"
- // -------------------------------------------------
- void puzzle3() {
- // TODO
- // ./exc1 2>&1 > tmpfile; ./exc2 2>&1 | ./exc3 ; ./exc4 < tmpfile2>&1
- // Output ordinaire: Never trust a computer you can't throw out a window
- // Puzzle 3 output:
- // ----------------
- // Never trust a computer
- // Timeout! Killing question...
- int p[2];
- if (pipe(p) < 0) {
- exit(1);
- }
- if (!fork()) {
- // ./exc1 2>&1 > tmpfile;
- int file = open("./puzzle3/tmpfile", O_WRONLY);
- dup2(STDOUT_FILENO, STDERR_FILENO); // 2 > &1
- dup2(file, STDOUT_FILENO); // &1 > ./puzzle3/tmpfile
- execl("./puzzle3/exc1", "exc1", NULL);
- }
- while(wait(NULL) > 0); //Wait for child process to end.
- if(!fork()) {
- // ./exc2 2>&1 |e
- dup2(STDOUT_FILENO, STDERR_FILENO);
- dup2(p[1], STDERR_FILENO);
- dup2(p[1], STDOUT_FILENO);
- execl("./puzzle3/exc2", "exc2", NULL);
- }
- if(!fork()) {
- // | ./exc3
- dup2(p[0], STDIN_FILENO);
- execl("./puzzle3/exc3", "exc3", NULL);
- }
- while(wait(NULL) > 0);
- if (!fork()) {
- //./exc4 < tmpfile 2>&1
- int file = open("./puzzle3/tmpfile", O_RDONLY);
- dup2(file, STDIN_FILENO);
- dup2(STDOUT_FILENO, STDERR_FILENO);
- execl("./puzzle3/exc4", "exc4", NULL);
- }
- while(wait(NULL) > 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement