LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-06-2007, 08:59 AM   #1
entz
Member
 
Registered: Mar 2007
Location: Milky Way , Planet Earth!
Distribution: Opensuse
Posts: 453
Blog Entries: 3

Rep: Reputation: 40
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
 
Old 04-06-2007, 12:04 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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.
 
Old 04-06-2007, 01:38 PM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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?
 
Old 04-06-2007, 01:53 PM   #4
entz
Member
 
Registered: Mar 2007
Location: Milky Way , Planet Earth!
Distribution: Opensuse
Posts: 453

Original Poster
Blog Entries: 3

Rep: Reputation: 40
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 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

Last edited by entz; 04-06-2007 at 01:55 PM.
 
Old 04-06-2007, 02:04 PM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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.
 
Old 04-07-2007, 05:33 AM   #6
TeodosiuS
LQ Newbie
 
Registered: Apr 2007
Location: Sofia, Bulgaria
Distribution: Gentoo Linux & Slackware
Posts: 2

Rep: Reputation: 0
Wink ahoy

In win32 the sleep() gets the param in milliseconds but here ...
entz, at what exactly are you working on?
 
Old 04-07-2007, 09:53 AM   #7
entz
Member
 
Registered: Mar 2007
Location: Milky Way , Planet Earth!
Distribution: Opensuse
Posts: 453

Original Poster
Blog Entries: 3

Rep: Reputation: 40
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
 
Old 04-12-2007, 04:54 AM   #8
TeodosiuS
LQ Newbie
 
Registered: Apr 2007
Location: Sofia, Bulgaria
Distribution: Gentoo Linux & Slackware
Posts: 2

Rep: Reputation: 0
aha

I understand
great
would you show the open src
 
Old 04-17-2007, 11:22 AM   #9
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slackware 14.2 kernel 4.19.43
Posts: 462

Rep: Reputation: 30
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.
 
  


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
ticks pacco Linux - General 1 02-14-2006 12:31 PM
udev playing ticks on me again! edM Slackware 1 12-06-2005 04:38 PM
High CPU on gethostbyname() function aravindtj Programming 0 01-17-2005 11:17 PM
All those free SPU ticks marghorp Linux - General 2 11-09-2004 06:05 AM
Finer Clock Ticks gothrog Linux - Networking 2 06-25-2004 04:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:38 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
Open Source Consulting | Domain Registration