Advertisement
VorMan

SCR6_1

Nov 18th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <signal.h>
  4.  
  5. #define MILI_SEC 1000000
  6. #define IGNORE_VALUE 1000
  7.  
  8. const long INTERVAL_MS = 500*MILI_SEC;
  9. unsigned int i=0,tmp=0;
  10. unsigned int RUNNING_FLAG = 1;
  11. unsigned int IGNORE_FLAG = 0;
  12.  
  13.  
  14. //Reakcje na sygnał
  15. void SIGALRM_handler(int signalNumber);
  16. void SIGTERM_handler(int signalNubmer);
  17. void SIGUSR1_handler(int signalNumber);
  18.  
  19. int main()
  20.  
  21. {
  22.    struct timespec sleepValue={0};
  23.    sleepValue.tv_nsec = INTERVAL_MS;
  24.  
  25.    //wyłapywanie sygnałów
  26.    signal(SIGALRM,SIGALRM_handler);
  27.    signal(SIGTERM,SIGTERM_handler);
  28.    signal(SIGUSR2,SIG_IGN); //ignorowanie syganłu
  29.    signal(SIGUSR1,SIGUSR1_handler);
  30.    
  31.   while(RUNNING_FLAG)
  32.     {
  33.          nanosleep(&sleepValue, NULL);
  34.      printf("%d    %d\n",i, IGNORE_FLAG);
  35.          ++i;
  36.      
  37.      
  38.      if(IGNORE_FLAG == 1){     
  39.      if(i>tmp+IGNORE_VALUE){
  40.        IGNORE_FLAG = 0;
  41.      }
  42.      }        
  43.     }
  44.   return 0;
  45. }
  46.  
  47.  
  48. void SIGALRM_handler(int signalNumber)
  49. {
  50.   printf("Wylapano sygnal: %d \n",signalNumber);
  51.   printf("Zakonczenie dzialania programu... \n");
  52.   RUNNING_FLAG=0;
  53.  
  54. }
  55.  
  56. void SIGTERM_handler(int signalNumber)
  57. {
  58.   printf("Wylapano sygnal: %d \n",signalNumber);
  59.  
  60. }
  61. void SIGUSR1_handler(int signalNumber)
  62. {
  63.   if(IGNORE_FLAG == 0) {
  64.     printf("Wylapano sygnal: %d \n",signalNumber);
  65.     IGNORE_FLAG = 1;
  66.     tmp=i;
  67.   }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement