This commit is contained in:
Arnaud NGWALA NGWALA 2023-09-28 16:20:08 +02:00
parent 8386f31be1
commit a918c301ce
5 changed files with 177 additions and 0 deletions

19
SCR3.1/TP4/helpers.c Normal file
View 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
SCR3.1/TP4/helpers.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef _HELPERS_H
#define _HELPERS_H
int set_signal_handler(int signo, void (*handler)(int));
double tstamp(void);
#endif

59
SCR3.1/TP4/pi.c Normal file
View File

@ -0,0 +1,59 @@
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<signal.h>
#include<stdint.h>
#include<assert.h>
#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;
}
}

58
SCR3.1/TP4/ping_pong.c Normal file
View File

@ -0,0 +1,58 @@
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/types.h>
#include <errno.h>
#include <assert.h>
#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;
}

View File

@ -0,0 +1,34 @@
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<signal.h>
#include<stdint.h>
#include<assert.h>
#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);
}
}