X-Git-Url: https://git.piment-noir.org/?p=TD_IML.git;a=blobdiff_plain;f=TD2%2Fexercise3%2Fexercise.c;fp=TD2%2Fexercise3%2Fexercise.c;h=c4e9e4102b0445357a0edacbc31da488abedac5a;hp=0000000000000000000000000000000000000000;hb=000bb88ff53b62a983a73e75b6e19dea79d63981;hpb=80b1633cbdb86828624f8b84399e10b340b398c6 diff --git a/TD2/exercise3/exercise.c b/TD2/exercise3/exercise.c new file mode 100644 index 0000000..c4e9e41 --- /dev/null +++ b/TD2/exercise3/exercise.c @@ -0,0 +1,41 @@ +#include +#include +#include +#include + +#include +#include + +int main() { + int pipefd[2]; + char tmpbuf; + + if (pipe(pipefd) == -1) { + perror("Pipe creation failure\n"); + exit(EXIT_FAILURE); + }; + + pid_t cpid = fork(); + + if (cpid == -1) { + perror("fork failure\n"); + exit(EXIT_FAILURE); + } else if (cpid == 0) { + close(pipefd[0]); + dup2(pipefd[1], STDOUT_FILENO); + printf("[%d] Hey Jude\n", getpid()); + //execl("/bin/ls", "ls", (char*)NULL); + close(pipefd[1]); + exit(EXIT_SUCCESS); + } else { + close(pipefd[1]); + printf("[%d] REDIRECTION: \n", getpid()); + while (read(pipefd[0], &tmpbuf, 1) > 0) { + write(STDOUT_FILENO, &tmpbuf, 1); + } + close(pipefd[0]); + wait(NULL); + } + + return EXIT_SUCCESS; +}