Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Re: Killing child process

by sgifford (Prior)
on Oct 12, 2003 at 15:49 UTC ( [id://298663]=note: print w/replies, xml ) Need Help??


in reply to Re: Killing child process
in thread Killing child process

I couldn't get that to work:
#!/usr/bin/perl -w use strict; $SIG{TERM}= sub { print "TERM $$\n"; exit(1); }; if (my $kidpid = fork) { # Parent sleep(1); print "\n"; kill -15, $kidpid; sleep(5); } else { print "CREATED CHILD $$\n"; # Child for my $i (1..10) { if (!fork) { # Grandchild print "CREATED GRANDCHILD $$\n"; sleep(5); exit(0); } } sleep(300); }
Is there a bug in this code, or are things not working as documented?

Replies are listed 'Best First'.
Re: Re: Re: Killing child process
by Thelonius (Priest) on Oct 13, 2003 at 02:20 UTC
    Oops, yeah, I forgot you need to set the process group for the process you want to be a group leader. To do this call setpgrp(0,0); after the fork. In your test program, do it right where you have print "CREATED CHILD $$\n"; and don't do it in the grandchildren.

      Cool, that worked! Thanks, I've always wondered how process groups worked.

      Now, I wonder if this answered the OP's question?... :-)

Re: Re: Re: Killing child process
by pg (Canon) on Oct 12, 2003 at 17:02 UTC
    Two points about OS:
    • To send a signal to a process group, you have to be a super user;
    • Most of OS's, if not all, does not let you capture SIGTERM for ordinary users. That's for a good reason, if some processes (intentionally or by mistake - bugs) decide to be alive forever, that will really be a nightmare.
        • This program doesn't work when run as root either.
        • I believe you're thinking of SIGKILL. SIGTERM is a perfectly normal signal. If I change the kill command to just send a normal signal to $kidpid, then the child process prints the TERM message, but not the grandchildren. Similarly, if you make the sleeps a little longer, you can send TERM signals by hand to individual grandchildren, and they print the message.

    Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Node Status?
    node history
    Node Type: note [id://298663]
    help
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (6)
    As of 2024-03-19 14:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found