Consider the following Pseudo-C code:
static int G = 0;
void alrm_handler(int signo) {
G = 1;
}
void son() {
struct sched_param param;
param.sched_priority = 50;
sched_setscheduler(getpid(),SCHED_FIFO, ¶m);
kill(getppid(), SIG_ALRM);
sched_yield();
printf("A\n");
while(1);
exit(0);
}
int main() {
signal(SIG_ALRM, alrm_handler);
int son_pid = fork();
if(son_pid==0)
son();
waitpid(son_pid);
printf("B\n");
return 0;
}
It's from an exam in OS. The question is as follows:
In multi cores CPU what would be printed?
The answers is that we can't know because it could be A
and then B
or B
and then A
. I don't understand why. If parent is waiting for the son with waitpid
and the son sends signal SIG_ALRM
which invokes the alrm_handler
function for the parent, the parent will finish executing the function and then return to waiting the son until he will finish running, no? So it should be A
and then B
.