Eradicatore has asked for the wisdom of the Perl Monks concerning the following question:

Ok, now that I have perl/tk working with fork I have another question about the forked child process. In the following code, the child happily waits for urls from the parent to go get them (I'm on win32 btw). But the quit button ends the parent but not the child for some reason. Also, I want the child to end if the user clusily kills the parent window from the "x" on the windows window. I read the signal info in the IPC area but didn't really understand it. Thanks for any help!
#!/usr/local/bin/perl -w use POSIX; use Tk; use LWP::UserAgent; use URI::URL; pipe(FROM_P, TO_C) or die "pipe: $!"; select(((select(TO_C), $| = 1))[0]); my $ua = LWP::UserAgent->new; if (!($kid_pid = fork)) { close(TO_C); while($line = <FROM_P>) { chomp($line); my $url = $line; my $file = 'yahoo.txt'; my $req = HTTP::Request->new(GET => $url); $req->header('Accept' => 'text/html'); $ua->request($req, $file); }; POSIX::exit(0); } my $mw = MainWindow->new(); $button = $mw->Button(-text => "hello", -activebackground => 'red', -command => \&printit)->pack(); $button2 = $mw->Button(-text => "quit", -command => \&myexit)->pack(); MainLoop; sub printit { print TO_C "http:\/\/www\.yahoo\.com\n"; } sub myexit { kill 9, $kid_pid; exit; }

Justin Eltoft

"If at all god's gaze upon us falls, its with a mischievous grin, look at him" -- Dave Matthews

Replies are listed 'Best First'.
Re: Making sure child process dies with parent on Win32
by the_slycer (Chaplain) on Jun 08, 2001 at 09:32 UTC
    What you need to do is trap the signal that is sent when the program exits. I can't remember off the top of my head what that is called on Windows - but if you print keys %SIG you should be able to tell.

    Once you get it figured out which signal you need to trap, put something like the following in your code (before the MainLoop).
    $SIG{KILL}=sub { kill 15 => $kid; exit; };
    Note, you will also want to read through perlipc for more info regarding trapping of signals.
Re: Making sure child process dies with parent on Win32
by Anonymous Monk on Jun 08, 2001 at 16:52 UTC
    Here are the signals on my windows machine. I tried exactly what you said and none of these I tried seemed to work. I tried __die__, segv, kill, quit, and stop. I even thought that maybe the kill command just isn't working and so I tried a print statement in the signal subroutine and nothing.

    (all are capitalized really) stop, num05, num06, alrm, num07, num24, num16, __warn__, num17, num18, num19, ill, chld, segv, __die__, pipe, cld, abrt, cont, int, quit, kill, break, term, num01, fpe, num10, num12

    Please let me know if I didn't try the right ones. Also, I just wanted to point out that I think "kill 15, $kid_pid" doesn't work either just because I have a subroutine called "myexit" that runs when the users clicks the exit button from my tk app (rather than killing the windows window) and even that leaves the child hanging. Any idea on that either?

    Eradicatore

      Whoops - me bad, there is a better way in Tk - use OnDestroy. Now that I'm back at work I've looked at some of the Tk stuff that I've done and this is what I've used.
      $main_window->OnDestroy(sub { kill 15 => $kid; exit; } );
      The OnDestroy is a Tk routine, should do the trick. Get rid of the myexit sub and use this, you should be Ok. kill does work - just make sure you are killing the right pid :-)