ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Here is what I want to do. I want to create a timer and set it to expire after certain time. When It expires I want to send some data to the signal handler. The data is multiple bytes (may be as much as 24 bytes). Here is how you can help me out:
1. Point me to some tutorial which will explain the intricacies of timer functionality in Linux.
2. Provide pointers to help me solve the problem.
Ya, look at the man page for setitimer. It'll allow you to set the timer, and it tells you what signal is fired when it runs out. You can then define what happens when you get that signal.
Thanks for all the good replies. "setitimer" may be too restrictive because from what I understand you can have no more than three interval timers per process. In my case I have several devices on which I will have to react based on the timers. My next questions are:
1. Is there a limitation to the number of timers per process/task I can create via timer_create() function?
2. If I can create a number of timers, I am still at a loss as to how I can associate the timer expiry to a particular device.
Well the way a timer like that works is it typically triggers a software interupt which causes a signal to be sent to the processor. You have to create a function that is suppose to run based on that signal. Are you using this for doing operations on hardware? Perhaps you should write a kernel module.
I assume this is a simple user-space program, not a kernel module, so let's keep it simple here...
1) Have a linked list of "timer" objects that contain references back to the particular device.
2) Keep the list sorted by the expiration time, most recent first.
3) Use setitimer to send a signal when the earliest expiration time will occur.
4) In the signal handler, walk through the timer list, calling an "expire" method against the object if the timeout has occurred. Stop when you hit a timer that hasn't expired.
5) Calculate the next interval and set your itimer.
I haven't used the qtimer classes, but I'll bet they are just what you are looking for.
If you need it, I can whip up a simple set of timer classes for an example.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.