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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.