On Tue, Feb 29, 2000 at 06:34:50PM +0100, Jens Lauer wrote:
> Hi folks,
> 
[...]
> What could be the reason for the misbehaviour of sleep, usleep and
> select in a function?
> 

This is slightly off your topic, but the following source code shows
some of the issues involved in using usleep() and select() functions
in Linux.  Run the program and graph the results.  Oviously, since
the Linux timer granularity is (typ) 10 ms, you see this effect show
up in the graph.


dave...



#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>

void test1(void);
void test2(void);

int main(int argc,char *argv[])
{
        test1();

        return 0;
}

void test1(void)
{
        struct timeval tv1;
        struct timeval tv2;
        int usec;
        int actual;

        for(usec=0;usec<10000;usec+=100){
                gettimeofday(&tv1,NULL);
                usleep(usec);
                gettimeofday(&tv2,NULL);

                actual=(tv2.tv_sec-tv1.tv_sec)*1000000+
                        (tv2.tv_usec-tv1.tv_usec);
                //actual=tv2.tv_sec*1000000+tv2.tv_usec;
                printf("%d %d\n",usec,actual);
        }
}

void test2(void)
{
        struct timeval now;
        struct timeval targ,t;
        int usec,sec;
        int actual;
        int i;

        usec=(1.0e6/230.0);
        sec=0;
        gettimeofday(&targ,NULL);
        for(i=0;i<1000;i++){
                targ.tv_usec+=usec;
                if(targ.tv_usec>=1000000){
                        targ.tv_usec-=1000000;
                        targ.tv_sec++;
                }
                t=targ;
                gettimeofday(&now,NULL);
                t.tv_sec-=now.tv_sec;
                if(t.tv_usec<now.tv_usec){
                        t.tv_usec+=1000000;
                        t.tv_sec--;
                }
                t.tv_usec-=now.tv_usec;
                select(0,NULL,NULL,NULL,&t);
                actual=(now.tv_sec-targ.tv_sec)*1000000+
                        (now.tv_usec-targ.tv_usec);
                printf("%d %d\n",i*usec,actual);
        }
}

-- [rtl] ---
To unsubscribe:
echo "unsubscribe rtl" | mail [EMAIL PROTECTED] OR
echo "unsubscribe rtl <Your_email>" | mail [EMAIL PROTECTED]
---
For more information on Real-Time Linux see:
http://www.rtlinux.org/~rtlinux/

Reply via email to