#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
typedef unsigned int uint32;
#define million 1000000L
double duration2ms, duration10ms, duration100ms;
double Task2ms_Raster, Task10ms_Raster, Task100ms_Raster;
timer_t firstTimerID, secondTimerID, thirdTimerID;
void TASK1(Task2ms_Raster)
{
struct timespec start, stop;
double StartTime, StopTime;
int a=1, b=3,c;
if( (StartTime = clock_gettime( CLOCK_REALTIME, &start)) == -1 )
{
perror("clock gettime");
}
StartTime =start.tv_sec + 0.0000001 * start.tv_nsec;
printf("start time is %lf", StartTime);
printf("value is %d",c);
printf("ETAS1\n");
if( (StopTime = clock_gettime( CLOCK_REALTIME, &stop)) == -1 )
{
perror( "clock gettime" );
}
StopTime = stop.tv_sec + 0.0000001 * stop.tv_nsec;
printf("stop time is %lf", StopTime);
duration2ms = StopTime - StartTime;
printf( "time difference is= %lf\n", duration2ms );
}
void TASK2(Task10ms_Raster)
{
int a,b,c;
struct timespec start, stop;
double StartTime, StopTime;
if( clock_gettime( CLOCK_REALTIME, &start) == -1 ) {
perror( "clock gettime" );
}
StartTime =start.tv_sec + 0.0000001 * start.tv_nsec;
printf("start time is %lf", StartTime);
printf("ETAS2\n");
if( (StopTime = clock_gettime( CLOCK_REALTIME, &stop)) == -1 )
{
perror( "clock gettime" );
}
StopTime = stop.tv_sec + 0.0000001 * stop.tv_nsec;
printf("stop time is %lf", StopTime);
duration10ms = ( stop.tv_sec - start.tv_sec )
+ (double)( stop.tv_nsec - start.tv_nsec )
/ (double)million;
printf( "time difference is= %lf\n", duration10ms );
}
void TASK3(Task100ms_Raster)
{
int a,b,c;
struct timespec start, stop;
double StartTime, StopTime;
if( clock_gettime( CLOCK_REALTIME, &start) == -1 ) {
perror( "clock gettime" );
}
StartTime =start.tv_sec + 0.0000001 * start.tv_nsec;
printf("start time is %lf", StartTime);
printf("value is %d",c);
printf("ETAS1\n");
if( (StopTime = clock_gettime( CLOCK_REALTIME, &stop)) == -1 )
{
perror( "clock gettime" );
}
StopTime = stop.tv_sec + 0.0000001 * stop.tv_nsec;
printf("stop time is %lf", StopTime);
duration100ms = StopTime -StartTime;
printf( "time difference is= %lf\n", duration100ms );
}
static void timerHandler( int sig, siginfo_t *si, void *uc )
{
timer_t *tidp;
tidp = si->si_value.sival_ptr;
if ( *tidp == firstTimerID )
TASK1(Task2ms_Raster);
else if ( *tidp == secondTimerID )
TASK2(Task10ms_Raster);
else if ( *tidp == thirdTimerID )
TASK3(Task100ms_Raster);
}
static int makeTimer( char *name, timer_t *timerID, int expireMS, int intervalMS )
{
struct sigevent te;
struct itimerspec its;
struct sigaction sa;
int sigNo = SIGRTMIN;
/* Set up signal handler. */
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = timerHandler;
sigemptyset(&sa.sa_mask);
if (sigaction(sigNo, &sa, NULL) == -1)
{
perror("sigaction");
}
/* Set and enable alarm */
te.sigev_notify = SIGEV_SIGNAL;
te.sigev_signo = sigNo;
te.sigev_value.sival_ptr = timerID;
timer_create(CLOCK_REALTIME, &te, timerID);
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = intervalMS * 100000;
its.it_value.tv_sec = 0;
its.it_value.tv_nsec = expireMS * 100000;
timer_settime(*timerID, 0, &its, NULL);
return 1;
}
int main()
{
makeTimer("First Timer", &firstTimerID, 2, 2); //2ms
makeTimer("Second Timer", &secondTimerID, 10, 10); //10ms
makeTimer("Third Timer", &thirdTimerID, 100, 100); //100ms
while(1)
;;
}
I created a timer to call the task for every 2ms, 10ms and 100ms. I am using a handler to handle task. The above code is not interrupting the task2 at exact 10ms, and task3 at 100ms. It is not interrupting at a right position and the output is shown below.
output:
10ms
2ms
2ms
10ms
2ms
10ms
2ms
2ms
10ms
2ms
10ms
100ms
2ms
10ms
2ms
10ms
2ms
10ms
2ms
10ms
2ms
10ms
2ms
10ms
2ms
10ms
100ms
2ms
10ms
2ms
10ms
2ms
10ms
2ms
10ms
2ms
10ms
100ms
2ms
10ms
2ms
2ms
10ms
2ms
2ms
10ms
2ms
2ms
10ms
2ms
10ms
2ms
2ms
10ms
100ms
2ms
2ms
10ms
2ms
10ms
2ms
2ms
10ms
2ms
10ms
2ms
10ms
2ms
10ms
2ms
10ms
100ms
2ms
10ms
2ms
10ms
2ms
10ms
2ms
10ms
2ms
what is the reason ?