wait4 (waitpid) and trapped signals

wait4 (waitpid) and trapped signals

Post by Vincent Lefevr » Sun, 13 Apr 2003 22:37:55

Consider the following sh script:

echo "PID: $$"


trap pterm 15
sleep 10

Under Linux, when I send a TERM signal to the process, this doesn't
call the pterm function immediately, only when the child (sleep 10)
has terminated. This can be seen with the strace output:

     0.000235 wait4(-1, 0x7ffff258, 0, NULL) = ? ERESTARTSYS (To be restarted)

     0.000120 wait4(-1, [WIFEXITED(s) && WEXITSTATUS(s) == 0], 0, NULL) = 24952
     5.754998 rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0

Is there a (portable) way to give the priority to the signal handler
so that it is called before the wait4 system call is restarted?

With a similar program written in Perl:

#!/usr/bin/env perl
use strict;
my $pid;
$SIG{'TERM'} = sub
  { warn "[".localtime()."] $$ - SIGTERM\n";
    kill 'TERM', $pid unless $pid == 0;
    exit; };
$pid = fork;
defined $pid or die "$0: can't fork\n";
unless ($pid)
  { sleep 17;
    exit; }
warn "[".localtime()."] PID: $$ (parent) - $pid (child)\n";
waitpid -1, 0;

there is no such problem. The strace output says:

     0.008290 wait4(-1, 0x7ffff418, 0, NULL) = ? ERESTARTSYS (To be restarted)

     0.000252 brk(0x10028000)           = 0x10028000
     0.000222 time([1050153664])        = 1050153664

Note: this is under Linux, but I also want to use my scripts on other
unices, in particular OSF1 and Solaris.

TIA for any information,


validated (X)HTML - Acorn Risc PC, Yellow Pig 17, Championnat International
des Jeux Mathmatiques et Logiques, TETRHEX, etc.
Work: CR INRIA - computer arithmetic / SPACES project at LORIA


1. trap "<new trap action>; $(get_old_trap SIGNAL)" SIGNAL

I want to be able to add new trap to existing trap by saying:

trap "<new trap action>; $(get_old_trap SIGNAL)" SIGNAL

Please comment if the following function get_old_trap()
covers all cases. This only works with ksh93, and that is ok
for me. This is because:

(1) bash as of the current release (v3.0) is not POSIX compliant as
demonstrated by:

trap 'echo a' exit
echo $a
/* no output */

POSIX requires that the old trap is saved in $a.

(2) while ksh88 saves the trap, it does not in a function:

$ /bin/ksh # Version M-11/16/88i
$ trap 'echo a' exit
$ a=$(trap)
$ echo $a
trap -- 'echo a' EXIT
$ a() { trap; }
$ a


get_old_trap() {
  typeset line signal="$1" old_trap=/tmp/$$.$RANDOM
  trap > $old_trap
  exec 3< $old_trap
  rm $old_trap
  while read -u3 -r -- line; do
    [[ "$line" == *?(SIG)$signal ]] || continue
    line=${line#trap --+( )}
    eval line=${line%+( )?(SIG)$signal}
    printf "%s\n" "$line"
    return 0
  return 1

trap "echo trap1" EXIT
trap "echo trap2; $(get_old_trap EXIT)" EXIT
For low fair air travel, take Independence Air - http://www.flyi.com/.

2. PS/2 mouse jitter

3. trap "action; exit" signals = trap "action" exit?

4. sendmail configuration

5. Will the waitpid() in parent process return if I don't catch the SIGCHLD signal?

6. IBM ISA token ring and RH 6.x

7. UNP Book Question - waitpid in SIGCHLD signal handler

8. csh vars questions: URL passed to IE (on Solaris) from script

9. signal trapping

10. Traps,Signals,interrupts

11. how to trap signals in csh script ?

12. signal trapping problem in [k]sh...

13. What is signal 6, IOT trap/Abort?