diff --git a/SCR3.1/TP4/helpers.c b/SCR3.1/TP4/helpers.c new file mode 100644 index 0000000..718fe40 --- /dev/null +++ b/SCR3.1/TP4/helpers.c @@ -0,0 +1,19 @@ +#include "helpers.h" +#include +#include + +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; +} + + diff --git a/SCR3.1/TP4/helpers.h b/SCR3.1/TP4/helpers.h new file mode 100644 index 0000000..2084c6a --- /dev/null +++ b/SCR3.1/TP4/helpers.h @@ -0,0 +1,7 @@ +#ifndef _HELPERS_H +#define _HELPERS_H + +int set_signal_handler(int signo, void (*handler)(int)); +double tstamp(void); + +#endif diff --git a/SCR3.1/TP4/pi.c b/SCR3.1/TP4/pi.c new file mode 100644 index 0000000..5d2ceb3 --- /dev/null +++ b/SCR3.1/TP4/pi.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include +#include "helpers.h" +#include "helpers.c" + +uint64_t shots = 0, + shots_in = 0; + +double pi = 0, t1; + +void int_handler(int){ + alarm(0); + char c; + printf("Le programme tourne depuis %f secondes, voulez-vous vraiment l'arrĂȘter ? (O)ui / (N)on", tstamp() - t1); + c = getchar(); + if (c == 'O' || c == 'o' || c == 'Y' || c == 'y'){ + exit(0); + } + else{ + return; + alarm(5); + } +} + +void alarm_handler(int){ + printf("Valeur de pi en cours = %f\n", pi); + printf("Nombre de tirs effectuĂ©s = %f\n", (double) shots); + alarm(5); +} + +void quit_handler(int){ + alarm(0); + shots = 0; + shots_in = 0; + t1 = tstamp(); + alarm(5); +} + +int main(int argc,char * argv[]) +{ + double x,y; + t1 = tstamp(); + alarm(5); + set_signal_handler(SIGALRM, &alarm_handler); + set_signal_handler(SIGINT, &int_handler); + set_signal_handler(SIGQUIT, &quit_handler); + while(1){ + x = ((double)rand())/(double)RAND_MAX; + y = ((double)rand())/(double)RAND_MAX; + shots ++; + if ((x*x+y*y) <= 1) + shots_in ++; + pi = shots_in * 1.0 / shots * 4; + } +} diff --git a/SCR3.1/TP4/ping_pong.c b/SCR3.1/TP4/ping_pong.c new file mode 100644 index 0000000..2ae839a --- /dev/null +++ b/SCR3.1/TP4/ping_pong.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include +#include +#include + +#define N 10000 +void sig_hand(int sig) {} + +sigset_t saveMask, blockMask; + +void player_wait(){ + pause(); +} +void child_process() +{ + int x = 0; + while(x < N) + { + player_wait(); + printf("\tPong %d!\n", ++x); + kill(getppid(), SIGUSR1); + } + return ; +} + +void parent_process(pid_t pid) +{ + int y = 0; + while (y < N) + { + printf("Ping %d!\n", ++y); + kill(pid, SIGUSR1); + player_wait(); + } + return ; +} + +int main(int argc, char* argv[]) +{ + //set up signal handler for parent & child + struct sigaction sa; + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + sa.sa_handler = sig_hand; + + assert (sigaction(SIGUSR1, &sa, NULL) != -1); + + pid_t pid = fork(); + + if (pid == 0) + child_process(); + else + parent_process(pid); + return 0; +} diff --git a/SCR3.1/TP4/section_critique.c b/SCR3.1/TP4/section_critique.c new file mode 100644 index 0000000..04725f4 --- /dev/null +++ b/SCR3.1/TP4/section_critique.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include +#include +#include "helpers.h" + + +int x=2,y=3; + +int swap(int *x,int *y) +{ + int tmp=*x; + *x=*y; + *y=tmp; +} + +void sig_handler(int signo) +{ + switch(signo){ + case SIGQUIT : + printf("x=%d y=%d\n",x,y); + break; + } +} + +int main(int argc,char * argv[]) +{ + assert(set_signal_handler(SIGQUIT,sig_handler)==0); + while(1){ + swap(&x,&y); + } +}