SHARED_MEM=turno
echo pippo > $SHARED_MEM

processo(){ # name other
 for i in $(seq 100); do
     enter_region $1 $2
     critical_session $1
     leave_region $1
     io
 done
}
critical_session(){ 
  for i in 1 2 3; do
      echo -n $1
      io
  done
  echo .
}

io(){ 
    dd if=/dev/zero of=/dev/null count=1000 2>/dev/null 
}

enter_region() { # process other
 echo "true" > interested_$1
 echo $1 > $SHARED_MEM
 while grep $1 $SHARED_MEM >/dev/null && 
       grep true interested_$2 >/dev/null ; 
 do 
     true; 
 done
}
leave_region() { # process
 echo "false" > interested_$1
}

processo pippo pluto & processo pluto pippo &
wait
