LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 10: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: 303

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, 11:48 AM   #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, 12: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: 303

Original Poster
Rep: Reputation: 32
gettimeofday() is also a timer function.
Can we have some other mechanism ?
 
Old 09-09-2011, 02: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



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 11:15 AM
calling a function from within another function in php jayakrishnan Programming 2 06-19-2007 08:36 AM
calling a function by name in linux Four Programming 0 02-24-2007 11:19 PM
Calling another function from a function using GTK geminigal Programming 4 07-11-2005 03:15 PM
using timer for repeated function calls poojamanu Programming 1 04-09-2003 08:17 PM

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

All times are GMT -5. The time now is 09:28 PM.

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
Open Source Consulting | Domain Registration