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

New Topic/Question
Reply



MultiQuote



|