diff options
-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); } |