Timer in C

Hi there,

I need to have a simple timer that can do the following.  The timer should call a callback function once the timer expires.  Also, I should be able to stop it.  I want it to be in C.  Can someone provide some example.  thank you.
ambuliAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Is this school work?
0
ambuliAuthor Commented:
No:-) I am out of school for many years.  

I tried alarm() and it works, but I want a way to cancel the timer.  thanks.
0
phoffricCommented:
Haven't done this in Linux, but I found this for you:
— Function: unsigned int alarm (unsigned int seconds)

The alarm function sets the real-time timer to expire in seconds seconds. If you want to cancel any existing alarm, you can do this by calling alarm with a seconds argument of zero.

The return value indicates how many seconds remain before the previous alarm would have been sent. If there is no previous alarm, alarm returns zero.
http://www.gnu.org/software/libc/manual/html_node/Setting-an-Alarm.html
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

phoffricCommented:
This may be closer to what you are looking for:
alarm(3) - Linux man page
If seconds is 0, a pending alarm request, if any, is canceled.
http://linux.die.net/man/3/alarm
0
omarfaridCommented:
I have done this on solaris long time ago, and here is the code:

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <setjmp.h>

static void act() ;

char line[80] ;
int  rem_time ;
jmp_buf env ;

main()
{
      signal(SIGALRM , act) ;
      if (setjmp(env) == 0)
      {
            alarm(10) ;
            printf("Enter your name: ") ;
            fgets(line , 80 , stdin) ;
      }
      rem_time=alarm(0) ;
      printf("\n%s ; response time is : %d\n" , line , 10 - rem_time) ;
}

void act()
{
      strcpy(line , " missed it !!!") ;
      longjmp(env , 1) ;
}
0
jkrCommented:
Apart from 'alarm()', there's also 'setitimer()' (http://linux.about.com/library/cmd/blcmdl2_setitimer.htm), e.g.

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

#define LOOP_LIMIT  0x0000ffff

volatile int sigcount=0;

void catcher( int sig ) {

    struct itimerval value;
    int which = ITIMER_REAL;

    printf( "Signal catcher called for signal %d\n", sig );
    sigcount++;

    if( sigcount > 1 ) {

        /*
         * Disable the real time interval timer
         */

        getitimer( which, &value );

        value.it_value.tv_sec = 0;
        value.it_value.tv_usec = 0;

        setitimer( which, &value, NULL );
    }
}

int main( int argc, char *argv[] ) {

    int result = 0;

    struct itimerval value, ovalue, pvalue;
    int which = ITIMER_REAL;

    struct sigaction sact;
    volatile double count;
    time_t t;

    sigemptyset( &sact.sa_mask );
    sact.sa_flags = 0;
    sact.sa_handler = catcher;
    sigaction( SIGALRM, &sact, NULL );

    getitimer( which, &pvalue );

    /*
     * Set a real time interval timer to repeat every 200 milliseconds
     */

    value.it_interval.tv_sec = 0;        /* Zero seconds */
    value.it_interval.tv_usec = 200000;  /* Two hundred milliseconds */
    value.it_value.tv_sec = 0;           /* Zero seconds */
    value.it_value.tv_usec = 500000;     /* Five hundred milliseconds */

    result = setitimer( which, &value, &ovalue );

    /*
     * The interval timer value returned by setitimer() should be
     * identical to the timer value returned by getitimer().
     */

    if( ovalue.it_interval.tv_sec != pvalue.it_interval.tv_sec  ||
        ovalue.it_interval.tv_usec != pvalue.it_interval.tv_usec ||
        ovalue.it_value.tv_sec != pvalue.it_value.tv_sec ||
        ovalue.it_value.tv_usec != pvalue.it_value.tv_usec ) {
        printf( "Real time interval timer mismatch\n" );
        result = -1;
    }


    time( &t );
    printf( "Before loop, time is %s", ctime(&t) );

    for( count=0; ((count<LOOP_LIMIT) && (sigcount<2)); count++ );

    time( &t );
    printf( "After loop, time is %s\n", ctime(&t) );


    if( sigcount == 0 )
        printf( "The signal catcher never gained control\n" );
    else
        printf( "The signal catcher gained control\n" );

    printf( "The value of count is %.0f\n", count );

    return( result );
}

Open in new window


(code taken from http://publib.boulder.ibm.com/iseries/v5r1/ic2924/index.htm?info/apis/setitime.htm)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.