Here's an extremely simplistic example of how to fork a child process off and have it running alongside the parent... if you want to do something simple and non-vital, you could do it this way...
beware though, there are many problems and pitfalls with forks. (
perhaps in a future version of perl, there'll be a spork() function that will eliminate some of the problems with fork() :))
To use this code, just replace the while() loops with whatever function calls you need to make... hope it helps
#!/usr/bin/perl
use strict;
my $k_pid;
die "Can't fork! ($!)" unless defined ($k_pid = fork());
my $i = 1;
my $n = 1;
if ($k_pid) {
#parent code
while (1==1) {
$n++;
print "Parent ($i)($n) \n";
sleep(1);
}
kill("TERM" => $k_pid);
} else {
#child code
while (1==1) {
$i++;
print "Child: ($i)($n)\n";
sleep(1);
}
}
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.