Unix Programming - nanosleep mystery

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > December 2007 > nanosleep mystery





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author nanosleep mystery
chandanlinster

2007-12-13, 1:24 pm

Hi,
I executed the following code on slackware 10.2 (with 2.6.22 kernel)
and ubuntu 7.10 (again with 2.6.22 kernel).
On slackware, the value of "diff" is sometimes 0(zero). On ubuntu it
is consistently above 1(one). Can anybody tell me why this may be
happening??

/***************** Code listing ****************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/times.h>

int main(int argc, char *argv[])
{
clock_t t1, t2, diff;
struct timespec req_time, rem_time;

req_time.tv_sec = 0;
req_time.tv_nsec = atol(argv[1]);

do {
if ((t1 = times(NULL)) == (clock_t)(-1)) {
perror("times");
exit(1);
}

if (nanosleep(&req_time, &rem_time) == -1) {
perror("nanosleep");
exit(1);
}

if ((t2 = times(NULL)) == (clock_t)(-1)) {
perror("times");
exit(1);
}

diff = t2 - t1;

printf("diff = %ld\n", diff);
} while (1);

return 0;
}
David Schwartz

2007-12-13, 1:24 pm

On Dec 13, 10:36 am, chandanlinster <chandanlins...@gmail.com> wrote:

> I executed the following code on slackware 10.2 (with 2.6.22 kernel)
> and ubuntu 7.10 (again with 2.6.22 kernel).
> On slackware, the value of "diff" is sometimes 0(zero). On ubuntu it
> is consistently above 1(one). Can anybody tell me why this may be
> happening??


There could be any number of reasons, all due to odd implementation
details. Both results are within the realm of reason. My best guess
would be that one platform has HZ defined above the resolution of the
minimum sleep interval and one has HZ defined below it.

DS
chandanlinster

2007-12-14, 1:35 am

On Dec 13, 11:49 pm, David Schwartz <dav...@webmaster.com> wrote:
> On Dec 13, 10:36 am, chandanlinster <chandanlins...@gmail.com> wrote:
>
>
> There could be any number of reasons, all due to odd implementation
> details. Both results are within the realm of reason. My best guess
> would be that one platform has HZ defined above the resolution of the
> minimum sleep interval and one has HZ defined below it.
>
> DS


Both these distributions have HZ value set to 100. (I got the value
using sysconf()).
Rainer Weikusat

2007-12-14, 1:35 am

chandanlinster <chandanlinster@gmail.com> writes:
> Hi,
> I executed the following code on slackware 10.2 (with 2.6.22 kernel)
> and ubuntu 7.10 (again with 2.6.22 kernel).
> On slackware, the value of "diff" is sometimes 0(zero). On ubuntu it
> is consistently above 1(one). Can anybody tell me why this may be
> happening??


[ code snipped ]

No, because you have failed to mention what you pass as argv[1].


Måns Rullgård

2007-12-14, 1:35 am

chandanlinster <chandanlinster@gmail.com> writes:

> On Dec 13, 11:49 pm, David Schwartz <dav...@webmaster.com> wrote:
>
> Both these distributions have HZ value set to 100. (I got the value
> using sysconf()).


That means nothing. The actual timer frequency is what matters.
Check /proc/config.gz (if present) for CONFIG_HZ instead.

--
Måns Rullgård
mans@mansr.com
David Schwartz

2007-12-14, 1:35 am

On Dec 13, 11:46 am, chandanlinster <chandanlins...@gmail.com> wrote:

> Both these distributions have HZ value set to 100. (I got the value
> using sysconf()).


First, that's the user-visible HZ value which may or may not match the
kernel's internal HZ value. Second, they might still have different
minimum nanosleep resolution. In any event, it really doesn't matter.
Both behaviors are completely reasonable.

DS
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com