Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > SIGCHLD handler not working correctly

Reply
Thread Tools

SIGCHLD handler not working correctly

 
 
Asfand Yar Qazi
Guest
Posts: n/a
 
      12-15-2007
Hi,

Here's some test code that demonstrates what I'm doing:

----------------------cut here----------------------

#!/usr/bin/ruby -w

trap_quit = lambda { |sig|
signame = Signal.list.find {|k, v| v == sig }[0]
STDERR.puts("Trapping signal #{signame}")
}

huphandler = inthandler = termhandler = chldhandler = nil
huphandler = trap("HUP", &trap_quit)
inthandler = trap("INT", &trap_quit)
termhandler = trap("TERM", &trap_quit)

cmd_exitstatus = nil

chldhandler = trap("CHLD") {
STDERR.puts("Trapping signal CHLD")
begin
# tmppid, status = Process.wait2(cmdpid)
tmppid = Process.wait
status = $?
cmd_exitstatus = status.exitstatus
STDERR.puts("Process::Status => " + status.inspect)
STDERR.puts("exitstatus => " + cmd_exitstatus.inspect)
if ! status.exited?
raise "ARGH! The child has not exited yet!"
end
rescue Errno::ECHILD => e
STDERR.puts("Command failed - probably ok? #{e.inspect}")
# bah, ignore it, just means a command failed
end
}

cmdpid = fork {
begin
exec("dd", "if=/dev/zero", "of=/tmp/zerofile1.asdfsdf",
"bs=1", "count=333000000")
rescue Errno::ENOENT => e
STDERR.puts e.message
exit 127
end
}

while(cmd_exitstatus.nil?)
sleep 1
STDOUT.puts "still going..."
STDOUT.flush
end

----------------------cut here----------------------

To see the problem, run the command, then press CTRL-C while it's
running. It will say "ARGH! The child has not exited yet!"

Inside the CHLD handler, exitstatus should be the valid exit status of
the child process. However, it is nil. This is really really bad,
because it seems to imply that the child signal, which is meant to be
caught when a child exits, is being called while the child has NOT exited.

Anyone tell me what I'm doing wrong?

Thanks

--
To reply, take of all ZIGs !!

Alternative email address:
 
Reply With Quote
 
 
 
 
Lionel Bouton
Guest
Posts: n/a
 
      12-15-2007
Asfand Yar Qazi wrote the following on 15.12.2007 09:30 :
> --
>
> To see the problem, run the command, then press CTRL-C while it's
> running. It will say "ARGH! The child has not exited yet!"
>
> Inside the CHLD handler, exitstatus should be the valid exit status of
> the child process.


If I'm not mistaken, there's none because the child didn't call exit.

> However, it is nil. This is really really bad,
> because it seems to imply that the child signal, which is meant to be
> caught when a child exits, is being called while the child has NOT exited.
>


Looking at Process::Status#exited? documentation it seems it only
returns true if the process actually exited on its own. Here dd dies
because of a SIGINT (calling $?.signaled? returns true).

I'm not familiar with the signal handling details, so the following is
no more than an educated guess.
I believe that depending on the actual implementation of the signal
handler you can get different reactions to a signal relative to exit
status. Either:
- the program aborts *in the signal handler* and don't return an exit
status,
- the signal handler is only used to modify internal structures and
doesn't abort the program right away. The normal course of the program
only checks for the exit condition so you get a delayed "normal" exit
controlled by a signal. In this case you get an exit status.

Lionel

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Missing SIGCHLD Dan Stromberg Python 1 02-16-2011 05:22 AM
SIGCHLD not called under Windows kalahari@gmail.com Perl Misc 1 10-31-2006 07:07 PM
Does pthread emit a sigchld signal on successful exit? chuckles C Programming 2 08-18-2005 09:29 PM
problem in POSIX module with handling SIGCHLD msoulier Perl Misc 1 07-15-2005 11:11 PM
ignoring SIGCHLD Moritz Karbach Perl Misc 3 06-23-2005 12:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57