Locks, FIFOs and race conditions
A race condition appears when a result depends on the timing of concurrent operations. Two workers reading a counter and later writing it can both overwrite the same old value. Put the complete read-modify-write operation under a shared coordination protocol. On Linux, util-linux flock supports advisory locks on open file descriptors or a command wrapper; every cooperating writer must use the same lock, and filesystem behavior varies. Keep the lock file stable; unlinking it while held can let another process lock a different inode. flock -n fails immediately on contention; -w bounds the wait. mkdir can also act as an atomic acquisition attempt in a trusted directory, but stale ownership and recovery must be designed. A FIFO is a named pipe, not persistent message storage: opening one end may block until the other opens. Use bounded producer and consumer lifetimes, close descriptors, and remove only owned paths after both finish. GNU timeout can bound the entire FIFO command including its open. These local examples are not distributed-lock or production IPC guarantees. The lock examples assume a fresh private directory and cooperating writers. Use an explicit flock conflict status to distinguish contention from other errors; check opening the descriptor and removing the owned FIFO separately.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code.
Reject a competing lock
exec 9> shared.lock || exit 1flock -x -w 1 9 || exit 1if flock -n -E 75 shared.lock true; then printf 'unexpected\n' exit 1else rc=$? [[ $rc == 75 ]] || exit 1 printf 'busy\n'fiexec 9>&- || exit 1flock -n shared.lock printf 'available\n' Expected stdout:
busyavailableThe second independent open conflicts while the original descriptor holds the lock. Closing the original releases it when no other holder remains.
Protect a shared counter
printf '0\n' > count.txt || exit 1increment() ( exec 9> shared.lock || exit 1 flock -x -w 1 9 || exit 1 IFS= read -r n < count.txt || exit 1 printf '%s\n' "$((n + 1))" > count.txt)increment &a=$!increment &b=$!wait "$a"; ra=$?wait "$b"; rb=$?(( ra == 0 && rb == 0 )) || exit 1cat count.txtExpected stdout:
2Each worker opens the same lock independently and protects its full update. The parent checks both statuses.
Send one record through a FIFO
mkfifo channel || exit 1timeout 2s bash -c 'printf "message\n" > channel' &writer=$!timeout 2s cat channelreader_rc=$?wait "$writer"; writer_rc=$?rm -- channel || exit 1(( reader_rc == 0 && writer_rc == 0 )) || exit 1Expected stdout:
messageBoth opens are inside timed commands. The FIFO is removed only after the two ends finish.
Common mistakes
Do not test whether a lock file exists and then create it as two independent operations. Do not remove a held lock file. A read timeout alone does not bound an earlier FIFO open.
Practice
Use the lock-protected counter example with two workers each incrementing twice. Verify the total is four, then explain the lost-update schedule that is possible without locking. Use a fresh private directory and finite lock waits.
Next: Startup, invocation and shell configuration