Does 'setitimer' library function work ??

From: <joe_at_resptk.bhp.com.au>
Date: Wed, 30 Aug 95 13:37:10 +1000

Hi all,

I have a problem of using the 'setitimer' library function. It just does not
want to work which I do not have the foggiest why. The following code sets up
a 'timer' to call a function every second. The code compiles ok, but fails
when the 'setitimer' function is called. The code works fine on Sun-Sparcs.

Would anybody know if the 'setitimer' library function has a bug ??

Or can anybody see if my code is suspect.

Thanks in advance. Joe

joe_at_resptk.bhp.com.au

-------------------------->>>>> code (test.c) <<<<<-----------------------------
#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#include <time.h>

/******************************************************************************/
/* Check Interrupt - check if have to stop and return from code */
/******************************************************************************/
void
check_interrupt()
{
        static int count = 0;

        printf("Got Blinked: %d", count++);

        return;
}

/******************************************************************************/
/* Setup Timer - routine to set up a repetitive timer for SIGALRM */
/******************************************************************************/
struct itimerval blink_timer;
int
setup_timer(useconds, seconds)
        int useconds;
        int seconds;
{
        int ret;

        /*
         * Set the interval which to Re-Load repeatedly the timer
         */
        blink_timer.it_interval.tv_usec = useconds;
        blink_timer.it_interval.tv_sec = seconds;

        /*
         * Set the initial value with which to set the timer
         */
        blink_timer.it_value.tv_usec = useconds;
        blink_timer.it_value.tv_sec = seconds;

        /*
         * Turn on the timer NOW
         */
        ret = setitimer(ITIMER_REAL, blink_timer, NULL);

        /*
         * Return 0 - setitimer set OK
         * Return -1 - setitimer FAILED
         */
        return ret;
}

/******************************************************************************/
/* Start Blinker - start the timer blinking on 1sec intervals */
/******************************************************************************/
int
start_blinker()
{
        int ret;

        /*
         * Set the timer to blink on 1sec intervals
         */
        ret = setup_timer(0, 1);
        if(ret == -1) {
                return -1;
        }

        signal(SIGALRM, check_interrupt);

        return 0;
}

/******************************************************************************/
/* Stop Blinker - stop the timer & remove signal trap */
/******************************************************************************/
void
stop_blinker()
{
        /*
         * Turn off timer
         */
        (void)setup_timer(0, 0);

        signal(SIGARLM, SIG_IGN);

        return;
}

/******************************************************************************/
/* Main Code */
/******************************************************************************/
main()
{
        if(start_blinker() == -1) {
                printf("Failed start_blinker\n");
        }

        for(;;) {
                pause();
        }
}
--------------------->>>>> end of code <<<<<<<<<<-------------------------------

How to compile:

        cc -o test test.c
Received on Wed Aug 30 1995 - 05:54:44 NZST

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:45 NZDT