Resources for writing Loop Driven Scheduling/Time Based System

I was wondering if someone could point me to some good resources for help on writing a monitoring system. It will be a loop driven system that needs to perform various tasks based on time intervals, with a prefered time granularity of 100ms. The piece I need help with is the loop itself and how to manage the time cycles and etc..  I have never written anything like this and was hoping there were some good sites/resources for a good starting point.

I desire to write this in C but C++ would also work.

Thanks,
rhugga
LVL 1
rhuggaAsked:
Who is Participating?
 
van_dyConnect With a Mentor Commented:
i havent actually done anything of this sort, but here are some ideas.

#include <sys/time.h>                /* for setitimer(), assuming *nix platform, 100ms is very "fine", alarm() wont help */
#include <signal.h>

#define  N      5                         /* assuming you will perform 5 tasks overtime */
void (*task[N])(void);              

void task1(void)
{
  /* define first task */
}

/* similarily define task2, task3, task4 ... taskN */

int taskno;                                  /* we receive SIGALRM every 100ms, so we increment this upon each SIGALRM*/  

struct itimerval   timer;               /* this is our timer structure, we recieve SIGALRM everytime timer expires */

void a_handler(int signo)           /* SIGALRM handler */
{
          taskno++;
}

int main()
{
          int turn = taskno;
          timer.it_interval.tv_sec = 0;
          timer.it_interval.tv_usec = 100000;             /* reset timer for 100ms */
          timer.it_value.tv_sec = 0;
          timer.it_value.tv_usec = 100000;                 /* generate SIGALRM after 100ms */

          task[0] = task1;                                            /* this will facilitate in calling the right task */
          task[1] = task2;
          ....
          task[N - 1] = taskN;
          setitimer(ITIMER_REAL, &timer, NULL);         /* initialize the timer, set it ticking */
          signal(SIGALRM, a_handler);

          for(;;){
                     if(turn != taskno){
                               turn = taskno;
                               (task[taskno % 5])(void);                /* do the task */
                      }
          }
}

there might be many issues overlooked in above, like what shall be done if timer expires while the program is
in a call to some task(). Also, the loop does not appear to be very efficient (it might be executed many times before
taskno is updated by an alarm if task() returned well in time). However i hope you get some general ideas from it.

hope this helps,
van_dy
0
 
SadrulConnect With a Mentor Commented:
using `pause' rather than looping all the time would perhaps be a better option:

for(;;)
{
    pause(); /* wait for a signal */

    /* we get here only after we have received a signal */
    if(turn != taskno)  /* just making sure we got the signal we are looking for */
    {
        ...
    }
}


-- Adil
0
All Courses

From novice to tech pro — start learning today.