Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] using rusage



2008/6/19 Curt Sampson <cjs@example.com>:

> On 2008-06-19 17:55 +0900 (Thu), Hung Nguyen Vu wrote:
>
>> I expected that after a sleep(1), stopTimer() will return 1 seconds
>
> Well, that's a misunderstanding on your part.
>
> You asked the OS not to schedule your process for at least one second.
> Clearly, you won't be using any CPU during that wait; it's what you
> specifically asked for.

A couple of comments:

1. The standard way to do *wall clock* timing is to grab a timestamp,
go to sleep, grab a new timestamp when you wake up, and subtract the
old one from the new one. If you want user or system time, as Curt
noted, you won't get any while sleeping.

2. Use nanosleep(2) instead of sleep(2). nanosleep(2) correctly
handles signals, and will get you closer to 1.0 seconds than sleep(2)
will. Just beware of this if you are doing really hi-res timing:

BUGS
       The  current implementation of nanosleep() is based on the normal kernel
       timer mechanism, which has a resolution of 1/HZ s (see time(7)).  There-
       fore, nanosleep() pauses always for at least the specified time, however
       it can take up to 10 ms longer than specified until the process  becomes
       runnable  again.  For  the  same reason, the value returned in case of a
       delivered signal in *rem is usually rounded to the next larger  multiple
       of 1/HZ s.

-- 
Cheers,
Josh


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links