468,497 Members | 2,016 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,497 developers. It's quick & easy.

nanosleep mystery

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;
}
Dec 13 '07 #1
3 3161
In article <0e**********************************@s12g2000prg. googlegroups.com>,
chandanlinster <ch************@gmail.comwrote:
>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??
Off topic, blah, blah, blah.

We all know the drill by now.

Dec 13 '07 #2
chandanlinster wrote:
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??
Ask on a newsgroup related to those operating systems. This is not
something that is actually related to the C language.

As nanosleep is a POSIX function, comp.unix.programmer may be a suitable
place to raise the question. Alternatively, as this relates to Linux
systems in particular, one of the Linux newsgroups may be better.
Dec 13 '07 #3
chandanlinster 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??
.... snip ...
>
printf("diff = %ld\n", diff);
} while (1);

return 0;
}
diff is a clock_t. What does printf desire for a %ld descriptor|?

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Dec 14 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Red | last post: by
12 posts views Thread by Raymond Hettinger | last post: by
reply views Thread by Morris Carré | last post: by
reply views Thread by William Wisnieski | last post: by
115 posts views Thread by Mark Shelor | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.