please look at the following piece of code
Code: Select all
pid_t child = 0;
void forward(int signum)
{
if (child != 0) {
printf("sig in parent\n");
} else {
printf("sig in child\n");
}
}
int main(int argc, char *argv[])
{
for(;;) {
pid_t child = fork();
if (child < 0) exit(1);
if (child == 0) {
argv[0] = "ping";
execvp("ping", argv);
}
signal(SIGINT, &forward);
int status;
wait(&status);
printf("child %d dead :-( respawning...\n", child);
}
}
when i press ctrl-c, i get the output:
why?sig in child
the signal handler was installed only AFTER the fork and only within the parent's address space.
can any of you make sense of that?
thanks
martin