nanosleep mystery

Discussion in 'C Programming' started by chandanlinster, Dec 13, 2007.

  1. 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;
    }
    chandanlinster, Dec 13, 2007
    #1
    1. Advertising

  2. In article <>,
    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??


    Off topic, blah, blah, blah.

    We all know the drill by now.
    Kenny McCormack, Dec 13, 2007
    #2
    1. Advertising

  3. chandanlinster

    Mark Bluemel Guest

    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.
    Mark Bluemel, Dec 13, 2007
    #3
  4. chandanlinster

    CBFalconer Guest

    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
    CBFalconer, Dec 13, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. valentin tihomirov

    another array ranges mystery

    valentin tihomirov, Jun 18, 2005, in forum: VHDL
    Replies:
    2
    Views:
    473
    Mike Treseler
    Jun 18, 2005
  2. John K. Humkey

    sed regexp mystery

    John K. Humkey, Jul 8, 2003, in forum: Perl
    Replies:
    0
    Views:
    2,034
    John K. Humkey
    Jul 8, 2003
  3. bjh

    Mystery Repost

    bjh, Jan 13, 2004, in forum: Perl
    Replies:
    0
    Views:
    710
  4. Todd Plambeck

    Mystery Error

    Todd Plambeck, Jul 15, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    498
    Victor Garcia Aprea [MVP]
    Jul 15, 2003
  5. yezi

    nanosleep and nanosecond

    yezi, Nov 3, 2005, in forum: C Programming
    Replies:
    4
    Views:
    456
    Michel Bardiaux
    Nov 7, 2005
Loading...

Share This Page