septembre + octobre

This commit is contained in:
2023-10-12 16:39:49 +02:00
commit 06bf5f9488
389 changed files with 4233 additions and 0 deletions

61
SCR/TP04/ex1.c Normal file
View File

@@ -0,0 +1,61 @@
#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();
}
}

60
SCR/TP04/ex1.c~ Normal file
View File

@@ -0,0 +1,60 @@
#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){
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();
}
}

BIN
SCR/TP04/exe Executable file

Binary file not shown.

19
SCR/TP04/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
SCR/TP04/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

31
SCR/TP04/pi.c Normal file
View File

@@ -0,0 +1,31 @@
#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;
int main(int argc,char * argv[])
{
double x,y;
t1 = tstamp();
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 ++;
}
}

58
SCR/TP04/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);
}
}