Created
December 8, 2011 22:29
-
-
Save thinkerbot/1448968 to your computer and use it in GitHub Desktop.
IPC properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -o noclobber | |
lockfile=${1:-lockfile} | |
while read line | |
do | |
while ! echo "$$" > "$lockfile" | |
do true #printf "waiting for: $lockfile [$(cat $lockfile)]\n" 1>&2 | |
done 2>/dev/null | |
trap 'rm "$lockfile"; exit $?' INT TERM EXIT | |
printf "$line\n" | |
rm "$lockfile" | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
############################################################################# | |
# Demonstrates how to use a fifo as a persitent queue. | |
############################################################################# | |
target=${1:-target} | |
if [ -e "$target" ] | |
then rm "$target" | |
fi | |
mkfifo "$target" | |
# Launch a hang process to hold open the fifo and thereby prevent broken pipes | |
# as multiple processes to access the fifo and then exit. Set a trap to | |
# cleanup the hang loop on exit. | |
trap 'kill -9 "$hangpid"; exit' INT TERM EXIT | |
while true; do sleep 1; done < "$target" > "$target" & | |
hangpid=$! | |
echo abc > "$target" | |
echo pqr > "$target" | |
echo xyz > "$target" | |
head -n 3 < "target" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
############################################################################# | |
# Illustrates file locks producing coherent output. Check with: | |
# | |
# wc -l target # 30000 | |
# uniq -c target # random grouping of a, b, or c lines | |
# | |
############################################################################# | |
target=${1:-target} | |
lockfile=${1:"$target.lock"} | |
rm "$target" "$lockfile" | |
ruby -e " | |
10000.times do | |
puts 'a' * 10 | |
end | |
" | ./echo_with_lock.sh "$lockfile" >> "$target" & | |
ruby -e " | |
10000.times do | |
puts 'b' * 10 | |
end | |
" | ./echo_with_lock.sh "$lockfile" >> "$target" & | |
ruby -e " | |
10000.times do | |
puts 'c' * 10 | |
end | |
" | ./echo_with_lock.sh "$lockfile" >> "$target" | |
! grep -qE 'ab|ba' "$target" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
############################################################################# | |
# Illustrates a race condition when multiple processes append to the same | |
# target. Exit status 0 if the target has coherent output, and 1 if the | |
# output is garbled. | |
############################################################################# | |
target=${1:-target} | |
if [ -e "$target" ] | |
then rm "$target" | |
fi | |
ruby -e " | |
1000.times do | |
puts 'a' * 10 | |
end | |
" >> "$target" & | |
ruby -e " | |
1000.times do | |
puts 'b' * 10 | |
end | |
" >> "$target" | |
! grep -qE 'ab|ba' "$target" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment