summaryrefslogtreecommitdiffhomepage
path: root/tests/scripts/parallel.sh
blob: c516a2cdf7b31951c552190453349fcc59f7421b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Integration: run workers in the background and collect their results.
work() { # id seconds
    sleep "$2"
    echo "worker $1 done" >"result.$1"
    return "$1"
}
pids=
for i in 1 2 3 4; do
    work $i 0.$((5 - i)) &
    pids="$pids $!"
done
failures=0
for p in $pids; do
    wait "$p" || failures=$((failures + 1))
done
echo "failures: $failures"
cat result.1 result.2 result.3 result.4
(sleep 0.2; exit 9) & bg=$!
wait $bg; echo "wait \$!: $?"
for i in 1 2 3; do (echo "$i" >"o$i") & done; wait
cat o1 o2 o3 | tr '\n' ' '; echo