This is an example question for an exam I’m studying for – the question is what’s wrong with the code. I’m thinking it may be the alarm(3), the delay causing it to jump into the while loop? Or possibly that SIGALRM shouldn’t be used to wake up from a sleep, but I don’t think that’s a valid point here. Any feedback appreciated, I’ve found it very difficult to find relevant information relating to this kind of stuff.
static void foo(int signo) {
printf("Nudge-nudge\n");
alarm(3);
}
int main() {
sigset_t set;
sigemptyset(&set);
sigset(SIGALRM, foo);
alarm(3);
while (1) {
sigsuspend(&set);
}
return (0);
}
The problem here (maybe just one of them) is that you suspend the process on an empty signal set,
sigset(3)does not populate it.sigsuspend(2)modifies process signal mask, soSIGALRMis blocked.