http://www.perlmonks.org?node_id=37205

reyjrar has asked for the wisdom of the Perl Monks concerning the following question: (programs and processes)

could I do something to kill off that child process regardless of where it is in its run after 2 minutes? I've heard messing with SIGs is bad and my first instinct is to:
SIG{'ALRM'} = sub {exit(0); } alarm '120';
but I don't want to break this box :)

thanks much..

Originally posted as a Categorized Question.

  • Comment on If I Want every child process to have EXACTLY 2 minutes to execute?
  • Download Code

Replies are listed 'Best First'.
Re: If I Want every child process to have EXACTLY 2 minutes to execute?
by tye (Sage) on Oct 17, 2000 at 22:28 UTC
    $SIG{ALRM}= sub { exit(0); }; alarm 120;

    runrig is certainly right when programming in C (set a flag and return). However, Perl signal handlers are unsafe no matter what you do so I'd go with your solution (with the typos fixed as I've shown).

         - tye
Re: If I Want every child process to have EXACTLY 2 minutes to execute?
by runrig (Abbot) on Oct 17, 2000 at 22:21 UTC
    Better to set a flag in the sig handler, and check for the flag throughout your code at key points. If you're paranoid anyway :)
Re: If I Want every child process to have EXACTLY 2 minutes to execute?
by AgentM (Curate) on Oct 17, 2000 at 23:13 UTC
    If your parent is just chillin' while the kiddies are out havin' fun, try using the signal killer in the parent, instead (it ain't got no nothin' betta ta doo!) Signals are still faulty in perl, but a process kill is a better process kill when you're not signaling yourself. sleep(120) in the parent while the kiddies are movin', after the wakeup, send a kill (or even better a sigqueue which no one uses!) to finish off those kiddies! Only one fatal signal involved which will best interrupt the kiddies at play- hurrah. If both the kiddie and the papa are executin', you can always write a short script to be their parents! (or pipe the PID to the "grandparent" and tell it to kill it after 2 minutes.) There's gotta be a UNIX command for that- if I could only remember...