# Select doesn't work correctly on EC2



## yamaneko (Nov 18, 2012)

Hi

I found select's timeout doesn't work correctly on EC2. This problem is reproduced by following way.

1. Create an EC2 instance using 118940168514/FreeBSD/EC2 8.3-RELEASE amd64/HVM(AMI-6c1dac6d).
2. Run following commands on the instance.
`$ vi testselect.c`

```
#include <stdio.h>
#include <sys/select.h>
int main(){
        struct timeval t;
        t.tv_sec = 5;
        t.tv_usec = 0;
        select(0, NULL, NULL, NULL, &t);
        return 0;
}
```


```
$ gcc testselect.c
$ time ./a.out
        8.61 real         0.00 user         0.00 sys
```
How can I fix this problem?

Thanks


----------



## sossego (Nov 18, 2012)

Shouldn't this post be in development?


----------



## yamaneko (Nov 19, 2012)

sossego said:
			
		

> Shouldn't this post be in development?


O.K. Thanks for your advice!


----------



## SirDice (Nov 19, 2012)

Don't double post yamaneko, if a post is in the wrong place a mod will move it.


----------



## yamaneko (Nov 19, 2012)

SirDice said:
			
		

> Don't double post yamaneko, if a post is in the wrong place a mod will move it.



Sorry, I'll be careful from now on.


----------



## SirDice (Nov 19, 2012)

From select(2):

```
If timeout is not a null pointer, it specifies the maximum interval to
     wait for the selection to complete.  [b]System activity can lengthen the
     interval by an indeterminate amount.[/b]
```


----------



## yamaneko (Nov 19, 2012)

OK. I should not write a program that expects select to finish within `timeout`.


----------



## expl (Nov 23, 2012)

If you want to get more precision and efficiency you should use poll() or even better (but not so portable) kqueue(). Select syscall has been around almost as long as UNIX itself and is not the most advanced way to handle I/O.


----------

