Facebook
From Crippled Echidna, 7 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 232
  1. #include <time.h>
  2. #include <sys/siginfo.h>
  3. #include <sys/netmgr.h>
  4. #include <sys/neutrino.h>
  5. #include <sys/time.h>
  6. #include <pthread.h>
  7. #include <stdio.h>
  8. #include <signal.h>
  9.  
  10. #define MY_PULSE_CODE1   1
  11. #define MY_PULSE_CODE2   2
  12.  
  13. //struktury potrzebne do obliczania czasu od uruchomienia programu
  14. struct timeval startTime, tempTime;
  15. int seconds=0;
  16. long elapsed;
  17.  
  18. typedef union {
  19.     struct _pulse pulse;
  20. } my_message_t;
  21.  
  22. pthread_mutex_t m;
  23.  
  24. timer_t timer1_id, timer2_id, timer3_id, timer4_id;
  25. int chid;
  26. int coid2, coid3;
  27.  
  28. //funkcja wyswietlajaca komunikat wraz z czasem (sekundy:milisekundy)
  29. void say(char* msg)
  30. {
  31.     gettimeofday(&tempTime, NULL);
  32.     elapsed = ((tempTime.tv_sec-startTime.tv_sec)*1000000 + tempTime.tv_usec-startTime.tv_usec)/1000;
  33.     seconds = elapsed/1000;
  34.     elapsed = elapsed%1000;
  35.    
  36.     printf("%d:%03ld - %s\n",seconds,elapsed , msg);
  37. }
  38.  
  39. //watek timera 1.
  40. void* thread1(void* arg)
  41. {
  42.     pthread_mutex_lock(&m);
  43.     say("T1 - thread triggered");
  44.     pthread_mutex_unlock(&m);
  45. }
  46.  
  47. //funkcja obslugi sygnalu przerwania
  48. void signal_callback_handler(int signum)
  49. {
  50.  
  51.     pthread_mutex_lock(&m);
  52.     say("T4 - interrupt signal got");
  53.     timer_delete(timer1_id);
  54.     timer_delete(timer2_id);
  55.     timer_delete(timer3_id);
  56.     ConnectDetach(coid2);
  57.     ConnectDetach(coid3);
  58.     ChannelDestroy(chid);
  59.     pthread_mutex_unlock(&m);
  60.     //nie zabija watkow wywolanych przez T1, musza wypisac co swoje, dopiero zakancza program
  61.     exit(0);
  62. }
  63.  
  64. int main()
  65. {
  66.     struct sigevent         event1, event2, event3, event4;
  67.     struct itimerspec       itime1, itime2, itime3, itime4;
  68.     int                     rcvid;
  69.     my_message_t            msg;
  70.  
  71.     char                    *message;
  72.    
  73.     signal(SIGINT, signal_callback_handler);
  74.      
  75.     chid = ChannelCreate(0);
  76.  
  77.     /***************************
  78.      * EVENT 1
  79.      * ************************/
  80.     SIGEV_THREAD_INIT (&event1, (void*)thread1, &timer1_id, NULL);
  81.     /**************************
  82.      * TIMER 1
  83.      * ************************/
  84.     itime1.it_value.tv_sec = 0;
  85.     /* 500 million nsecs = 0.5s */
  86.     itime1.it_value.tv_nsec = 500000000;
  87.     itime1.it_interval.tv_sec = 0;
  88.     /* 500 million nsecs = 0.5s */
  89.     itime1.it_interval.tv_nsec = 500000000;
  90.     /***************************
  91.      * EVENT 2
  92.      * ************************/
  93.     coid2 = ConnectAttach(ND_LOCAL_NODE, 0, chid, _NTO_SIDE_CHANNEL, 0);
  94.     SIGEV_PULSE_INIT (&event2, coid2, getprio(0), MY_PULSE_CODE1, NULL);
  95.     /**************************
  96.      * TIMER 2
  97.      * ************************/
  98.     itime2.it_value.tv_sec = 2;
  99.     itime2.it_value.tv_nsec = 0;
  100.     itime2.it_interval.tv_sec = 2;
  101.     itime2.it_interval.tv_nsec = 0;
  102.     /***************************
  103.      * EVENT 3
  104.      * ************************/
  105.     coid3 =  ConnectAttach(ND_LOCAL_NODE, 0, chid, _NTO_SIDE_CHANNEL, 0);
  106.     SIGEV_PULSE_INIT (&event3, coid3, getprio(0), MY_PULSE_CODE2, NULL);
  107.     /**************************
  108.      * TIMER 3
  109.      * ************************/
  110.     itime3.it_value.tv_sec = 5;
  111.     itime3.it_value.tv_nsec = 0;
  112.     itime3.it_interval.tv_sec = 1;
  113.     itime3.it_interval.tv_nsec = 0;
  114.     /***************************
  115.      * EVENT 4
  116.      * ************************/
  117.     SIGEV_SIGNAL_INIT (&event4, SIGINT);
  118.     /**************************
  119.      * TIMER 4
  120.      * ************************/
  121.     itime4.it_value.tv_sec = 10;
  122.     itime4.it_value.tv_nsec = 0;
  123.     itime4.it_interval.tv_sec = 0;
  124.     itime4.it_interval.tv_nsec = 0;
  125.  
  126.     //tworzymy czasomierze i ustawiamy je majac gotowe struktury z ustawieniami
  127.     timer_create(CLOCK_REALTIME, &event1, &timer1_id);
  128.     timer_create(CLOCK_REALTIME, &event2, &timer2_id);
  129.     timer_create(CLOCK_REALTIME, &event3, &timer3_id);
  130.     timer_create(CLOCK_REALTIME, &event4, &timer4_id);
  131.  
  132.     timer_settime(timer1_id, 0, &itime1, NULL);
  133.     timer_settime(timer2_id, 0, &itime2, NULL);
  134.     timer_settime(timer3_id, 0, &itime3, NULL);
  135.     timer_settime(timer4_id, 0, &itime4, NULL);
  136.  
  137.     //czas rozpoczecia
  138.     gettimeofday(&startTime, NULL);
  139.    
  140.     while(1)
  141.     {
  142.         rcvid = MsgReceive(chid, &msg, sizeof(msg), NULL);
  143.         if(rcvid == 0)
  144.         {
  145.             if(msg.pulse.code == MY_PULSE_CODE1)
  146.             {
  147.                 pthread_mutex_lock(&m);
  148.                 say("T2");
  149.                 pthread_mutex_unlock(&m);
  150.             }
  151.             else if(msg.pulse.code == MY_PULSE_CODE2)
  152.             {
  153.                 pthread_mutex_lock(&m);
  154.                 say("T3 - third");
  155.                 pthread_mutex_unlock(&m);
  156.             }
  157.         }
  158.     }
  159.  
  160.     return 0;
  161. }