Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <time.h>
- #include <signal.h>
- #include <stdio.h>
- #include <pthread.h>
- #include <sched.h>
- #include <sys/time.h>
- #include <stdlib.h>
- #define NUMTHRDS 3 // 3 threads
- pthread_t t [NUMTHRDS]; //thread array
- typedef struct threadinfo{
- int signal_number;
- sigset_t timer_signal;
- int missed_Signal_count;
- timer_t timerID;
- int timer_Period;
- } ThreadArgs;
- //------------------------------------------------------------------------------
- void CreateAndArmTimer(int unsigned period, ThreadArgs* threadinfo){
- //period can be: 1,2, or 4
- //threadInfo is struct at top
- static int CurrSignal = 0; //checks if CurrSignal is 0. If not..
- if (!CurrSignal){
- CurrSignal = SIGRTMIN;//CurrSignal = min signal
- }else{
- CurrSignal++;
- }
- //initializing the struct values
- threadinfo->signal_number = CurrSignal;
- threadinfo->missed_Signal_count = 0;
- threadinfo->timer_Period = period;
- 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.
- sigaddset(&(threadinfo->timer_signal), CurrSignal);//The sigaddset() function adds the individual signal specified by the signo to the signal set pointed to by set.
- int timer_settime(timerid,
- // Create a timer associated with real-time signal
- struct sigevent mySignalEvent;
- mySignalEvent.sigev_notify = SIGEV_SIGNAL;
- mySignalEvent.sigev_signo = threadInfo->signalValue; //A value from SIGRTMIN and SIGRTMAX
- mySignalEvent.sigev_value.sival_ptr = (void *)&(threadInfo->timerId);
- ret = timer_create (CLOCK_MONOTONIC, &mySignalEvent, &threadInfo ->timerId);
- // Arm the timer for a period defined in microseconds
- struct timespec {
- time_t tv_sec; //seconds
- long tv_nsec; //nanoseconds
- }timerspec;
- seconds = period/1000000;
- nanoseconds = (period - (seconds * 1000000)) * 1000;
- timerSpec.it_interval.tv_sec = seconds;
- timerSpec.it_interval.tv_nsec = nanoseconds;
- timerSpec.it_value.tv_sec = seconds;
- timerSpec.it_value.tv_nsec = nanoseconds;
- ret = timer_settime (threadInfo ->timer_id, 0, & timerSpec, NULL);
- */
- }
- //------------------------------------------------------------------------------
- /*
- WaitForTimer(ThreadArgs* threadinfo){
- sigwait(); to wait on the timer_signal
- update missed signal count -> timer_getoverrun();
- }
- */
- //------------------------------------------------------------------------------
- void *thread0(void *_){
- ThreadArgs *threadinfo =_;
- int unsigned period = 1;
- printf("hello1 \n");
- CreateAndArmTimer(period, threadinfo);
- //printf("Thread[0] \t Timer Delta [%i]us \t Jitter [%i]us", int Delta, int Jitter);
- return NULL;
- }
- void *thread1(void * _){
- ThreadArgs *threadinfo =_;
- int unsigned period = 2;
- printf("hello2 \n");
- CreateAndArmTimer(period, threadinfo);
- //printf("Thread[1] \t Timer Delta [%i]us \t Jitter [%i]us", int Delta, int Jitter);
- return NULL;
- }
- void *thread2(void * _){
- ThreadArgs *threadinfo =_;
- int unsigned period = 4;
- printf("hello3 \n");
- CreateAndArmTimer(period, threadinfo);
- //printf("Thread[2] \t Timer Delta [%i]us \t Jitter [%i]us", int Delta, int Jitter);
- return NULL;
- }
- //------------------------------------------------------------------------------
- int main(int argc, char *argv[]){
- ThreadArgs threadinfo;
- struct ThreadArgs *ptr;
- //itimerspec timerSpec;
- malloc(sizeof(ThreadArgs));//giving the correct amount of bytes for the struct to be built
- pthread_t t1;
- pthread_t t2;
- pthread_t t3;
- pthread_create(&t1, NULL, thread0, &threadinfo);
- pthread_create(&t2, NULL, thread1, &threadinfo);
- pthread_create(&t3, NULL, thread2, &threadinfo);
- pthread_exit(NULL);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement