This commit is contained in:
Tom MOGULJAK 2023-10-11 11:00:11 +02:00
parent 5a48a98dc9
commit b3d9184a9a

View File

@ -13,18 +13,24 @@ uint64_t shots = 0,
double pi = 0, double pi = 0,
t1; t1;
sigset_t alset, intset, quitset;
void alarm_handler(int) void alarm_handler(int)
{ {
sigprocmask(SIG_BLOCK, &alset, NULL);
printf("pi = %f\n", pi); printf("pi = %f\n", pi);
printf("le nombre de tirs est de %f\n", (double)shots); printf("le nombre de tirs est de %f\n", (double)shots);
alarm(5); alarm(5);
sigprocmask(SIG_UNBLOCK, &alset, NULL);
} }
void int_handler(int) void int_handler(int)
{ {
char c;
alarm(0); alarm(0);
sigprocmask(SIG_BLOCK, &intset, NULL);
char c;
printf("Voulez-vous quitter ? (y/n)\n"); printf("Voulez-vous quitter ? (y/n)\n");
c = getchar(); c = getchar();
@ -33,15 +39,20 @@ void int_handler(int)
printf("Le temps écoulé est de %f\n", tstamp() - t1); printf("Le temps écoulé est de %f\n", tstamp() - t1);
exit(0); exit(0);
} }
return;
sigprocmask(SIG_UNBLOCK, &intset, NULL);
alarm(5); alarm(5);
} }
void quit_handler(int) void quit_handler(int)
{ {
alarm(0);
sigprocmask(SIG_BLOCK, &quitset, NULL);
shots = 0; shots = 0;
shots_in = 0; shots_in = 0;
t1 = tstamp(); t1 = tstamp();
sigprocmask(SIG_UNBLOCK, &quitset, NULL);
alarm(5);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -49,6 +60,12 @@ int main(int argc, char *argv[])
double x, y; double x, y;
t1 = tstamp(); t1 = tstamp();
alarm(5); alarm(5);
sigaddset(&alset, SIGINT);
sigaddset(&alset, SIGQUIT);
sigaddset(&intset, SIGALRM);
sigaddset(&intset, SIGQUIT);
sigaddset(&quitset, SIGALRM);
sigaddset(&quitset, SIGINT);
set_signal_handler(SIGALRM, &alarm_handler); set_signal_handler(SIGALRM, &alarm_handler);
set_signal_handler(SIGINT, &int_handler); set_signal_handler(SIGINT, &int_handler);
set_signal_handler(SIGQUIT, &quit_handler); set_signal_handler(SIGQUIT, &quit_handler);
@ -61,7 +78,7 @@ int main(int argc, char *argv[])
if ((x * x + y * y) <= 1) if ((x * x + y * y) <= 1)
shots_in++; shots_in++;
pi = 4 * (double)(shots_in) / shots; pi = 4 * (double)(shots_in) / shots;
} }
} }