Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
19 years ago

usleep and select timeout

I wrote some test programs to test out the usleep and select timeout functions. The NIOS II processor seems to have a minimum resolution of 10 ms. usleep also has a tendency to double the sleep time for a given 10 ms interval. For instance,

1ms = 10 ms sleep time

10ms = 20ms sleep time

... and so on...

Does anyone have any experience with this?

2 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    A piece of sample code that does not work... the code hangs when going into the loop as best as I can tell. If I take the delay wether it be sleep, usleep, or select via timeout it hangs.

    Why is this happening? I have no clue. It hangs the same way on regular linux systems.... never had this problem before. Maybe I am just passing over something simple.

    Code as follows:

    // ********************************************************************************

    // **

    // ** select timeout timing test

    // **

    // ********************************************************************************

    // OS Provided Includes# include <sys/stat.h># include <sys/signal.h># include <sys/select.h># include <sys/types.h># include <time.h># include <fcntl.h># include <stdio.h># include <string.h># include <math.h>

    int main(void)

    {

    int counter = 0;

    struct timeval timeout;

    printf("..................\n");

    printf("serial receive test\n");

    // The main loop

    // ... essentially just sleep and print every 1 second

    for (counter = 0; counter < 100; counter++)

    {

    // Print out a .

    printf(".");

    /*

    // Sleep for 1 second

    timeout.tv_sec = 0;

    timeout.tv_usec = 1000000;

    select(0, NULL, NULL, NULL, &timeout);

    */

    usleep(1000000);

    //sleep(1);

    //printf(".");

    }

    printf("\n");

    return TRUE;

    }
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I was up till midnight trying to find a solution... at least to the delay hanging up a loop.

    Here is what I found:

    while(1)

    {

    prinft(".");

    usleep(100000);

    }

    The above code seems to hang on NIOS II processor and Intel based processor linux. However after having some patience it bursts output after about a minute of waiting.

    So... what is the difference between this and the delay tests I made in the original post....?

    Here is a reduced version of the delay test...

    while(1)

    {

    start timer;

    printf("something\n");

    sleep;

    stop timer;

    printf("timing difference\n");

    }

    So... the big difference is the \n (carriage return line feed). LINUX seems to buffer the console output until it runs out of buffer or it finds a carriage return line feed when used IN CONJUNCTION WITH USLEEP or any type of blocking delay.

    Why does it do this and how do I tell LINUX to stop doing that.

    Thanks....

    Also if anyone knows how to increase the resolution of the usleep or why it sleeps for 10 more milliseconds that you tell it that would be great too...