-
c posix timer
1. in case of posix timers, could the next fire cause interfere with function that has not been exited yet?. if timr period is short and before function exit, another fire occurs, what would happen?. they get queued?. could cause congestion?.
2.
Code:
struct itimerval t;
t.it_interval.tv_sec=0;
t.it_interval.tv_usec=500000;
t.it_value=t.it_inerval;
raise(SIGALRM);
settiner(TIMER_REAL,&t, NULL);
raise(SIGALRM) is called to start the timer immediately.
how to stop a timer immediately?. How to restart the timer after stopping?.
how to delete a timer?. How to restart the timer after deleting?.
-
> if timr period is short and before function exit, another fire occurs, what would happen?.
Signals are not queued.
Generally speaking, you should not be doing a lot of work inside signal handlers.
Code:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/signal.h>
#include <sys/time.h>
// https://en.wikipedia.org/wiki/Ackermann_function#Table_of_values
// https://www.geeksforgeeks.org/ackermann-function/
int ack(int m, int n)
{
if (m == 0){
return n+1;
}
else if((m > 0) && (n == 0)){
return ack(m-1, 1);
}
else if((m > 0) && (n > 0)){
return ack(m-1, ack(m, n-1));
}
}
// Only call things on this list from a signal handler
// https://man7.org/linux/man-pages/man7/signal-safety.7.html
void fire(int sig) {
static char in[] = "@-in\n";
static char out[] = "@-out\n";
write(1,in,strlen(in));
if ( ++in[0] > '~' ) in[0] = '@';
// Uncomment the next line to cause many events to be missed.
//ack(3,11); // burn way too much time in signal handler
write(1,out,strlen(out));
if ( ++out[0] > '~' ) out[0] = '@';
}
int main ( ) {
signal(SIGALRM,fire);
struct itimerval t;
t.it_interval.tv_sec=0;
t.it_interval.tv_usec=500000;
t.it_value = t.it_interval;
raise(SIGALRM);
setitimer(ITIMER_REAL, &t, NULL);
printf("Ack(3,11)=%d\n",ack(3,11));
printf("Ack(4,1)=%d\n",ack(4,1));
unsigned int u = sleep(5);
printf("Sleep returned %u\n", u);
return 0;
}
Uncomment line 30 for a totally different experience.
> how to stop a timer immediately?. How to restart the timer after stopping?.
> how to delete a timer?. How to restart the timer after deleting?.
The manual page has all these answers.
getitimer(3p) - Linux manual page
-
this is what i'm trying to do.
1. as soon as a flag is raised , need as instantaneously as possible to poll a status byte from serial port, then set timer off till next cycle. I found the fast timers suitable as it seems to be deterministic. But in my case when i make it too fast, the serial IO at low level crashes. I read handling the timers through threads is better than the signal handlers in these cases. I am not familiar with this yet.
2. replaced the timer with with a thread and works but pthread_create() may not be as instantaneous and deterministic as timers. I was thinking of calling pthread_create once and use some flag for stop/restart cycle. This has not worked satisfactorily yet.
any ...
-
So why don't you make your serial line unbuffered and call read() with a single char buffer from a thread?
The thread will block until there is a char available, and because it's unbuffered, it will return immediately with the char.