f



sleep() API behaves differently in Linux 2.4 and Linux 2.6

Hi,
I have a simple program utilizing "sleep()" api to sleep 3 msec
periodically.
I measured the time difference before calling the api and after it
returns back.
The same application behaves differently running on Linux 2.4 and 2.6
In Linux 2.6.11, it takes about 3 msec, and running on Linux 2.4.20 it
takes about 10 msec.
I have attached the program below, many thanks in advance.

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>

#define TO_64(a) (a##LLU)

#define kUsecTimeScale TO_64(1000000)
typedef unsigned long long UInt64;
typedef unsigned long long TSTimestamp;
/*UInt64 TSTimestamp;*/
TSTimestamp GetUsecTimestampFromTimeval(struct timeval* pTimeval) {
    return ((((UInt64)pTimeval->tv_sec) * (UInt64)kUsecTimeScale) +
((UInt64)pTimeval->tv_usec)); }

TSTimestamp GetUsecTimestamp(void)
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return GetUsecTimestampFromTimeval(&tv); }

void test()
{
struct timeval tvsel;
tvsel.tv_sec = 0;
tvsel.tv_usec = 3000;
static int counter = 0;
TSTimestamp t1, t2, diff;

t1 = GetUsecTimestamp();
select(0,NULL, NULL, NULL, &tvsel);
t2 = GetUsecTimestamp();

diff  =  t2 - t1;

printf("time slept: %llu %llu %llu\n", t1, t2, diff); }

main()
{
int s = 1000;
while (s--)
        test();
}

Thanks

0
bizhan
2/28/2007 11:02:00 AM
comp.os.linux.development.apps 5216 articles. 1 followers. Post Follow

3 Replies
519 Views

Similar Articles

[PageSpeed] 41

bizhan.gholikhamseh@gmail.com <bizhan.gholikhamseh@gmail.com> wrote:
> I have a simple program utilizing "sleep()" api to sleep 3 msec
> periodically.
> I measured the time difference before calling the api and after it
> returns back.
> The same application behaves differently running on Linux 2.4 and 2.6
> In Linux 2.6.11, it takes about 3 msec, and running on Linux 2.4.20 it
> takes about 10 msec.

Under 2.4 kernels the timer interrupt frequency of the system
typically was 100 Hz (10 ms), at least for PCs, while under
2.6 kernels it's a compile time setting (you can select be-
tween 100 Hz, 250 Hz and 1000 Hz when configuring the kernel
but one can also set other values). Probably it's set to
250 Hz or 1 kHz on your 2.6 kernel, thus sleep() is returning
after a shorter time than with the 2.4 kernel.

                               Regards, Jens
--
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de
0
jt
2/28/2007 11:52:30 AM
On Feb 28, 3:02 am, "bizhan.gholikham...@gmail.com"
<bizhan.gholikham...@gmail.com> wrote:
> Hi,
> I have a simple program utilizing "sleep()" api to sleep 3 msec
> periodically.
> I measured the time difference before calling the api and after it
> returns back.
> The same application behaves differently running on Linux 2.4 and 2.6
> In Linux 2.6.11, it takes about 3 msec, and running on Linux 2.4.20 it
> takes about 10 msec.
> I have attached the program below, many thanks in advance.

This seems like normal and expected behavior. You can't really expect
a general purpose operating system to be able to use the CPU in 3
millisecond chunks. That's just too small for a general purpose OS.

DS

0
David
2/28/2007 8:31:52 PM
On 2007-02-28, bizhan.gholikhamseh@gmail.com <bizhan.gholikhamseh@gmail.com> wrote:

> I have a simple program utilizing "sleep()" api to sleep 3
> msec periodically.

No, you don't.  You have a program that uses select() api to
sleep at least 3ms periodically.

> I measured the time difference before calling the api and
> after it returns back.

> The same application behaves differently running on Linux 2.4
> and 2.6 In Linux 2.6.11, it takes about 3 msec, and running on
> Linux 2.4.20 it takes about 10 msec.

The default system timer tick on 2.4 kernels was 10ms. With a
10ms tick, 10ms is the minimum amount of time you can sleep.

On 2.6, most systems use a 1ms or 4ms timer tick.  The system
on which you tested must have been configured for 1ms.  Some
2.6 systems might still be configured for 10ms.

Unfortunately, the man page for select() is incorrect.  timeout
is not an upper bound on the the amount of time elapsed before
select() returns (as is stated on the man page).

timeout is actually a lower bound on the maximimum time slept
in the case where the selected events do not occur and the
system call is not interrupted.  In that case the amount of
time slept will be a multiple of the system tick time that is
greater than or equal to the specified timeout.  On a busy
system it could be many ticks larger than the specified
timeout.

Expecting it to be precisely 3ms is futile.

-- 
Grant Edwards                   grante             Yow!  PUNK ROCK!! DISCO
                                  at               DUCK!! BIRTH CONTROL!!
                               visi.com            
0
Grant
2/28/2007 8:55:02 PM
Reply: