[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Resources for writing Loop Driven Scheduling/Time Based System

Posted on 2004-10-23
4
Medium Priority
?
328 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:rhugga
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 5

Accepted Solution

by:
van_dy earned 152 total points
ID: 12390440
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
 
LVL 2

Assisted Solution

by:Sadrul
Sadrul earned 148 total points
ID: 12435839
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question