Problem with nanosleep - Unix

This is a discussion on Problem with nanosleep - Unix ; Hi there, I'm trying to create a square wave signal with a duty cycle of 1ms. My code (FOX BOARD) looks like this: #include #include #include #include #include #include #define _POSIX_C_SOURCE 199309 #include #include #include int main(int argc, char **argv) ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: Problem with nanosleep

  1. Problem with nanosleep

    Hi there,

    I'm trying to create a square wave signal with a duty cycle of 1ms. My
    code (FOX BOARD) looks like this:

    #include
    #include
    #include
    #include
    #include
    #include
    #define _POSIX_C_SOURCE 199309
    #include
    #include
    #include

    int main(int argc, char **argv) {
    struct timespec ts={
    .tv_sec=0,
    .tv_nsec=1000000
    };
    int return_value=0;

    // set PA0 as output
    gpiosetdir(PORTA, DIROUT, PA0);

    while(1) {
    gpiosetbits(PORTA, PA0);
    return_value=nanosleep(&ts, NULL);
    if(return_value)
    perror("nanosleep");

    gpioclearbits(PORTA, PA0);
    return_value=nanosleep(&ts, NULL);
    if(return_value)
    perror("nanosleep");
    }
    return(0);
    }

    But my oscillator is showing a frequency of 25 Hz with a duty cycle of
    20 ms...

    My kernel is: 2.6.15 #1 PREEMPT Thu Oct 30 10:41:51 CET 2008 cris
    unknown

    Best regards
    Johann

  2. Re: Problem with nanosleep

    2008-10-30, 04:09(-07), horvat.johann@gmx.net:
    [...]
    > struct timespec ts={
    > .tv_sec=0,
    > .tv_nsec=1000000
    > };
    > int return_value=0;

    [...]
    > return_value=nanosleep(&ts, NULL);

    [...]

    nanosleep will sleep for *at least* 1ms, that's all it can
    guarantee. On a multitask system, you can't even be sure you'll
    have the CPU.

    > My kernel is: 2.6.15 #1 PREEMPT Thu Oct 30 10:41:51 CET 2008 cris
    > unknown

    [...]

    Upgrading to 2.6.22 or newer will get you high precision timing.
    So that it will sleep for 1ms unless the scheduler schedules
    another process which uses the CPU for longer than that.

    --
    Stéphane

  3. Re: Problem with nanosleep

    On Oct 30, 4:09*am, horvat.joh...@gmx.net wrote:

    > I'm trying to create a square wave signal with a duty cycle of 1ms. My
    > code (FOX BOARD) looks like this:

    [snip]
    > But my oscillator is showing a frequency of 25 Hz with a duty cycle of
    > 20 ms...
    >
    > My kernel is: 2.6.15 #1 PREEMPT Thu Oct 30 10:41:51 CET 2008 cris
    > unknown


    The kernel won't schedule a process unless it can give it a full
    timeslice. The kernel cannot give you a full timeslice every
    millisecond. That would be unfair to every other process on the
    system. (Consider the cost of context switches, loss of data hot in
    the cache, and so on.)

    The system simply doesn't consider a normal program's desire for the
    CPU every millisecond to be important enough to justify the
    performance impact on the rest of the system. If it really is that
    important, you need to tell the system that. Expect a significant
    negative performance impact.

    DS

  4. Re: Problem with nanosleep

    In article
    ,
    horvat.johann@gmx.net wrote:

    > Hi there,
    >
    > I'm trying to create a square wave signal with a duty cycle of 1ms. My
    > code (FOX BOARD) looks like this:

    [snip]
    > But my oscillator is showing a frequency of 25 Hz with a duty cycle of
    > 20 ms...


    man nanosleep

    The function will sleep for AT LEAST the amount of time specified. It
    may sleep longer due to system latencies and the resolution of the
    realtime clock used by nanosleep.

    You can find out the realtime clock's resolution by calling
    clock_getres(). On Linux the resolution is determined by the kernel
    constant HZ.

  5. Re: Problem with nanosleep

    2008-10-30, 14:06(-05), Wayne C. Morris:
    [...]
    >> I'm trying to create a square wave signal with a duty cycle of 1ms. My
    >> code (FOX BOARD) looks like this:

    > [snip]
    >> But my oscillator is showing a frequency of 25 Hz with a duty cycle of
    >> 20 ms...

    >
    > man nanosleep
    >
    > The function will sleep for AT LEAST the amount of time specified. It
    > may sleep longer due to system latencies and the resolution of the
    > realtime clock used by nanosleep.
    >
    > You can find out the realtime clock's resolution by calling
    > clock_getres(). On Linux the resolution is determined by the kernel
    > constant HZ.


    The above is no longer true of Linux since 2.6.22 since which
    high resolution timers are implemented and used throughout
    including for nanosleep. Note that YMMV depending on the
    architecture.

    a nanosleep(1000000) will now more often than not sleep for
    almost exactly 1ms and it is not dependent on HZ nor the kernel
    HZ but on the precision of whatever hardware clock the kernel
    uses.

    --
    Stéphane

+ Reply to Thread