daily pastebin goal
26%
SHARE
TWEET

Untitled

a guest Nov 21st, 2017 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <time.h>
  2. #include <signal.h>
  3. #include <stdio.h>
  4. #include <pthread.h>
  5. #include <sched.h>
  6. #include <sys/time.h>
  7. #include <stdlib.h>
  8.  
  9. #define NUMTHRDS 3  // 3 threads
  10. pthread_t t [NUMTHRDS]; //thread array
  11.  
  12. typedef struct threadinfo{
  13.     int signal_number;
  14.     sigset_t timer_signal;
  15.     int missed_Signal_count;
  16.     timer_t timerID;
  17.     int timer_Period;
  18. } ThreadArgs;
  19.  
  20.  
  21.  
  22.  
  23. //------------------------------------------------------------------------------
  24.  
  25.  
  26. void CreateAndArmTimer(int unsigned period, ThreadArgs* threadinfo){
  27. //period can be: 1,2, or 4
  28. //threadInfo is struct at top
  29.     static int CurrSignal = 0; //checks if CurrSignal is 0. If not..
  30.     if (!CurrSignal){          
  31.         CurrSignal = SIGRTMIN;//CurrSignal = min signal
  32.     }else{
  33.         CurrSignal++;
  34.     }
  35.    
  36.     //initializing the struct values
  37.     threadinfo->signal_number = CurrSignal;
  38.     threadinfo->missed_Signal_count = 0;
  39.     threadinfo->timer_Period = period;
  40.        
  41.  
  42.     sigemptyset(&(threadinfo->timer_signal));//The sigemptyset() function initialises the signal set pointed to by set, such that all signals defined in this document are excluded.
  43.     sigaddset(&(threadinfo->timer_signal), CurrSignal);//The sigaddset() function adds the individual signal specified by the signo to the signal set pointed to by set.
  44.    
  45.     int timer_settime(timerid,
  46.  
  47.     // Create a timer associated with real-time signal
  48.     struct sigevent mySignalEvent;
  49.    
  50.     mySignalEvent.sigev_notify = SIGEV_SIGNAL;
  51.     mySignalEvent.sigev_signo = threadInfo->signalValue; //A value from SIGRTMIN and SIGRTMAX
  52.     mySignalEvent.sigev_value.sival_ptr = (void *)&(threadInfo->timerId);
  53.     ret = timer_create (CLOCK_MONOTONIC, &mySignalEvent, &threadInfo ->timerId);
  54.  
  55.  
  56.     // Arm the timer for a period defined in microseconds
  57.     struct timespec {
  58.         time_t tv_sec; //seconds
  59.         long tv_nsec;   //nanoseconds
  60.  
  61.     }timerspec;
  62.     seconds = period/1000000;
  63.     nanoseconds = (period - (seconds * 1000000)) * 1000;
  64.     timerSpec.it_interval.tv_sec = seconds;
  65.     timerSpec.it_interval.tv_nsec = nanoseconds;
  66.     timerSpec.it_value.tv_sec = seconds;
  67.     timerSpec.it_value.tv_nsec = nanoseconds;
  68.     ret = timer_settime (threadInfo ->timer_id, 0, & timerSpec, NULL);
  69.  
  70. */
  71. }
  72.  
  73. //------------------------------------------------------------------------------
  74.  
  75. /*
  76. WaitForTimer(ThreadArgs* threadinfo){
  77.     sigwait(); to wait on the timer_signal
  78.     update missed signal count -> timer_getoverrun();
  79. }
  80. */
  81.  
  82. //------------------------------------------------------------------------------
  83.  
  84. void *thread0(void *_){
  85.     ThreadArgs *threadinfo =_;
  86.         int unsigned period = 1;
  87.         printf("hello1 \n");
  88.     CreateAndArmTimer(period, threadinfo);
  89.     //printf("Thread[0] \t Timer Delta [%i]us \t Jitter [%i]us", int Delta, int Jitter);
  90.     return NULL;
  91. }
  92. void *thread1(void * _){
  93.     ThreadArgs *threadinfo =_;
  94.         int unsigned period = 2;
  95.         printf("hello2 \n");
  96.     CreateAndArmTimer(period, threadinfo);
  97.     //printf("Thread[1] \t Timer Delta [%i]us \t Jitter [%i]us", int Delta, int Jitter);
  98.     return NULL;
  99. }
  100. void *thread2(void * _){
  101.     ThreadArgs *threadinfo =_;
  102.         int unsigned period = 4;
  103.         printf("hello3 \n");
  104.     CreateAndArmTimer(period, threadinfo);
  105.     //printf("Thread[2] \t Timer Delta [%i]us \t Jitter [%i]us", int Delta, int Jitter);
  106.     return NULL;
  107. }
  108. //------------------------------------------------------------------------------
  109.  
  110. int main(int argc, char *argv[]){
  111.     ThreadArgs threadinfo;
  112.     struct ThreadArgs *ptr;
  113.  
  114.     //itimerspec timerSpec;
  115.     malloc(sizeof(ThreadArgs));//giving the correct amount of bytes for the struct to be built
  116.  
  117.     pthread_t t1;
  118.     pthread_t t2;
  119.     pthread_t t3;
  120.    
  121.  
  122.    
  123.     pthread_create(&t1, NULL, thread0, &threadinfo);
  124.     pthread_create(&t2, NULL, thread1, &threadinfo);
  125.     pthread_create(&t3, NULL, thread2, &threadinfo);
  126.  
  127.        
  128.     pthread_exit(NULL);
  129.        
  130.     return 1;
  131. }
RAW Paste Data
Top