diff options
author | Stefan Zabka <zabkaste@hu-berlin.de> | 2020-05-22 00:30:47 +0200 |
---|---|---|
committer | Stefan Zabka <zabkaste@hu-berlin.de> | 2020-05-22 00:30:47 +0200 |
commit | 4f80f043aacc568fac0e3681b62aa1971a97291a (patch) | |
tree | a06c8bb24583f790f521bb74fd3f6c0ae3c3b2e2 | |
parent | 40d578c09654ba6720ffd6e365e0d411cfa38613 (diff) | |
download | betriebssysteme-4f80f043aacc568fac0e3681b62aa1971a97291a.tar.gz betriebssysteme-4f80f043aacc568fac0e3681b62aa1971a97291a.zip |
FUCK YEAH ls -lisa | grep worksprocess_and_pipes
-rw-r--r-- | 02_exercise/process.c | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/02_exercise/process.c b/02_exercise/process.c index 00f8762..08be0fd 100644 --- a/02_exercise/process.c +++ b/02_exercise/process.c @@ -104,15 +104,16 @@ int parse_line(char const *const line, process **processes) { int exec_command(process p, unsigned timeout) { timeout = timeout ^ timeout; - int pid; int status; - if ((pid = fork()) == 0) { + if ((p.pid = fork()) == 0) { if (p.in_fd != 0) { dup2(p.in_fd, 0); + close(p.in_fd); } if (p.out_fd != 0) { dup2(p.out_fd, 1); + close(p.out_fd); } execvp(p.argv[0], p.argv); fprintf(stderr, "could not execute \"%s\"\n", p.argv[0]); @@ -120,12 +121,18 @@ int exec_command(process p, unsigned timeout) { exit(-1); } - if (pid < 0) { + if (p.pid < 0) { fprintf(stderr, "no fork\n"); exit(-2); } + if (p.in_fd != 0) { + close(p.in_fd); + } + if(p.out_fd != 0) { + close(p.out_fd); + } - waitpid(pid, &status, 0); + waitpid(p.pid, &status, 0); return WEXITSTATUS(status); } |