Reputation: 177
I have the following code:
void sig_handler(int sig) {
printf("Hello child\n");
}
int main()
{
pid_t child = fork();
if (child > 0)
{
printf("Hello parent\n");
kill(child, SIGUSR1);
}
else if (child == 0)
{
signal(SIGUSR1, sig_handler);
printf("In child\n");
}
else
{
printf("Error\n");
}
return 0;
}
I want the code to run:
Hello parent
Hello child
In child
But the child is killed immediately after the parent sends kill(child, SIGUSR1);
, and the result is just:
Hello parent
I have read document that the dafault action of the SIGUSR1
is termination, however, I have already implemented the signal handler signal(SIGUSR1, sig_handler);
for catching the SIGUSR1
, then why is the child still killed?
Upvotes: 1
Views: 1429
Reputation: 44
I'd say there are two possibilities: either the son process dies before the main process sends the signal, or the main process sends the signal before the handler is set.
Edit: if you only get "Hello parent" it can't be the first one.
Upvotes: 2