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

is there a way to set up a scrypt so that once it is exited it also forces the child process that it spawned to exit as well.

in other words, process A spawns process B using backticks(`), when process A is killed I want process B to be exited as well. So here we don't explicitly kill B through A, we set it up so that once A is gone, B gets dumped as well. I'm runing this on a PC platform.

Replies are listed 'Best First'.
Re: child process killing
by merlyn (Sage) on Oct 19, 2001 at 00:57 UTC
    By the time you've evaluated backticks, the child process is always already over.

    Perhaps you mean a grandchild process?

    In that case, you'll need to discover some out-of-band way to determine the kid, and kill it in an END block or something. I'm not a windows expert, so I'll stop there. {grin}

    -- Randal L. Schwartz, Perl hacker

      Thanks, but I do plan to exit the first process when it's inside the backticks sometimes, would you know of any to do that and have the child process die?
        You may plan to exit, but what merlyn is saying is that the backtick operator isn't interruptable. You may be thinking of fork, which lets the parent and the child go their merry ways.

        I'm not sure what would happen if the parent caught a signal while waiting for the backtick operation to complete, but it's possible you could interrupt the process there and exit the whole program. The only other possibility that comes to mind is causing a core dump or a segfault, and if you do that, the Perl 5 Porters would probably like to know how.

        In short, the backticks operator tells perl "run this program, wait for its output, and give it to me". You're not likely to get it to skip the second and third steps. (By "not likely", I mean, "even if you do find a way, you probably won't like the results.")

Re: child process killing
by Zaxo (Archbishop) on Oct 19, 2001 at 05:31 UTC

    That is supposed to be done for you by the OS. When the parent dies, the child is inherited by system init which kills it for you. I don't know how close to good signal emulation is on win32 perl.

    For fancier programs which use fork, keeping a list of kids, a signal handler can pass the signal along to the kids.

    After Compline,
    Zaxo