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
|
#define _POSIX_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <setjmp.h>
#include <time.h>
int compt=0;
int temps=10;
const int APRES=1;
sigjmp_buf contexte;
void plustard(int sig)
{
compt++;
printf("coucou\n");
if(compt==10)
printf("Cela fait %d secondes",compt);
siglongjmp(contexte,1);
}
int main(int argc, char *argv[])
{
struct sigaction act;
int i,j;
const int LONGTEMPS=500000;
srand(time(NULL));
act.sa_handler=plustard;
sigaction(SIGALRM,&act,NULL);
printf("Av\n");
alarm(APRES);
printf("Ava\n");
for(i=0;i<LONGTEMPS;i++)
{
printf("Avan\n");
if(sigsetjmp(contexte, 1))
{
alarm(APRES);
}
for(j=0;j<rand()*10;j++)
;
printf("i = %d\n",i);
}
printf("Le prog a duré %d.\n",compt);
return EXIT_SUCCESS;
} |
Partager