diff options
author | Stefan Zabka <zabkaste@hu-berlin.de> | 2020-05-24 12:19:52 +0200 |
---|---|---|
committer | Stefan Zabka <zabkaste@hu-berlin.de> | 2020-05-24 12:19:52 +0200 |
commit | 04576dc2a3f761eb041b808b56f13a58052e7655 (patch) | |
tree | c6bed6db34e29f0dec0c844fbc931b3dd4fab8db /shell/beispiele/dup/dup.c | |
parent | 65966ded0cc15c5966c6568cf0ff2f2bbe1fc29a (diff) | |
download | betriebssysteme-04576dc2a3f761eb041b808b56f13a58052e7655.tar.gz betriebssysteme-04576dc2a3f761eb041b808b56f13a58052e7655.zip |
Moved back to 02_exercise
Diffstat (limited to 'shell/beispiele/dup/dup.c')
-rw-r--r-- | shell/beispiele/dup/dup.c | 51 |
1 files changed, 0 insertions, 51 deletions
diff --git a/shell/beispiele/dup/dup.c b/shell/beispiele/dup/dup.c deleted file mode 100644 index ea3c5a6..0000000 --- a/shell/beispiele/dup/dup.c +++ /dev/null @@ -1,51 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> - -/* https://www.youtube.com/watch?v=gaXigSu72A4 */ -static inline void die(const char* msg) { - perror(msg); - exit(EXIT_FAILURE); -} - -int main(int argc, char **argv) { - int pdes[2]; - pid_t child; - - if (pipe(pdes) < 0) - die("pipe()"); - - if ((child = fork()) < 0) - die("fork()"); - - if (child == 0) { - /* child process */ - close(pdes[0]); - close(1); /* close stdout */ - - if (dup(pdes[1]) < 0) - die("dup()"); - - /* now stdout and pdes[1] are equivalent (dup returns lowest free descriptor) */ - if (execlp("cat", "cat", "/etc/passwd", NULL) < 0) - die("execlp()"); - - exit(EXIT_SUCCESS); - } - else { - /* parent process */ - close(0); /* close stdin */ - close(pdes[1]); - - if (dup(pdes[0]) < 0) - die("dup()"); - - /* now stdin and pdes[0] are equivalent (dup returns lowest free descriptor) */ - if (execlp("wc", "wc", "-l", NULL) < 0) - die("execlp()"); - - exit(EXIT_SUCCESS); - } - - return 0; -} |