LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Get cpu ticks c function (https://www.linuxquestions.org/questions/programming-9/get-cpu-ticks-c-function-543808/)

entz 04-06-2007 08:59 AM

Get cpu ticks c function
 
Hello,

I'm looking right now at the man pages for a function/s to get the amount of cpu ticks (i.e milliseconds or whatever) that have elapsed since the machine has started , you know !

also I'm looking for a function that halts process execution for a certain time (as definde in Ms ) , so far I've found sleep() for that matter but it only takes integer as an argument denoting seconds , but I want to be able to halt the execution in the millisecond range (at least that if not even in microseconds , the smaller the time unit the better)

so your knowledge is required again.

P.s as I said I'm searching how to deal with those two issues but you probably could spare me the time in case you know the answers already

and thanks

wjevans_7d1@yahoo.co 04-06-2007 12:04 PM

To sleep for periods less than a second, use select(), using no file descriptors. See:

Code:

man 2 select
This is portable among Unix/Linux variants. Like any other way to sleep, there is no guarantee that you won't sleep beyond the specified time. There are various reasons you might sleep less than the specified time; use gettimeofday() to verify that you've slept long enough. See:

Code:

man 2 gettimeofday
Hope this helps.

dmail 04-06-2007 01:38 PM

I'm currently working on a cross platform app and was also looking for a milliseconds sleep, in the end I used the following macro which uses usleep which takes microseconds.
Code:

ms_sleep(x) usleep(x*1000)
As for the cpus ticks I don't know if there is such as standard call?

entz 04-06-2007 01:53 PM

Hmmm , thanks for the feedback folks

As wjevans_7d1@yahoo.co has mentioned the issue of NOT having a guarantee that the sleep function does not wait longer than it's supposed is basically the thing that disturbs me the most.
Not that I'm a fan of Microsoft or something (those of you who've read some of my posts can assure that I loath it ) but in the regard of programming that involves timing , windows is Unfortunately:rolleyes: much easier .
in windows you have sleep() which takes a Millisecond integer and and a function called Getcputicks() (if my memory serves me right) which returns the number of ms's that elapsed since system boot.

I wish this issue would be as simple in *nix as it's in windoze :(

dmail 04-06-2007 02:04 PM

I don't know of a Getcputicks function but there is QueryPerformanceCounter and contrary to what you say about ms it jumps about and is recommended that you double check it with a call to a low resolution timer like timeGetTime and on that matter of windows version of sleep here is some news for you:

http://msdn2.microsoft.com/en-us/library/ms686298.aspx
Quote:

This function causes a thread to relinquish the remainder of its time slice and become unrunnable for an interval based on the value of dwMilliseconds. The system clock "ticks" at a constant rate. If dwMilliseconds is less than the resolution of the system clock, the thread may sleep for less than the specified length of time. If dwMilliseconds is greater than one tick but less than two, the wait can be anywhere between one and two ticks, and so on. To increase the accuracy of the sleep interval, call the timeGetDevCaps function to determine the supported minimum timer resolution and the timeBeginPeriod function to set the timer resolution to its minimum. Use caution when calling timeBeginPeriod, as frequent calls can significantly affect the system clock, system power usage, and the scheduler. If you call timeBeginPeriod, call it one time early in the application and be sure to call the timeEndPeriod function at the very end of the application.

After the sleep interval has passed, the thread is ready to run. If you specify 0 milliseconds, the thread will relinquish the remainder of its time slice but remain ready. Note that a ready thread is not guaranteed to run immediately. Consequently, the thread may not run until some time after the sleep interval elapses. For more information, see Scheduling Priorities.

TeodosiuS 04-07-2007 05:33 AM

ahoy
 
In win32 the sleep() gets the param in milliseconds but here ...
entz, at what exactly are you working on?

entz 04-07-2007 09:53 AM

Quote:

Originally Posted by TeodosiuS
In win32 the sleep() gets the param in milliseconds but here ...
entz, at what exactly are you working on?

WEll , I've been (shall i say) inspecting the possibility of incorporating those get-time and sleep functions to manage a program I'm writting by kinda like "leashing" the execution - if you know what I mean - so that it doesn't swallow the whole CPU , etc

but after seeing how unreliable this technique is , despite that it's easy from a theoretical perspective , I've dropped it and went to other methods...

cheers

TeodosiuS 04-12-2007 04:54 AM

aha
 
I understand :)
great :)
would you show the open src ;)

slzckboy 04-17-2007 11:22 AM

Do
man times

Code:


......

RETURN VALUE
      The  function  times returns the number of clock ticks that have elapsed since
      an arbitrary point in the past. For Linux this point is the moment the  system
      was  booted.  This  return  value  may  overflow  the  possible range of type
      clock_t.  On error, (clock_t) -1 is returned, and errno is set  appropriately.



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