PDA

View Full Version : Can I use multiple alarms() in my Program ?



kolichina_s_s
April 11th, 2006, 06:31 AM
HI,

I am new to linux just a few days back i ran my first c program in Linux. I need to use a timer kind of a thing in my program, i found the alarm() function with SIGALRM will serve the purpose. But what if i want to use more than one timer and if i use alarm() then the previous timer will be rescheduled I guess.. I am not sure about it Please help me on this.:confused:

I just want to do certain things on each timer like reading from a file and writing on to a file but at a cetain different intervals, is there any other way round for this ? , please help.

Thanks in Advance

LordHunter317
April 11th, 2006, 07:06 AM
What are you trying to time? Reading/writing asynchronously is better accomplished using select()/poll() or a similiar method.

You can have multiple alarms(), but you'll need to time them yourself and wrap the call to get the delivery semantics you want.

kolichina_s_s
April 11th, 2006, 08:12 AM
HI,

Thanks for the reply,


What are you trying to time? Reading/writing asynchronously is better accomplished using select()/poll() or a similiar method.


I need to read data from a serail port at a specified interval and put it on to a file.

also i need to send a query to the serail port device at a specified interval.


You can have multiple alarms(), but you'll need to time them yourself and wrap the call to get the delivery semantics you want.

if you can give any links on this alarm() programming than it will be gr8 help. cause the only book i am using is the Advanced Unix programming of 1992 :-? edition where it says that there can only be one timer per process. I know there are many changes since 1992 but i have no other go..

Thanks again

LordHunter317
April 11th, 2006, 02:59 PM
I need to read data from a serail port at a specified interval and put it on to a file.

also i need to send a query to the serail port device at a specified interval.My asynchronous I/O experience has shown me two things: Even if you're polling at specified inteverals, the timing may not be perfect and you may need to read/write ahead of schedule If you're doing both, the timing may not coincide Other issues don't want you blocking in the middle of nowhere for this.You do not want to use alarm() for this, trust me. Use select() or poll(), both of which can be handed a timeout, after placing the FDs in asynchronous mode. That way, if the port gets data early, you can read it to prevent a buffer overflow.


if you can give any links on this alarm() programming than it will be gr8 help. cause the only book i am using is the Advanced Unix programming of 1992 :-? edition where it says that there can only be one timer per process.Writing a wrapper around alarm() to figure out what the shortest alarm is, schedule it, and save the time of the next alarm(s) in a linked list; and writing a SIGALARM signal handler to schedule the next alarm (or force the main program to do it) is relatively straightforward.