title: Using pipes in bash to buffer data from a serial device
body: <p>I have a script that looks something like:</p>
  
  <pre><code>while true; do
      read -t10 -d$'\n' input_from_serial_device &lt;/dev/ttyS0
      # do some costly processing on the string
  done
  </code></pre>
  
  <p>The problem is that it will miss the next input from the serial device because it is burning CPU cycles doing the costly string processing.</p>
  
  <p>I thought I could fix this by using a pipe, on the principle that bash will buffer the input between the two processes:</p>
  
  <pre><code>( while true; do
       read -d$'\n' input_from_serial_device &lt;/dev/ttyS0
       echo $input_from_serial_device
    done ) | ( while true; do
       read -t10 input_from_first_process
       # costly string processing
    done )
  </code></pre>
  
  <p>I firstly want to check that I've understood the pipes correctly and that this will indeed buffer the input between the two processes as I intended. Is this idea correct?</p>
  
  <p>Secondly, if I get the input I'm looking for in the second process, is there a way to immediately kill both processes, rather than exiting from the second and waiting for the next input before exiting the first?</p>
  
  <p>Finally, I realise bash isn't the best way to do this and I'm currently working on a C program, but I'd quite like to get this working as an intermediate solution.</p>
  
  <p>Thank you!</p>