Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of select() system call in Bash

Tags:

In my script I am trying to read data from several serial ports to coordinate several devices.

I can open file descriptors to the serial ports using exec 9</dev/ttyACM0 && exec 8</dev/ttyACM1 etc.

I was hopping I could then do something like select() on these file descriptors to wait on data arriving, then process it with read 0<&9 or read 0<&8 depending on which one had received data. Unfortunately I cannot find an equivalent of select() for Bash.

The closest I can find is using read -t 0 0<&9 to poll for data to be read. I don't like this as it requires a sleep to prevent the script from consuming 100% the processor. Although sub second sleeps are possible e.g. while true; do sleep 0.01; done this consumes ~4% of the processor on my system. I can knock it back to 0.1 seconds and reduce it to <1% but it still does not "feel" the right way to do it as it adds at least 100ms of additional delay to any coordination between the devices.

Are there any solutions to this? Is there a Bash select() equivalent?

like image 824
SimonAlfie Avatar asked May 05 '16 11:05

SimonAlfie


1 Answers

Quoting Greg's Bash Wiki http://mywiki.wooledge.org/ProcessManagement

There is no shell scripting equivalent to the select(2) or poll(2) system calls. If you need to manage a complex suite of child processes and events, don't try to do it in a shell script. (That said, there are a few tricks in the advanced section http://mywiki.wooledge.org/ProcessManagement#advanced of this page.)

like image 99
Borut Hadžialić Avatar answered Sep 28 '22 03:09

Borut Hadžialić