Ajout de l'exo3 TP4
This commit is contained in:
19
TP4/Exo3/helpers.c
Normal file
19
TP4/Exo3/helpers.c
Normal 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
7
TP4/Exo3/helpers.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifndef _HELPERS_H
|
||||
#define _HELPERS_H
|
||||
|
||||
int set_signal_handler(int signo, void (*handler)(int));
|
||||
double tstamp(void);
|
||||
|
||||
#endif
|
44
TP4/Exo3/section_critique.c
Normal file
44
TP4/Exo3/section_critique.c
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user