1 Replies - 311 Views - Last Post: 05 December 2012 - 09:59 AM Rate Topic: -----

#1 zOption  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-December 12

Realtime timer assginment

Posted 05 December 2012 - 02:41 AM

Hello,

I am working at an assignment right now, in which the goal is to adapt the function setperiodic() in the code given below so that it starts a timer which has a starting time of 3 seconds and a periodic cycle time of 2.5 seconds:

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

/* Prototypes */
static void interrupthandler();
static int  setinterrupt();
static int  setperiodic();
int         main();


/* Defines */

#define BILLION 1000000000L
#define TIMER_MSG "Received Timer Interrupt"


/* File-local Variables */

int       i_r_counter=1;
timer_t timerid;


static void interrupthandler(int signo, siginfo_t *info, void *context)
{
    int errsave;

    errsave = errno;
    printf("%s %d\n",TIMER_MSG, i_r_counter++);
    errno = errsave;
}


static int setinterrupt(void) 
{
    struct sigaction act;

    act.sa_flags = SA_SIGINFO;
    act.sa_sigaction = interrupthandler;
    if ((sigemptyset(&act.sa_mask) == -1) ||
        (sigaction(SIGALRM, &act, NULL) == -1)) {
        return -1;
    }
    return 0;
}


static int setperiodic(double sec) 
{
    struct itimerspec value;

    if (timer_create(CLOCK_REALTIME, NULL, &timerid) == -1) {
        return -1;
    }
    value.it_value.tv_sec = (long)sec;
    value.it_value.tv_nsec = (sec - value.it_value.tv_sec)*BILLION;
    if (value.it_value.tv_nsec >= BILLION) {
        value.it_value.tv_sec++;
        value.it_value.tv_nsec -= BILLION;
    }
    value.it_interval = value.it_value;
    return timer_settime(timerid, 0, &value, NULL);
}


int main(void) 
{
int loopcounter;
    if (setinterrupt() == -1) {
        perror("Fehler SIGALRM Händler Anbindung");
        return 1;
    }
    if (setperiodic(2.0) == -1) {
        perror("Fehler bei Start des periodischen Timers");
        return 1;
    }
    for (loopcounter=0; loopcounter < 5; loopcounter++){
        pause();
    }
    timer_delete(timerid);    
}


The progam has to be compiled with the realtime-library, example:
gcc -o timer timer.c -lrt

How can I achieve it that value.it_interval and value.it_value have different values? I think that it_interval sets an interval in which the timer gets called again if it runs out and it_value is responsible for how long the timer runs. But
value.it_interval = value.it_value;
should mean that the timer always gets called again if it runs out and I guess that is what has to be changed.

How can I achieve that? Do I have to add another argument to the function with which I can set the interval the timer should get called if it runs out or has it to be solved in another way?


Beside that, I have a secondary issue just for logical unterstanding:

The function setperiodic() has a double variable as an argument. Now through the following line tv_sec gets the whole second amount through an explicit long cast:
value.it_value.tv_sec = (long)sec;

and tv_nsec gets the nanosecond amount through:
value.it_value.tv_nsec = (sec - value.it_value.tv_sec)*BILLION;


Now with the following line:
if (value.it_value.tv_nsec >= BILLION)

I wonder, isn't tv_nsec always < BILLION or did I miss something?

I would appreciate any help with this assignment and thank you very much for your time and effort reading this.

Regards

zOption

Is This A Good Question/Topic? 0
  • +

Replies To: Realtime timer assginment

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 630
  • View blog
  • Posts: 2,107
  • Joined: 31-December 10

Re: Realtime timer assginment

Posted 05 December 2012 - 09:59 AM

Is setinterrupt() supposed to establish a signal handler for the SIGINT signal? If so, why are you establishing it for SIGALRM? Also, just so you know, there is a function int siginterrupt(int sig, int flag); defined in <signal.h>. Why are you using the SA_SIGINFO flag? In the handler you don't use any of the info, so is there a particular reason you're using that flag? Do you know that printf() is non-async-signal-safe so it shouldn't really be used inside a signal handler.

itimerspec.it_interval is the interval for the periodic timer, in this case, 2.5.
itimerspec.it_value specifies when the timer will first expire, 3 seconds.

*EDIT*: If one of the fields inside it_interval is non-zero, then the timer will be a periodic timer. If both fields are zero, the timer will expire once.

You should also make sure you always check the return values from any system call because if it fails, you might not be able to continue. The timer_settime() and timer_delete() calls both return -1 on failure.

This post has been edited by vividexstance: 05 December 2012 - 10:04 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1