1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| #include <stdio.h>
#include <unistd.h>
#include <signal.h>
typedef void (*sighandler_t)(int);
//! Dummy function to avoid exit on alarm
void hdle_alarm (int val)
{
// do nothing
}
int main (void)
{
int i = -1 ;
int j = 3 ;
sighandler_t hldle_bckp = NULL ;
struct sigaction act ;
struct sigaction oldact ;
sigset_t set ;
int ret = 0 ;
printf("Begin\n");
/*hldle_bckp = signal (SIGALRM, hdle_alarm) ;
if (hldle_bckp == SIG_ERR)
{
printf("error handler");
return -1 ;
}*/
// Installer notre gestionnaire
sigfillset (&set) ;
act.sa_handler = hdle_alarm ;
act.sa_mask = set ;
act.sa_flags = 0 ; // L'interet ici par rapport a signal est de justement est de ne pas redemarrer les appels systemes lents avec le flags SA_RESTART qui est fournit lors de signal()
if ( sigaction (SIGALRM, &act, &oldact) == -1 )
{
printf("error handler");
// can disp errno
return -1 ;
}
while ((j--) && (ret<=0) )
{
alarm(1) ;
ret = scanf("%d", &i) ;
if (ret<=0)
{
printf("alarm\n") ;
}
}
printf("End with i=%d\n", i);
//signal (SIGALRM, hldle_bckp);
sigaction (SIGALRM, &oldact, NULL);
return 0 ;
} |