Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/mman.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <semaphore.h>
- #include <time.h>
- #include <signal.h>
- #include <limits.h>
- #define RELATIVE_TIMER 0
- #define ABSOLUTE_TIMER 1
- struct sigaction sig_act;
- struct sigaction sig_act1;
- timer_t timer_id,timer_id_Ten,timer_id_Inter_SIG;
- struct itimerspec timer_def,timer_Ten,timer_Ten_Wait,timer_Sig;
- struct sigevent sig_pour_timer;
- union sigval sigval_appl;
- //pid_t id_main;
- int Occur = 1;
- void Handler_Signaux(int signal,siginfo_t *siginfo_hdler,int *val);
- void Handler_Timers(int signal,siginfo_t *siginfo_hdler,int *val);
- int main(int argc, char *argv[])
- {
- int ret_val ;
- //*********************************************************************************
- // DEFINIR COMPORTEMENT DU PROCESS COURANT POUR UN SIGNAL DONNE
- // * COMPORTEMENT : HANDLER
- // Handler_Signaux
- // SIGNAL : SIGUSR1
- // FLAGS : sa_siginfo
- //
- //*******************************************************************************
- sig_act.sa_handler = Handler_Signaux;
- sig_act.sa_flags = SA_SIGINFO;
- if((ret_val = sigaction(SIGALRM, &sig_act, 0)) == -1)
- {
- perror("\nErreur Sigaction ! \n");
- exit(1);
- }
- sig_pour_timer.sigev_signo = SIGUSR1;
- sig_pour_timer.sigev_notify = SIGEV_SIGNAL;
- sig_act1.sa_handler = Handler_Timers;
- sig_act1.sa_flags = SA_SIGINFO;
- if((ret_val = sigaction(SIGUSR1, &sig_act1, 0)) == -1)
- {
- perror("\nErreur Sigaction ! \n");
- exit(1);
- }
- //*********************************************************************************
- // CREATION D'UN TIMER
- // IDENTIFICATEUR : timer_id, etc.
- // HORLOGE : CLOCK_REALTIME
- // STRUCTURE SIGEVENT : Sig par defaut (SIGALARM), sig_pour_timer pour le timer one shot
- //
- //*******************************************************************************
- if((timer_create(CLOCK_REALTIME, 0, &timer_id_Ten)) == -1) //10 secondes
- {
- perror("Erreur creation timer\n");
- exit(1);
- }
- timer_Ten.it_value.tv_sec = 10;
- timer_Ten.it_value.tv_nsec = 0;
- timer_Ten.it_interval.tv_sec = 0;
- timer_Ten.it_interval.tv_nsec = 0;
- if((timer_create(CLOCK_REALTIME, &sig_pour_timer, &timer_id)) == -1) //one shot 2 secondes
- {
- perror("Erreur creation timer\n");
- exit(1);
- }
- timer_def.it_value.tv_sec = 2;
- timer_def.it_value.tv_nsec = 0;
- timer_def.it_interval.tv_sec = 0;
- timer_def.it_interval.tv_nsec = 0;
- if((timer_create(CLOCK_REALTIME, 0 , &timer_id_Inter_SIG)) == -1) //5 secondes secondes
- {
- perror("Erreur creation timer\n");
- exit(1);
- }
- timer_Sig.it_value.tv_sec = 0;
- timer_Sig.it_value.tv_nsec = 1;
- timer_Sig.it_interval.tv_sec = 0;
- timer_Sig.it_interval.tv_nsec = 500000000;//500000000
- while(1)
- {
- //*********************************************************************************
- // INIT D'UN TIMER
- // IDENTIFICATEUR : timer_id,etc.
- // FLAGS : RELATIVE_TIMER
- // TIMINGS itermspec : timer_def, etc.
- //
- //*******************************************************************************
- puts("DEBUT 10 SEC");
- if((timer_settime(timer_id_Ten, RELATIVE_TIMER, &timer_Ten, 0)) == -1)
- {
- perror("Erreur init. timer 10\n");
- exit(1);
- }
- puts("DEBUT 2 SEC");
- if((timer_settime(timer_id, RELATIVE_TIMER, &timer_def, 0)) == -1)
- {
- perror("Erreur init. timer 2 \n");
- exit(1);
- }
- timer_def.it_value.tv_sec = 2;
- timer_gettime(timer_id_Ten,&timer_Ten_Wait);
- while(timer_Ten_Wait.it_value.tv_sec!=0 && timer_Ten_Wait.it_value.tv_nsec!=0)
- {
- timer_gettime(timer_id_Ten,&timer_Ten_Wait);
- }
- puts("FIN TIMER 10S");
- timer_Ten.it_value.tv_sec = 10;
- }
- return EXIT_SUCCESS;
- }
- /**********************************************************************************/
- /*GESTIONNAIRE DE SIGNAUX**********************************************************/
- /**********************************************************************************/
- void Handler_Signaux(int signal, siginfo_t *siginfo_hdler, int *val)
- {
- printf("\nHandler : Signal : %d, %d",signal, Occur);
- puts("");
- printf("Handler : si_signo : %d",siginfo_hdler->si_signo);
- printf("\nHandler : si_code : %d",siginfo_hdler->si_code);
- printf("\nHandler : si_value : %d\n",siginfo_hdler->si_value.sival_int);
- Occur++;
- }
- void Handler_Timers(int signal, siginfo_t *siginfo_hdler, int *val)
- {
- int tmp = 0, bou = 0;
- Occur++;
- printf("\nHandler : Signal : %d",signal);
- puts("");
- printf("Handler : si_signo : %d",siginfo_hdler->si_signo);
- printf("\nHandler : si_code : %d",siginfo_hdler->si_code);
- printf("\nHandler : si_value : %d\n",siginfo_hdler->si_value.sival_int);
- if((timer_settime(timer_id_Inter_SIG, 0, &timer_Sig, 0)) == -1)
- {
- perror("Erreur init. timer 5\n");
- exit(1);
- }
- tmp = Occur;
- while(bou < 10)
- {
- bou = Occur - tmp;
- if(bou > 10)
- {
- puts("");
- }
- }
- timer_Sig.it_interval.tv_nsec = 0;
- if((timer_settime(timer_id_Inter_SIG, RELATIVE_TIMER, &timer_Sig, 0)) == -1)
- {
- perror("Erreur init. timer 5\n");
- exit(1);
- }
- timer_Sig.it_interval.tv_nsec = 500000000;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement