Reputation: 1704
What is the relation between the SIGCHLD
handling and the sleep
function?
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
sig_atomic_t child_exit_status;
void clean_up_child_process(int signal_number)
{
int status;
wait(&status);
child_exit_status = status;
}
int main()
{
struct sigaction sigchld_action;
memset(&sigchld_action, 0, sizeof(sigchld_action));
sigchld_action.sa_handler = &clean_up_child_process;
sigaction(SIGCHLD, &sigchld_action, NULL);
pid_t child_pid = fork();
if (child_pid > 0) {
printf("Parent process, normal execution\n");
sleep(60);
printf("After sleep\n");
} else {
printf("Child\n");
}
return 0;
}
Because when I execute the code from above, it prints:
Parent process, normal execution
Child
After sleep
But there is no effect from the sleep
function in the execution. Is there any particularity that happens here?
Upvotes: 0
Views: 275
Reputation: 1704
From the documentation:
RETURN VALUE
Zero if the requested time has elapsed, or the number of seconds left to sleep, if the call was interrupted by a signal handler.
What happens is that the sleep function is interrupted by the signal handler. So checking the return value of it a 60
is seen. Another strategy could be used to sleep the remaining seconds if that's the intention.
Upvotes: 1