#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
sig_atomic_t child_exit_status;
void clean_up_child_process (int signal_number)
{
/* Clean up the child process. */
int status;
wait (&status);
/* Store its exit status in a global variable. */
child_exit_status = status;
}
int main ()
{
/* Handle SIGCHLD by calling clean_up_child_process. */
struct sigaction sigchld_action;
memset (&sigchld_action, 0, sizeof (sigchld_action));
sigchld_action.sa_handler = &clean_up_child_process;
sigaction (SIGCHLD, &sigchld_action, NULL);
/* Now do things, including forking a child process. */
/* ... */
pid_t t = fork();
if (t!=0) {
// parent
sleep(30); // After handling signal, why does it not continue to sleep 20 (30-10) more seconds?
printf("Parent exits\n");
}
else {
// child
sleep(10);
printf("child exists\n");
}
return 0;
}
Результат, который я получил через 10 секунд, и дочерний, и родительский процессы выводят свое сообщение, а затем завершаются.Я ожидаю, что дочерний процесс сначала распечатает сообщение, затем родительский процесс будет спать еще около 20 секунд, прежде чем распечатать свое сообщение и выйти.Почему родительский процесс не возвращается в «точное местоположение» перед обработкой сигнала, который спит еще 20 секунд?Есть ли способ, которым я могу достичь этого?