SHARED_MEM=lock
echo 0 > $SHARED_MEM

processo(){ # name
 for i in $(seq 1000); do
     while grep 1 $SHARED_MEM >/dev/null ; do true; done
     echo 1 > $SHARED_MEM
     critical_session $1
     echo 0 > $SHARED_MEM
     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
}

processo pippo & processo pluto &
wait