From 0b734103b18a652a03803ec5d53d4bad920fc5a0 Mon Sep 17 00:00:00 2001 From: Denis Monnerat Date: Thu, 25 Sep 2025 14:06:50 +0200 Subject: [PATCH] src --- tp/tp4/src/ex1/Makefile | 23 ++++++++++++++ tp/tp4/src/ex1/parexec.c | 12 ++++++++ tp/tp4/src/ex1/rebours.c | 19 ++++++++++++ tp/tp4/src/helpers.c | 19 ++++++++++++ tp/tp4/src/helpers.h | 7 +++++ tp/tp4/src/pi.c | 31 +++++++++++++++++++ tp/tp4/src/ping_pong.c | 58 +++++++++++++++++++++++++++++++++++ tp/tp4/src/section_critique.c | 34 ++++++++++++++++++++ 8 files changed, 203 insertions(+) create mode 100644 tp/tp4/src/ex1/Makefile create mode 100644 tp/tp4/src/ex1/parexec.c create mode 100644 tp/tp4/src/ex1/rebours.c create mode 100644 tp/tp4/src/helpers.c create mode 100644 tp/tp4/src/helpers.h create mode 100644 tp/tp4/src/pi.c create mode 100644 tp/tp4/src/ping_pong.c create mode 100644 tp/tp4/src/section_critique.c diff --git a/tp/tp4/src/ex1/Makefile b/tp/tp4/src/ex1/Makefile new file mode 100644 index 0000000..cee4115 --- /dev/null +++ b/tp/tp4/src/ex1/Makefile @@ -0,0 +1,23 @@ + + +# si on tape "make" sans préciser de cible, make va chercher à +# construire la *première* cible du Makefile. +# +default: all + +all: rebours parexec + +########################################## +# compilation des programmes + +rebours: rebours.c + gcc -g -Wall -Wextra -Werror -o rebours rebours.c + +parexec: parexec.c + gcc -g -Wall -Wextra -Werror -o parexec parexec.c + +########################################## +# nettoyage des fichiers générés + +clean: + rm -f *.o parexec rebours diff --git a/tp/tp4/src/ex1/parexec.c b/tp/tp4/src/ex1/parexec.c new file mode 100644 index 0000000..91e0fab --- /dev/null +++ b/tp/tp4/src/ex1/parexec.c @@ -0,0 +1,12 @@ +#include +#include +#include +#include +#include +#include + +int main(int argc, char* argv[]) +{ + // TODO + return EXIT_SUCCESS; +} diff --git a/tp/tp4/src/ex1/rebours.c b/tp/tp4/src/ex1/rebours.c new file mode 100644 index 0000000..10114d8 --- /dev/null +++ b/tp/tp4/src/ex1/rebours.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include + +int main(int argc, char* argv[]) +{ + assert(argc == 2); + + int value = strtol(argv[1], NULL, 0); + pid_t p = getpid(); + printf("%d: debut\n",p); + for(int i = value; i>0; i--){ + printf("%d: %d\n",p,i); + sleep(1); + } + printf("%d: fin\n",p); + return EXIT_SUCCESS; +} diff --git a/tp/tp4/src/helpers.c b/tp/tp4/src/helpers.c new file mode 100644 index 0000000..718fe40 --- /dev/null +++ b/tp/tp4/src/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/tp/tp4/src/helpers.h b/tp/tp4/src/helpers.h new file mode 100644 index 0000000..2084c6a --- /dev/null +++ b/tp/tp4/src/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/tp/tp4/src/pi.c b/tp/tp4/src/pi.c new file mode 100644 index 0000000..08856a5 --- /dev/null +++ b/tp/tp4/src/pi.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include +#include +#include +#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 ++; + } +} diff --git a/tp/tp4/src/ping_pong.c b/tp/tp4/src/ping_pong.c new file mode 100644 index 0000000..2ae839a --- /dev/null +++ b/tp/tp4/src/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/tp/tp4/src/section_critique.c b/tp/tp4/src/section_critique.c new file mode 100644 index 0000000..04725f4 --- /dev/null +++ b/tp/tp4/src/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); + } +}