LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-08-2011, 11:05 AM   #1
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 285

Rep: Reputation: 32
Calling a function in 1 sec without using any timer based function/API


Hi,
I need to call a function once in every 1 second, but the restriction is that I am not allowed to call any timer based call.

Is this possible using any C functions/login ?


Thanks,
Barun
 
Old 09-08-2011, 12:48 PM   #2
mulyadi.santosa
Member
 
Registered: Sep 2011
Posts: 96

Rep: Reputation: 15
Hi

how about using gettimeofday? so the idea is you do looping, call gettimeofday, so how far it advances....when it is bigger or equal than 1 secs..then shoot.

The drawback here is: since you do tight polling, your CPU utilization might be high. To reduce that, maybe you could call sched_yield() in between gettimeofday() calls.
 
Old 09-09-2011, 01:27 AM   #3
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 285

Original Poster
Rep: Reputation: 32
gettimeofday() is also a timer function.
Can we have some other mechanism ?
 
Old 09-09-2011, 03:15 PM   #4
mulyadi.santosa
Member
 
Registered: Sep 2011
Posts: 96

Rep: Reputation: 15
hi

maybe you can mimic what linux kernel does when calculating BogoMIPS.

So the idea is, you predict how much iterations it takes during one second. It could be derived using the following shell command:
$grep mips /proc/cpuinfo
bogomips : 3657.19
bogomips : 3657.65

So, roughly it means that in one second, you could do about 3 thousands and 6 hundreds empty loop. Then you could easily create such loop using any syntax you like. BTW, mind you, this is just an approximation, so no guarantee on precision.

PS: watch out for frequency scaling too. It might ruin this BogoMIPS....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
calling a function every 1 second soltanloo Programming 12 07-25-2009 12:15 PM
calling a function from within another function in php jayakrishnan Programming 2 06-19-2007 09:36 AM
calling a function by name in linux Four Programming 0 02-25-2007 12:19 AM
Calling another function from a function using GTK geminigal Programming 4 07-11-2005 04:15 PM
using timer for repeated function calls poojamanu Programming 1 04-09-2003 09:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration