Resources for writing Loop Driven Scheduling/Time Based System

Posted on 2004-10-23
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.

Question by:rhugga
    LVL 5

    Accepted Solution

    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,
    LVL 2

    Assisted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

    933 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now