Go Back   The UNIX and Linux Forums > Top Forums > Programming
.
google site



Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-11-2007
Registered User
 

Join Date: Jul 2006
Posts: 31
How to implement polling for a function using timer in C?

Hi,

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).

Thanks
Sponsored Links
  #2 (permalink)  
Old 11-11-2007
blowtorch's Avatar
AFK
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,351
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 (permalink)  
Old 11-12-2007
Registered User
 

Join Date: Jul 2006
Posts: 31
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 (permalink)  
Old 11-12-2007
porter porter is offline Forum Advisor  
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by naan View Post
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 (permalink)  
Old 11-14-2007
Registered User
 

Join Date: Jul 2006
Posts: 31
Is there any apprach apart from using threads since I have not worked with threads at all.
  #6 (permalink)  
Old 11-14-2007
porter porter is offline Forum Advisor  
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Depends what it's supposed to fit in.

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


Code:
main()
{
     while (1)
     {
          sleep(30);
          doCheck();
     }
}

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 (permalink)  
Old 11-18-2007
Registered User
 

Join Date: Jul 2006
Posts: 31
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.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Want to implement VLOOKUP (Excel function) in Unix pravani1 Shell Programming and Scripting 8 05-26-2008 05:08 AM
Polling/Interrogate Directory Questions dnidiffer Shell Programming and Scripting 1 06-14-2005 10:20 PM
timer k_oops9 Shell Programming and Scripting 3 11-08-2004 11:33 AM
how to implement timer Frank2004 AIX 2 06-09-2004 10:21 PM
Help - Polling Script brianmu Programming 1 09-06-2001 11:15 AM



All times are GMT -4. The time now is 06:53 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0