#! /bin/sh # # good lock using mkdir and directory, atomic proc() { echo "Process $1 begin" mkdir lockdir 2>/dev/null if [ "$?" != 0 ]; then # mkdir failed, lock already exists echo "Process $1 can't lock" else # lock did not exist, now it does and we have it echo "Process $1 got lock" fi echo "Process $1 end" } rmdir lockdir 2>/dev/null proc 1 & proc 2 & proc 3 & wait rmdir lockdir 2>/dev/null