title: Bash Pipe Handling
body: <p>Does anyone know how bash handles sending data through pipes?</p>
  
  <pre><code>cat file.txt | tail -20
  </code></pre>
  
  <p>Does this command print all the contents of file.txt into a buffer, which is then read by tail? Or does this command, say, print the contents of file.txt line by line, and then pause at each line for tail to process, and then ask for more data? </p>
  
  <p>The reason I ask is that I'm writing a program on an embedded device that basically performs a sequence of operations on some chunk of data, where the output of one operation is send off as the input of the next operation. I would like to know how linux (bash) handles this so please give me a general answer, not specifically what happens when I run "cat file.txt | tail -20".</p>
  
  <p>EDIT: Shog9 pointed out a relevant Wikipedia Article, this didn't lead me directly to the article but it helped me find this: <a href="http://en.wikipedia.org/wiki/Pipeline_%28Unix%29#Implementation" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Pipeline_%28Unix%29#Implementation</a> which did have the information I was looking for.</p>
  
  <hr>
  
  <p>I'm sorry for not making myself clear. Of course you're using a pipe and of course you're using stdin and stdout of the respective parts of the command. I had assumed that was too obvious to state.</p>
  
  <p>What I'm asking is how this is handled/implemented. Since both programs cannot run at once, how is data sent from stdin to stdout? What happens if the first program generates data significantly faster than the second program? Does the system just run the first command until either it's terminated or it's stdout buffer is full, and then move on to the next program, and so on in a loop until no more data is left to be processed or is there a more complicated mechanism?</p>