On 11/21/20 3:06 AM, Nikolay Borisov wrote: > The output is: > > my pid 12186 > 12186 subfun xxx 0 > funpid=12186 twopid=12187 mypid=12185 > killing 12186 12187 > waiting on everything > my pid 12187 > 12187 subfun yyy 0 > received term for xxx > 12187 subfun yyy 1 > 12187 subfun yyy 2 > 12187 subfun yyy 3 > > > and 12187 keeps printing. It would seem there $! can return a pid of the > subshell before it's in a state that it can be killed.
This is a potential race condition, but it's not with the shell. The parent shell sets $! as soon as the child pid returns from fork(), and that doesn't have anything to do with when the child gets scheduled or runs. You might have better luck running this under strace or truss and seeing what happens with the timing: whether the child sets SIGTERM to the trap handler before the signal arrives, or whether the child resets the SIGTERM handler to what the parent had before the trap before the signal arrives. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRU c...@case.edu http://tiswww.cwru.edu/~chet/