I have just some questions about the system calls signal, kill and the signal handler. I have this code:
int figlio=-1;
int main(int argc,char* argv[])
int pid1,pid2,dormi,numero;
float reciproco;
signal(SIGUSR1,gestore);
signal(SIGUSR2,gestore);
numero=atoi(argv[1]);
printf("Numero %d\n\n",numero);
fflush(stdout);
pid1=fork();
if(pid1>0) //PADRE
{
pid2=fork();
if(numero%2 == 0)
kill(pid1,SIGUSR1);
else
kill(pid2,SIGUSR2);
wait(NULL);
wait(NULL);
}
if(pid1 == 0) //FIGLIO1
{
//sleep(1);
printf("%d\n",figlio);
if(figlio == 1)
{
numero=numero*numero*numero;
fflush(stdout);
printf("Ho eseguito il cubo %d\n",numero);
}
else
{
pause();
if(figlio == 1) //Se il gestore di SIGUSR1 รจ partito
{
fflush(stdout);
printf("Ciao dal figlio 1\n");
}
}
printf("Figlio1 termina\n\n");
exit(0);
}
if(pid2 == 0) //FIGLIO2
{
if(figlio == 2)
{
dormi=numero;
reciproco=(float)numero;
reciproco=1/reciproco;
fflush(stdout);
printf("Ho eseguito il reciproco %f\n",reciproco);
sleep(dormi);
fflush(stdout);
printf("Mando un segnale a mio fratello %d\n",pid1);
kill(pid1,SIGUSR1);
}
else
printf("Arrivederci e grazie\n");
printf("Figlio2 termina\n\n");
exit(0);
}
return 0;
}
void gestore(int signo)
{
if(signo == SIGUSR1)
figlio=1;
else
figlio=2;
}
1) I don't understand why the first printf in the first child, return -1 without a sleep(1) before...seems that the handler is executed after the beginning of the child.
2) When there is a kill to a child, after this, the scheduler works on the child or continue on the father?
3) The handler is executed when the kill is sent or when the scheduler works on the child?