title: What's wrong with named pipes used between 2 processes (no. 2)?
body: <p>So, let's start from beginning: 2 processes 1 pipe to communicate, right? No! because communication is blocking, one waits for another. We need results from the second process through a different channel. Though it seems redundant it is not.</p>
  
  <p>Let's change it a bit: 2 processes 2 pipes, you can call one process the server and the other the client. One pipe sends jobs to clients and the other is used to collect results from the clients to the server.</p>
  
  <p>For convenience we call each pipe with the process name that is used for reading so, london reads london pipe and so on. That's the diagram of pipes and processes:</p>
  
  <pre><code>london ----writes madrid pipe--------&gt;
  london &lt;----reads london pipe------  |
                                    ^  |
                                    |  |
  madrid ----writes london pipe-----&gt;  v
  madrid &lt;----reads madrid pipe---------
  </code></pre>
  
  <p>Let's use 'london' as server and 'madrid' as client: server is responsible to end the endless loop.</p>
  
  <p>And this is the solution:</p>
  
  <pre><code>#!/bin/bash
  shopt -u failglob
  shopt -s extglob nullglob dotglob
  
  DIR=$( cd "$( dirname "$0" )" &amp;&amp; pwd )
  
  function london (){
     local i message answer london madrid
     london=london_$RANDOM.$RANDOM.$RANDOM.$$
     madrid=madrid_$RANDOM.$RANDOM.$RANDOM.$$
     cd $DIR
     mkfifo $london
     mkfifo $madrid
     ( madrid $madrid $london ) &amp;
     echo "parent id: $$, child id: $!"
     i=0
     #a mesterious situation: sometimes '3&lt; $london' just breaks it (?!)
     exec 3&lt;&gt; $london
     exec 4&gt; $madrid
  
     while true; do
  
        message="Greetings from London!($i)"
        echo "$message" &gt;&amp;4
  
        read -r answer &lt;&amp;3
        echo 'London says:&gt; '"$answer" #&gt;&amp; /dev/stdout
        (( i++ ))
  
        if [[ i -gt 1 ]]; then
           echo 'quit' &gt;&amp;4
           break
        fi
     done
  
     wait
     cd "$DIR"
     rm -rf $london
     rm -rf $madrid
  }
  
  function madrid (){
     local i message answer madrid london
     madrid=$1
     london=$2
     cd $DIR
     i=0
     exec 3&gt; $london
     exec 4&lt; $madrid
     while true; do
  
        read -r answer &lt;&amp;4
        echo 'Madrid says:&gt; '"$answer" #&gt;&amp; /dev/stdout
  
        message="Greetings from Madrid!($i)"
        echo "$message" &gt;&amp;3
        (( i++ ))
  
        if [[ $answer = 'quit' ]]; then
           break
        fi
     done
  }
  
  london
  </code></pre>
  
  <p>At function 'london' there is a comment just before</p>
  
  <pre><code>   exec 3&lt;&gt; $london
  </code></pre>
  
  <p>if you change this to</p>
  
  <pre><code>   exec 3&lt; $london
  </code></pre>
  
  <p>as it should(!) be then, I faced a situation where my program stalled repeatedly!
  After a few changes from &lt;> to &lt; and vice versa the problem eliminated and I can't reproduce it! I'm using an Ubuntu system so if anybody can test the program with his/her system and post some comments it will welcomed.</p>