How to implement polling for a function using timer in C?

Thread Tools Search this Thread
Top Forums Programming How to implement polling for a function using timer in C?
# 1  
Old 11-12-2007
How to implement polling for a function using timer in C?


Can you please help me in implementing a timer based polling for function in C? ie. the function should be called in say 30secs(when 30secs has lapsed).

# 2  
Old 11-12-2007
Use alarm(2) to set an alarm for 30 sec. When the timer runs out, a SIGALRM will be sent to your process. Catch that signal, do what you want, then set the alarm(2) again. Keep doing ad infinitum.
# 3  
Old 11-12-2007
Suppose if I have put the alarm in a function, and i need to catch the signal only inside the function, is it possible? ie. the process should be executing in the normal manner till it reaches this function and catches the signal.
# 4  
Old 11-12-2007
Originally Posted by naan
Can you please help me in implementing a timer based polling for function in C?
The two APIs designed for this are "select" (BSD) and "poll" (SYSV).

signal driven IO (SIGIO or SIGALRM) is very rarely used and hard to get right.

Then the modern approach is using select or poll in a thread.
# 5  
Old 11-14-2007
Is there any apprach apart from using threads since I have not worked with threads at all.
# 6  
Old 11-14-2007
Depends what it's supposed to fit in.

If you are writing a program from scratch then there is no problem with

     while (1)

If it's an Xt based client application then use one of the timer callback functions.

It all depends what framework it has to work in.
# 7  
Old 11-19-2007
This has to be implemented in a deamon process in UNIX system. The daemon waits for any messages in a message queue and parallely has to go and execute a function by polling in every 30secs.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Polling file

HI I need some help on this below one Have summuary file coming on daily basis with list of file names and count ,if the summuary file not exist ,pool it for every 5 mins till it arrives .Once arrived remove first and last line from the file and check all the files in respective director all... (12 Replies)
Discussion started by: mohan705
12 Replies

2. Shell Programming and Scripting

Trying to implement count_collatz_step function

Can anyone give me a clue why this code doesn't work as expected? The function count_collatz_step() take one parameter which a number that need to calculate collatz until it reaches 1. The func should return steps it takes. # # Count steps of collatz conjecture takes, until the number reach 1.... (8 Replies)
Discussion started by: Bunchhieng
8 Replies

3. UNIX for Dummies Questions & Answers

Implement the '&&' function in a shell

Hello, I'm currently implementing the && function in a shell using C. For example, if we input cmd1 && cmd2, then cmd2 executes only when cmd1 exits successfully. I'm thinking about: int main() { int i; char **args; while(1) { printf("yongfeng's shell:~$ "); args =... (5 Replies)
Discussion started by: Yongfeng
5 Replies

4. HP-UX

Polling target on Serviceguard

Hi Guys, we are planning to enable polling target on our SG in reference to this link MC/ServiceGuard: Subnet Goes Down when one of the Cluster Node is Down - HP Customer Care (United States - English) since our server is already in production can we apply the changes with ex. "cmapplyconf... (1 Reply)
Discussion started by: batas
1 Replies

5. Programming

Db polling...

Hi! Im trying to make a program that polls a mysqlDB every 5 minutes. the poll checks a scheduletable in the dB and exicutes a function if there was a "go" in the scheduletable, and if it retrives a "no go" from the table it should just wait for another 5 minutes before re polling the DB, ... (1 Reply)
Discussion started by: karlblau
1 Replies

6. Solaris

NTP polling interval

Is it possible to change the ntp poll manually. I notice that ntp poll is changing autimatically. (1 Reply)
Discussion started by: ningy
1 Replies

7. UNIX for Advanced & Expert Users

How to avoid polling???

Hi all, I have a directory where some process is keeping incremental/new log files. I need to code a program which will periodically poll this directory and if it founds a new file arrived then copy that new file to some other specific directory. We are OK with this polling approach. I just... (3 Replies)
Discussion started by: zing_foru
3 Replies

8. Shell Programming and Scripting

Want to implement VLOOKUP (Excel function) in Unix

Dear All, i want to implement vookup function which is there in excel into Unix. Suppose i have 2 files. The files are given below. File1: MSC Cell SDCA Patna-1 12 Bihar Patna-2 45 Ranchi Bhopal-1 85 Raigarh Bhopal-2 ... (8 Replies)
Discussion started by: pravani1
8 Replies

9. AIX

how to implement timer

anyone can help me how to implement the timer on AIX? I tried with 'setitimer' and its related functions, but it does not work correctly,the program exited each time. thanks (2 Replies)
Discussion started by: Frank2004
2 Replies

10. Programming

Help - Polling Script

How do I write a shell script to perform polling just like what happens with Microsoft mail. i.e display an alert box. (1 Reply)
Discussion started by: brianmu
1 Replies
Login or Register to Ask a Question