This commit is contained in:
2025-09-25 14:06:50 +02:00
parent 39f1f2aa11
commit 0b734103b1
8 changed files with 203 additions and 0 deletions

23
tp/tp4/src/ex1/Makefile Normal file
View File

@@ -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

12
tp/tp4/src/ex1/parexec.c Normal file
View File

@@ -0,0 +1,12 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char* argv[])
{
// TODO
return EXIT_SUCCESS;
}

19
tp/tp4/src/ex1/rebours.c Normal file
View File

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

19
tp/tp4/src/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
tp/tp4/src/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
tp/tp4/src/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
tp/tp4/src/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);
}
}