Ajout de l'exo3 TP4

This commit is contained in:
2023-10-12 09:39:37 +02:00
parent 9467a67177
commit 062c53f218
4 changed files with 131 additions and 0 deletions

19
TP4/Exo3/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
TP4/Exo3/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

View File

@@ -0,0 +1,44 @@
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <stdint.h>
#include <assert.h>
#include "helpers.h"
#include "helpers.c"
int x = 2, y = 3;
sigset_t quitset;
int swap(int *x, int *y)
{
sigemptyset(&quitset);
sigaddset(&quitset, SIGQUIT);
sigprocmask(SIG_BLOCK, &quitset, NULL);
int tmp = *x;
*x = *y;
*y = tmp;
sigprocmask(SIG_UNBLOCK, &quitset, NULL);
}
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);
}
}