how to solve this multiple linux timer with single signal handler ?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
how to solve this multiple linux timer with single signal handler ?
/*The handler checks that the value stored in sival_ptr matches a given timerID variable. The sival_ptr is the same as the one we set in makeTimer(), though here it lives in a different structure. Obviously, it got copied from there to here on the way to this signal handler. The point is that the timerID is what is used to determine which timer just went off and determine what to do next */
TASK1(Task2ms_Raster);
else if ( *tidp == secondTimerID )
TASK2(Task10ms_Raster);
else if ( *tidp == thirdTimerID )
TASK3(Task100ms_Raster);
}
/* The function takes a pointer to a timer_t variable that will be filled with the timer ID created by timer_create(). This pointer is also saved in the sival_ptr variable right before calling timer_create(). In this function notice that we always use the SIGRTMIN signal, so expiration of any timer causes this signal to be raised. The signal handler I've written for that signal is timerHandler. */
static int makeTimer( char *name, timer_t *timerID, int expireMS, int intervalMS )
{
//sigset_t mask;
struct sigevent te;
struct itimerspec its;
struct sigaction sa;
int sigNo = SIGRTMIN;
/* Set up signal handler. */
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = timerHandler;
sigemptyset(&sa.sa_mask);
if (sigaction(sigNo, &sa, NULL) == -1)
{
perror("sigaction");
}
/* Set and enable alarm */
te.sigev_notify = SIGEV_SIGNAL;
te.sigev_signo = sigNo;
te.sigev_value.sival_ptr = timerID;
timer_create(CLOCK_REALTIME, &te, timerID);
}
I am using single handler to call the task for every 2ms, 10ms and 100ms. I am using multiple timers with single signal handler. How to identify which timer just went. In debug mode : the last debug control session is : static void timerHandler( int sig, siginfo_t *si, void *uc ){ . Could anyone help me how to solve this ?
You can find more info here : http://www.graphics-muse.org/wp/?p=868
Last edited by hemanth1989; 02-27-2014 at 04:55 AM.
if we are printing some values or doing some calcuation then it is working fine but if you perform memory allocating then it is throqing error.
i am creating multiple timers, say 3 timers from a single process and all are supposed to land in single Timer handler.
But its giving bus error and "si->si_value.sival_ptr" ==> is of junk value and some how it is getting corrupted.
Please help me out.
Last edited by hemanth1989; 02-27-2014 at 03:37 PM.
i am creating multiple timers, say 3 timers from a single process (Ex: main ()). and all are supposed to land in single Timer handler.
But its giving bus error and "si->si_value.sival_ptr" ==> is of junk value and some how it is getting corrupted.
Please help me out.
That code which I placed there is literally your original code as posted with some modifications and it does exactly what you say, it creates 3 timers from a single process and it's not giving any bus error.
First, compile what I posted for you and see for yourself.
Second, if you're having trouble converting that example into your own specific case, then post exactly your code, the whole file, within [code][/code] tags, and also post representative output either from compiler/link errors you can't get past, or execution output which you can't get past, also within [code][/code] tags.
Note also that one does not need to have timer tasks as yet to prove that this works, I've done that with printf() statements. Therefore try that first, debug getting three timers working. This can also all be debugged using gdb, just put -ggdb within your compilation line and invoke your program using gdb.
However, consider that I've already tested, and then copied/pasted a working copy in my reply.
/*The handler checks that the value stored in sival_ptr matches a given timerID
variable. The sival_ptr is the same as the one we set in makeTimer(),
though here it lives in a different structure.
Obviously, it got copied from there to here on the way to this signal handler.
The point is that the timerID is what is used to determine which timer just went off
and determine what to do next */
TASK1(Task2ms_Raster);
else if ( *tidp == secondTimerID )
TASK2(Task10ms_Raster);
else if ( *tidp == thirdTimerID )
TASK3(Task100ms_Raster);
}
/*
The function takes a pointer to a timer_t variable that will be filled with the
timer ID created by timer_create(). This pointer is also saved in the sival_ptr
variable right before calling timer_create(). In this function notice that we
always use the SIGRTMIN signal, so expiration of any timer causes this signal to
be raised. The signal handler I've written for that signal is timerHandler.
*/
static int makeTimer( char *name, timer_t *timerID, int expireMS, int intervalMS )
{
//sigset_t mask;
struct sigevent te;
struct itimerspec its;
struct sigaction sa;
int sigNo = SIGRTMIN;
/* Set up signal handler. */
memset(&sa, 0, sizeof(sa));
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = timerHandler;
sigemptyset(&sa.sa_mask);
if (sigaction(sigNo, &sa, NULL) == -1)
{
perror("sigaction");
}
/* Set and enable alarm */
te.sigev_notify = SIGEV_SIGNAL;
te.sigev_signo = sigNo;
te.sigev_value.sival_ptr = timerID;
timer_create(CLOCK_REALTIME, &te, timerID);
rc=recvfrom(acceptSocket,buf, 256, 0, (struct sockaddr*) &client, &len);
if(rc==0)
{
printf("Server has no connection..\n");
break;
}
if(rc==-1)
{
printf("something went wrong with data %s", strerror(errno));
break;
}
XcpIp_RxCallback( (uint16) rc, (uint8*) buf, (uint16) port );
callBackTimers(); // whenever it recieves the data, I am calling this to start the timer . I think this is not the right way do. Could you suggest me some solution?
if ((long)XcpPort==port){
sentbytes = sendto(acceptSocket,(char*)pBytes,(long)numBytes,0, (struct sockaddr*)&client, sizeof(client));
}
XcpIp_TxCallback(port,(uint16)sentbytes);
}
I am working on a client and server architecture. Server code is shown above and I created a socket to recieve the request from the client via the ip address and port number. Server is waiting for a request from the client and send a response back to the client. when ever it recieves data from the client, it should call the timer task, For that I also created timer to call the task for every 2ms, 10ms and 100ms.
The new_value->it_interval field specifies the period of the timer,
in seconds and nanoseconds. If this field is nonzero, then each time
that an armed timer expires, the timer is reloaded from the value
specified in new_value->it_interval. If new_value->it_interval
specifies a zero value then the timer expires just once, at the time
specified by it_value.
This same information applies to timer_create() however that man page does not discuss it_interval.
I would say zero because you are always calling timer_create(), therefore you need no reloading of the timer, you always require the timers to be one-shot.
I believe so, and you should be trying this code versus repeatedly asking about syntax for something you were shown the reference too. Bear in mind that this may not be your sole problem, it was something I noticed. In the future, if you have problems, use the proper tags to show your code, those being [code][/code] tags. You can see the obvious result if you preview your post, and also in my earlier answers. The other thing to do is not show all your code, say "Help Me!", and then leave it at that, but instead to try to be as clear as possible exactly where you're stuck. Finally, if you're not showing the output, or a detailing of your steps to diagnose this yourself, eventually you're not going to get further help. Guess what? I'm at that point, you're repeatedly asking me about it_interval when it was explained in that post showing the manual page.
How to have multiple timers as well as different handler functions as well..
For example..
i would like to have multiple timers like
makeTimer(timerid1,func1,intervalMs);
makeTimer(timerid2,func2,intervalMs);
makeTimer(timerid3,func3,intervalMs);
and func1,func2,func3 should be called respectively on their timer expiry time...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.