diff --git a/td/td5/ex1.c b/td/td5/ex1.c new file mode 100644 index 0000000..e768607 --- /dev/null +++ b/td/td5/ex1.c @@ -0,0 +1,47 @@ +#include + +#include +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + pid_t p; + int t[2]; + assert(pipe(t) == 0); + + p = fork(); + + if (p == 0){ + close(t[0]); + dup2(t[1],STDOUT_FILENO); + close(t[1]); + execlp("ls","ls",NULL); + assert(0); + } + + p = fork(); + + if (p == 0){ + int fd = open("output.txt",O_WRONLY|O_CREAT|O_APPEND,0644); + assert(fd != -1); + + close(t[1]); + dup2(t[0],STDIN_FILENO); + close(t[0]); + dup2(fd,STDOUT_FILENO); + close(fd); + execlp("wc","wc",NULL); + assert(0); + + } + + close(t[0]); + close(t[1]); + + wait(NULL); + wait(NULL); + + return 0; +}