Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Creating a timer for scheduling the task ??: (1 Item)
   
Creating a timer for scheduling the task ??  
I created a timer for scheduling a task for every 5sec and the code is as shown below.  
I am calling a task for every 5sec and printing hello. If i want to schedule another task for 10sec, 100 sec. Do i want 
to create a separate timer for the other two ??  how to call the task for every 5ms, 10ms and 100ms ??
how to calculate the start time and stop time for executing some task ?? Could anyone give me some ideas ??

#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>

/*
This function is called periodically.
The actual functionality has to be provided by yourself **/
void my_handler ( timer_t timerid)
{
	printf("\nHello World\n");
}

int main()
{

 // declare some variables
 timer_t timerid;
 struct itimerspec value;
 struct sigevent sevp;

 // initalize the sigevent variable
 // the memset is necessary to avoid segmentation faults
  memset (&sevp, 0, sizeof (struct sigevent));
  sevp.sigev_notify=SIGEV_THREAD;
  sevp.sigev_notify_function=(void *)my_handler;

 // initialize the timer specification
 // currently the offset and the period is 5 seconds  // you have to adjust this to your actual needs
  value.it_value.tv_sec = 5;
  value.it_value.tv_nsec = 0;
  value.it_interval.tv_sec = 5;
  value.it_interval.tv_nsec = 0;

 // create the timer
 timer_create (CLOCK_REALTIME, &sevp, &timerid);
 timer_settime (timerid, 0, &value, NULL);

 // this loop has to be adapted, so that the program ends after some time
 while(1)
 {
  sleep(100);
 }
 return 0;
}