TP4 SCR
This commit is contained in:
19
SCR/SCR3.1/TP04/helpers.c
Normal file
19
SCR/SCR3.1/TP04/helpers.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include "helpers.h"
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
double tstamp(void) {
|
||||
struct timespec tv;
|
||||
clock_gettime(CLOCK_REALTIME, &tv);
|
||||
return tv.tv_sec + tv.tv_nsec * 1.0e-9;
|
||||
}
|
||||
|
||||
|
||||
7
SCR/SCR3.1/TP04/helpers.h
Normal file
7
SCR/SCR3.1/TP04/helpers.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifndef _HELPERS_H
|
||||
#define _HELPERS_H
|
||||
|
||||
int set_signal_handler(int signo, void (*handler)(int));
|
||||
double tstamp(void);
|
||||
|
||||
#endif
|
||||
1
SCR/SCR3.1/TP04/mes.txt
Normal file
1
SCR/SCR3.1/TP04/mes.txt
Normal file
@@ -0,0 +1 @@
|
||||
ALIVE !
|
||||
36
SCR/SCR3.1/TP04/pi.c
Normal file
36
SCR/SCR3.1/TP04/pi.c
Normal file
@@ -0,0 +1,36 @@
|
||||
#include<unistd.h>
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<signal.h>
|
||||
#include<stdint.h>
|
||||
#include<assert.h>
|
||||
#include "helpers.h"
|
||||
|
||||
|
||||
uint64_t shots = 0,
|
||||
shots_in = 0;
|
||||
|
||||
double pi = 0,
|
||||
t1;
|
||||
|
||||
void onAlarmSent(double pi, uint64_t shots) {
|
||||
printf("Pi value = %f, Shots value = %lu", pi, shots);
|
||||
}
|
||||
|
||||
int main(int argc,char * argv[])
|
||||
{
|
||||
double x,y;
|
||||
t1 = tstamp();
|
||||
|
||||
set_signal_handler(SIGALRM, onAlarmSent);
|
||||
alarm(5);
|
||||
|
||||
while(1){
|
||||
x = ((double)rand())/(double)RAND_MAX;
|
||||
y = ((double)rand())/(double)RAND_MAX;
|
||||
shots ++;
|
||||
|
||||
if ((x*x+y*y) <= 1)
|
||||
shots_in ++;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user