########################################################################
# #
# This software is part of the ast package #
# Copyright (c) 1982-2012 AT&T Intellectual Property #
# and is licensed under the #
# Eclipse Public License, Version 1.0 #
# by AT&T Intellectual Property #
# #
# A copy of the License is available at #
# (with md5 checksum b35adb5213ca9657e911e9befb180842) #
# #
# Information and Software Systems Research #
# AT&T Research #
# Florham Park NJ #
# #
# David Korn <dgk@research.att.com> #
# #
########################################################################
function err_exit
{
print -u2 -n "\t"
}
alias err_exit='err_exit $LINENO'
s=$($SHELL -c '
integer i foreground=0 background=0
set --errexit
trap "(( background++ ))" CHLD
(( d = delay ))
do sleep $d &
(( d *= 4 ))
(( s += d ))
done
do (( foreground++ ))
sleep $delay
(( s -= delay ))
done
if (( (s += delay) < 1 ))
then (( s = 1 ))
fi
sleep $s
wait
print foreground=$foreground background=$background
eval $s
(( foreground == FOREGROUND )) || err_exit "expected '$FOREGROUND foreground' -- got '$foreground' (DELAY=$DELAY)"
(( background == BACKGROUND )) || err_exit "expected '$BACKGROUND background' -- got '$background' (DELAY=$DELAY)"
set --noerrexit
then
jobmax=4
integer running=0 maxrunning=0
trap "((running--))" CHLD
for ((i=0; i<JOBCOUNT; i++))
do sleep 1 &
if ((++running > maxrunning))
then ((maxrunning=running))
fi
done
wait
print running=$running maxrunning=$maxrunning
')
typeset -A proc
trap "
print \${proc[\$!].name} \${proc[\$!].status} \$?
unset proc[\$!]
" CHLD
{ sleep 3; print a; exit 1; } &
proc[$!]=( name=a status=1 )
{ sleep 2; print b; exit 2; } &
proc[$!]=( name=b status=2 )
{ sleep 1; print c; exit 3; } &
proc[$!]=( name=c status=3 )
while (( ${#proc[@]} ))
do sleep -s
done
')
exp='c\nc 3 3\nb\nb 2 2\na\na 1 1'
[[ $got == $exp ]] || err_exit "SIGCHLD trap queueing failed -- expected $(printf %q "$exp"), got $(printf %q "$got")"
fi
{
trap handler CHLD; sleep .3 & IFS= read; print good')
set -- $(
(
trap \'wait $!; print $! $?\' CHLD
{ sleep 0.1; exit 9; } &
print $!
sleep 0.5
'
)
if (( $# != 4 ))
then err_exit "CHLD trap failed -- expected 4 args, got $#"
elif (( $4 != 0 ))
then err_exit "CHLD trap failed -- exit code $4"
elif (( $1 != $2 ))
then err_exit "child pid mismatch -- got '$1' != '$2'"
elif (( $3 != 9 ))
then err_exit "child status mismatch -- expected '9', got '$3'"
fi
trap '' CHLD
integer d
for ((d=0; d < 2000; d++))
then break
fi
done
trap - CHLD
trap 'rm -rf $tmp' EXIT