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:

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.

Replies To: Realtime timer assginment

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