Last active
April 11, 2018 11:42
-
-
Save borisisok/0edc75a282b0272c12f7d316c01faf7a to your computer and use it in GitHub Desktop.
TIL: multiple bash coprocs with dynamic names
This file contains 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
#!/usr/bin/bash | |
PID_MAIN=$$ | |
# notify subshells on ctrl-c | |
trap "kill 0" SIGINT | |
function wrk() { while read data; do echo $1 _${data}_; done } | |
function shw() { | |
pid=$1 | |
fd=$2 | |
while IFS= read result; do | |
echo out: $result | |
done < /proc/${pid}/fd/${fd} | |
} | |
# return worker names like "wrk0" | |
function workers() { | |
for i in {1..5}; do | |
echo wrk${i} | |
done | |
} | |
# spawn worker and printer processes | |
for worker in $(workers); do | |
eval "coproc ${worker} { wrk $worker; }" | |
fdname="${worker}[0]" | |
( shw ${PID_MAIN} ${!fdname} ) & | |
done | |
echo Filedescriptors of main process: | |
ls -lFh /proc/$$/fd/ | |
echo List of processes in processgroup: | |
jobs -l | |
echo feeding some test data to the workers | |
for worker in $(workers); do | |
fdname="${worker}[1]" | |
echo $fdname | |
echo test > /proc/$PID_MAIN/fd/${!fdname} | |
done | |
sleep 100 | |
kill 0 | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using multiple coprocs works nice but shows warnings like: