Re: select & gettimeofday

From: Moritz Franosch (mail_at_Franosch.org)
Date: 02/17/04


Date: 17 Feb 2004 19:33:54 +0100


Floyd Davidson <floyd@barrow.com> writes:

> Moritz Franosch <mail@Franosch.org> wrote:

> >I've set tm.tv_usec=3000 (3 ms) in the example run below.
> >
> >jfranosc@koma:~/tmp/test> cat /proc/version
> >Linux version 2.6.1 (jfranosc@cell2) (gcc version 3.2.2) #1 SMP Tue Feb 3 20:04:16 CET 2004
> >jfranosc@koma:~/tmp/test> g++ ./select.cpp
> >jfranosc@koma:~/tmp/test> ./a.out
> >2.97
> >10.238
> >9.799
> >10.727
> >10.578
> >9.835
> >9.966

> I'm not at all certain what you are doing.

There was a bug in my program. Here is the corrected version:

#include <sys/types.h>
#include <sys/time.h>

#include <iostream>

using namespace std;

int main() {
  timeval tm, t1, t2;
  int delay=3000;
  tm.tv_sec=0;
  tm.tv_usec=delay;
  int numselect=100;
  while(numselect--) {
    gettimeofday(&t1,NULL);
    int retval=select(1,NULL,NULL,NULL,&tm);
    //retval=usleep(10000);
    //retval = nanosleep(&tsp,NULL);
    if (retval!=0) perror("select");
    gettimeofday(&t2,NULL);
    cout << (t2.tv_usec-t1.tv_usec)/1000.0 << endl;
    tm.tv_sec=0;
    tm.tv_usec=delay;
  }
}

Indeed, kernel 2.6 has a granularity of 1 ms. For delay=500 (0.5 ms), I get:

jfranosc@koma:~/tmp/test> ./a.out
1.16
1.189
0.836
1.066
0.69
0.887
0.888
0.892
0.931

For delay=0, select returns immediately:

jfranosc@koma:~/tmp/test> ./a.out
0.012
0.004
0.003
0.003
0.003
0.003
0.003

Moritz

-- 
Dipl.-Phys. Moritz Franosch
http://Franosch.org


Relevant Pages