nanosleep mystery

C

chandanlinster

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;
}
 
K

Kenny McCormack

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.
 
M

Mark Bluemel

chandanlinster said:
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.
 
C

CBFalconer

chandanlinster said:
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|?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top