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
| #include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#include <errno.h>
extern int errno;
void proc(){/*procedure executee lorsque le signal SIGALRM est recu */
fprintf(stdout,"5 secondes ecoulees!\n");
}
main(){
char input[30];
struct itimerval timer; /
#ifdef BSD
signal(SIGALRM,proc);
#else
struct sigaction act;
act.sa_handler = proc;
act.sa_flags = SA_RESTART;
if(sigaction(SIGALRM, &act, NULL) == -1) {
fprintf(stderr,"error %d in sigaction\n");
}
#endif
/* Config du timer */
timer.it_interval.tv_sec = 5;
timer.it_interval.tv_usec = 0;
timer.it_value.tv_sec = 5;
timer.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &timer, 0);
while(1){
while((fscanf(stdin,"%30s",input)<0)&&(errno=EINTR));
fprintf(stdout,"%s\n",input);
if(!strncmp(input,"quit",5)){ /* demade de stop timer*/
memset(&timer, 0, sizeof(timer)); /* MARCHE PAS*/
}
}
} |
Partager