Make shell wait for processes to end after signal
authorLukas Jiriste <ljiriste@student.42prague.com>
Sun, 1 Sep 2024 08:20:58 +0000 (10:20 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sun, 1 Sep 2024 08:20:58 +0000 (10:20 +0200)
Before the shell would send the signal to children and assume they
terminated. Now it actually still waits for them to terminate.

src/execution.c

index 0fd24fd022ba938c73dfd917c76b6dbc8da36873..8ee7e805e5dd3fc15a39ae50692e58bffc6f3fd9 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: lnikolov <lnikolov@student.42prague.com    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/07/21 08:57:54 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/09/01 08:34:42 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/09/01 10:19:33 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -1056,21 +1056,29 @@ void    wait_for_all_to_end(t_vec *child_pids)
 {
        size_t  i;
        pid_t   pid;
+       int             sig_num;
 
+       sig_num = 0;
        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 == 0)
+                       waitpid(pid, NULL, 0);
                if (g_last_signal)
                {
+                       sig_num = g_last_signal;
+                       g_last_signal = 0;
                        killall(child_pids, g_last_signal);
-                       return ;
+                       i = child_pids->size;
                }
                ft_vec_forget(child_pids, i);
        }
        ft_vec_free(child_pids, NULL);
+       if (g_last_signal == 0)
+               g_last_signal = sig_num;
+       return ;
 }
 
 void   wait_for_return(t_execution_env *env)
@@ -1087,8 +1095,6 @@ void      wait_for_return(t_execution_env *env)
                last_pid = *(pid_t *)ft_vec_access(&env->child_pids,
                                env->child_pids.size - 1);
                waitpid(last_pid, &status, 0);
-               if (g_last_signal != 0)
-                       killall(&env->child_pids, g_last_signal);
                if (g_last_signal == 0)
                        if (WIFEXITED(status))
                                env->ret_val = WEXITSTATUS(status);