Skip to content

Instantly share code, notes, and snippets.

@cjhanks
Last active December 14, 2015 07:39
Show Gist options
  • Save cjhanks/5052334 to your computer and use it in GitHub Desktop.
Save cjhanks/5052334 to your computer and use it in GitHub Desktop.
Showing atomic consistency of Unix PIPES in BASH.
#!/bin/bash
set -e
pipe_name="test_pipe"
# --------------------------------------------------------------------------- #
pipe_ostream () {
char="$1"
data=$(for i in {1..1024}; do printf "$1"; done;)
printf $data | dd iflag=fullblock if=/dev/stdin of=$pipe_name \
ibs=1024 \
obs=1024 \
count=1
}
pipe_istream () {
o_file="$1"
dd if="$pipe_name" of=$o_file \
iflag=fullblock \
ibs=1024 \
obs=1024 \
count=1
}
# --------------------------------------------------------------------------- #
# CLEANUP
if [ -e $pipe_name ]; then
rm $pipe_name
fi
mkfifo $pipe_name
if [ -e out.a ]; then rm out.a; fi
if [ -e out.b ]; then rm out.b; fi
if [ -e out.c ]; then rm out.c; fi
if [ -e out.d ]; then rm out.d; fi
sleep .2
# - THIS WORKS ----------------------------------------------------------------
#pipe_ostream "a" &
#pipe_ostream "b" &
#pipe_ostream "c" &
#pipe_ostream "d" &
#
#pipe_istream "out.a"
#pipe_istream "out.b"
#pipe_istream "out.c"
#pipe_istream "out.d"
#
#wait
#exit 0
# - THIS FAILS ----------------------------------------------------------------
#pipe_istream "out.a" &
#pipe_istream "out.b" &
#
#pipe_ostream "a"
#pipe_ostream "b"
#
#wait
#exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment