From: Lukas Jiriste Date: Fri, 30 Aug 2024 15:41:27 +0000 (+0200) Subject: Make shell wait for all child processes to end X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=59b4114bd1d71da10e96dbd25cd5e7fa3d856bf1;p=42%2Fminishell.git Make shell wait for all child processes to end --- diff --git a/src/execution.c b/src/execution.c index 1b0c36d..ed263a2 100644 --- a/src/execution.c +++ b/src/execution.c @@ -1015,6 +1015,27 @@ void killall(t_vec *child_pids, int sig_num) return ; } +void wait_for_all_to_end(t_vec *child_pids) +{ + size_t i; + pid_t pid; + + i = child_pids->size; + while (i > 0) + { + --i; + pid = *(pid_t *)ft_vec_access(child_pids, i); + waitpid(pid, NULL, 0); + if (g_last_signal) + { + killall(child_pids, g_last_signal); + return ; + } + ft_vec_forget(child_pids, i); + } + ft_vec_free(child_pids, NULL); +} + void wait_for_return(t_execution_env *env) { pid_t last_pid; @@ -1034,7 +1055,7 @@ void wait_for_return(t_execution_env *env) if (WIFEXITED(status)) env->ret_val = WEXITSTATUS(status); } - ft_vec_free(&env->child_pids, NULL); + wait_for_all_to_end(&env->child_pids); return ; }