in reply to Forking Issue

Your fork is backwards. To get this to work, you can send a signal to the child process with kill -- SIGTERM is a good choice. Someone more versed in signals on non-Unix platforms will have to correct my assumptions (I'm thinking of tye or AgentM on this one).
if (my $pid = fork) { sleep 1; kill 9, $pid; wait; } else { while (1) { print "Yay!\n"; } }
The wait may or may not be necessary, if you have automatic reaping. waitpid may be more appropriate.

On the other hand, you could also use Time::HiRes and check to see if you've printed for enough ticks. That would relieve you of having to fork, especially on those platforms where you don't want to use alarm. (On Windows, I think it's implemented in the same manner as fork(), but again, someone who knows Perl on Windows will have to verify this.)

Update: dkubb has provided cleaner and slightly more elegant code. I would use his in production.

Replies are listed 'Best First'.
(dkubb) Re: (3) Forking Issue
by dkubb (Deacon) on Mar 18, 2001 at 11:14 UTC

    This example will not catch errors with fork. You first need to test if the returned $pid is defined or not:

    unless(defined(my $pid = fork)) { die "Could not fork: $!"; #fork failed } elsif($pid) { sleep 1; #parent kill 9, $pid; wait; } else { print "Yay!\n" while 1; #child }
Re: Re: Forking Issue
by Anonymous Monk on Mar 17, 2001 at 11:02 UTC
    if (my $pid = fork) { sleep 1; kill 9, $pid; die("Shplah"); } else { while (1) { print "Yay!\n"; } }
    This should exit the program on-die, correct? It doesn't work. I get the little flashing cursor after the die message.