62 lines
1.3 KiB
C
62 lines
1.3 KiB
C
#include<unistd.h>
|
|
#include<stdio.h>
|
|
#include<stdlib.h>
|
|
#include<signal.h>
|
|
#include<stdint.h>
|
|
#include<assert.h>
|
|
|
|
uint64_t shots_in=0,shots=0;
|
|
double x,y, pi;
|
|
|
|
void point(){
|
|
x = rand()/(RAND_MAX*1.0);
|
|
y = rand()/(RAND_MAX*1.0);
|
|
shots ++;
|
|
if ((x*x+y*y)<=1){
|
|
shots_in ++;
|
|
}
|
|
pi = ((double)shots_in)/((double)shots)*4.0;
|
|
}
|
|
|
|
void sig_alarm(int signum){
|
|
printf("pi = %lf\n", pi);
|
|
alarm(5);
|
|
}
|
|
void sig_int(int signum){
|
|
char reponse;
|
|
printf("voulez vous réellement arrêter le programme ? (y/n)\n");
|
|
scanf("%c", &reponse);
|
|
if (reponse == 'y'){
|
|
exit(0);
|
|
}
|
|
}
|
|
void sig_quit(int signum){
|
|
printf(" REINITIALISATION /^\n");
|
|
shots_in = 0;
|
|
shots = 0;
|
|
point();
|
|
}
|
|
|
|
int set_signal_handler(int signo, void (*handler)(int)) {
|
|
struct sigaction sa;
|
|
sa.sa_handler = handler; // call `handler` on signal
|
|
sigemptyset(&sa.sa_mask); // don't block other signals in handler
|
|
sa.sa_flags = SA_RESTART; // restart system calls
|
|
return sigaction(signo, &sa, NULL);
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
shots_in = 0;
|
|
shots = 0;
|
|
|
|
set_signal_handler(SIGALRM,sig_alarm);
|
|
set_signal_handler(SIGINT, sig_int);
|
|
set_signal_handler(SIGQUIT, sig_quit);
|
|
alarm(5);
|
|
|
|
for (;;){
|
|
point();
|
|
}
|
|
}
|