I am trying to learn how to fork() standalone processes from a parent Perl script. When I execute my learning script it does fork the sub functions, but one at a time. What I am trying to learn is how do I launch all the sub functions immediately so that all the forks are running together with the parent. Here is my learning code. I have tried reading the man on fork(), but I must be dense, stupid or both, because I can't seem to figure it out.
#!/usr/bin/perl
$SIG{CHLD} = "IGNORE";
$f1="file1.txt";
$f2="file2.txt";
$f3="file3.txt";
$naptime=5;
sub count{
print "Starting Fork 1\n";
open(FILE1, ">$f1");
while($x < 10){
$x=$x+1;
print FILE1 "$x\n";
sleep($naptime);
}
close(FILE1);
fork && exit;
}
sub count2{
print "Starting Fork 2\n";
open(FILE2, ">$f2");
while($y < 10){
$y=$y+1;
print FILE2 "$y\n";
sleep($naptime);
}
close(FILE2);
fork && exit;
}
count();
count2();
print "Starting Parent Process\n";
open(FILE3, ">$f3");
while($z < 10){
$z=$z+1;
print FILE3 "$z\n";
sleep($naptime);
}
close(FILE3);
print "Parent Script Finished!\n";
########################################################
Somebody please put me out of my misery!
Thanks,
Danny
update (broquaint): added <code> tags
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.