• C

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.

Who is Participating?
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 */

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);

                     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,
using `pause' rather than looping all the time would perhaps be a better option:

    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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.