Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  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.  
  8. #define NUMTHRDS 3 // 3 threads
  9. pthread_t t [NUMTHRDS]; //thread array
  10.  
  11. typedef struct threadinfo{
  12. int signal_number;
  13. sigset_t timer_signal;
  14. int missed_Signal_count;
  15. timer_t timerID;
  16. int timer_Period;
  17. } ThreadArgs;
  18.  
  19.  
  20.  
  21. //------------------------------------------------------------------------------
  22.  
  23.  
  24. void CreateAndArmTimer(int unsigned period, ThreadArgs* threadinfo){
  25. static int CurrSignal = 0;
  26. if (!CurrSignal){
  27. CurrSignal = SIGRTMIN;
  28. }else{
  29. CurrSignal++;
  30. }
  31.  
  32.  
  33. threadinfo->signal_number = CurrSignal;
  34. threadinfo->missed_Signal_count = 0;
  35. threadinfo->timer_Period = period;
  36. //threadinfo->timer_signal = 0;
  37. //threadinfo->timerID = 0;
  38. int sigemptyset(threadinfo->timer_signal);
  39. int sigaddset(threadinfo->timer_signal);
  40.  
  41.  
  42.  
  43.  
  44. //create signal mask for the signal number chose.. in timer_signal
  45. //sigemptyset();
  46. //signewset();
  47. /*
  48. //plug in create timer function here
  49. // Create a timer associated with real-time signal
  50. mySignalEvent.sigev_notify = SIGEV_SIGNAL;
  51. mySignalEvent.sigev_signo = threadInfo->signalValue; //A value b/t SIGRTMIN and SIGRTMAX
  52. mySignalEvent.sigev_value.sival_ptr = (void *)&(threadInfo->timerId);
  53. ret = timer_create (CLOCK_MONOTONIC, &mySignalEvent, &threadInfo ->timerId);
  54. // Arm the timer for a period defined in microseconds
  55. seconds = period/1000000;
  56. nanoseconds = (period - (sec * 1000000)) * 1000;
  57. timerSpec.it_interval.tv_sec = seconds;
  58. timerSpec.it_interval.tv_nsec = nanoseconds;
  59. timerSpec.it_value.tv_sec = seconds;
  60. timerSpec.it_value.tv_nsec = nanoseconds;
  61. ret = timer_settime (threadInfo ->timer_id, 0, & timerSpec, NULL);
  62. */
  63. }
  64.  
  65. //------------------------------------------------------------------------------
  66.  
  67. /*
  68. WaitForTimer(ThreadArgs* threadinfo){
  69. sigwait(); to wait on the timer_signal
  70. update missed signal count -> timer_getoverrun();
  71. }
  72. */
  73.  
  74. //------------------------------------------------------------------------------
  75.  
  76. void *thread0(void *_){
  77. int unsigned period = 1;
  78. printf("hello1 \n");
  79. CreateAndArmTimer(period, threadinfo);
  80. //printf("Thread[0] \t Timer Delta [%i]us \t Jitter [%i]us", int Delta, int Jitter);
  81. return NULL;
  82. }
  83. void *thread1(void * _){
  84. int unsigned period = 2;
  85. printf("hello2 \n");
  86. CreateAndArmTimer(period, threadinfo);
  87. //printf("Thread[1] \t Timer Delta [%i]us \t Jitter [%i]us", int Delta, int Jitter);
  88. return NULL;
  89. }
  90. void *thread2(void * _){
  91. int unsigned period = 4;
  92. printf("hello3 \n");
  93. CreateAndArmTimer(period, threadinfo);
  94. //printf("Thread[2] \t Timer Delta [%i]us \t Jitter [%i]us", int Delta, int Jitter);
  95. return NULL;
  96. }
  97. //------------------------------------------------------------------------------
  98.  
  99. int main(int argc, char *argv[]){
  100. ThreadArgs threadinfo;
  101. pthread_t t1;
  102. pthread_t t2;
  103. pthread_t t3;
  104.  
  105.  
  106. pthread_create(&t1, NULL, thread0, &threadinfo);
  107. pthread_create(&t2, NULL, thread1, &threadinfo);
  108. pthread_create(&t3, NULL, thread2, &threadinfo);
  109.  
  110.  
  111. pthread_exit(NULL);
  112.  
  113. return 1;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement