Skip to content

Instantly share code, notes, and snippets.

@colejhudson
Last active December 6, 2017 01:28
Show Gist options
  • Save colejhudson/90615fdfb8c992755a584a3e9e104c80 to your computer and use it in GitHub Desktop.
Save colejhudson/90615fdfb8c992755a584a3e9e104c80 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# 'cat' seems to take multiple file descriptors (fd) - vaguely analgous to pointers -
# and queues them, polling for available data and outputting it in a blocking FIFO manner.
# It moves onto the next fd condition on the closing of the last.
function cat_subprocess_handeling() {
START=${SECONDS}
cat -- \
<(
# First FD redirct
START=${SECONDS}
echo "[1] pid:${$}"
{ (sleep 3; echo "[1.1] pid:${$}"; echo "[1.1] Attempting to background..."; echo "[1.1] test 1"; echo "[1.1] test 2") & };
END=${SECONDS}
ELAPSED=$(( END - START ))
echo "[1] Executed in ${ELAPSED}"
) <(
START=${SECONDS}
echo "[2] pid:${$}"; echo "[2] sleep test 1"; sleep 1; echo "[2] sleep test 2"
END=${SECONDS}
ELAPSED=$(( END - START ))
echo "[2] Executed in ${ELAPSED}"
) <(
START=${SECONDS}
echo "[3] pid:${$}";{ sleep 3 && echo "[3] async test 1" & };echo "[3] async test 2"
END=${SECONDS}
ELAPSED=$(( END - START ))
echo "[3] Executed in ${ELAPSED}"
)
END=${SECONDS}
ELAPSED=$(( END - START ))
echo "cat took ${ELAPSED} seconds to complete"
}
# time how_do_cat_and_subprocesses_interact
# START TIME - 15781
# [1] pid:18994
# [1] Executed in 0
# [1.1] pid:18994
# [1.1] Attempting to background...
# [1.1] test 1
# [1.1] test 2
# [2] pid:18994
# [2] sleep test 1
# [2] sleep test 2
# [2] Executed in 1
# [3] pid:18994
# [3] async test 2
# [3] Executed in 0
# [3] async test 1
# END TIME - 15784
# cat took 3 seconds to complete
#
# real 0m3.003s
# user 0m0.000s
# sys 0m0.000s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment