From 4f80f043aacc568fac0e3681b62aa1971a97291a Mon Sep 17 00:00:00 2001 From: Stefan Zabka Date: Fri, 22 May 2020 00:30:47 +0200 Subject: FUCK YEAH ls -lisa | grep works --- 02_exercise/process.c | 15 +++++++++++---- 1 file 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); } -- cgit v1.2.3-54-g00ecf