Hi All,
I have been farting and fooling around with fork, and I was trying to see the time difference in having child processes create more child processes to complete a task vs the parent creating all the children. so here is what I did:
#!/usr/bin/perl -w
use strict;
use Time::HiRes qw(usleep ualarm gettimeofday tv_interval);
open (FILE, ">testfork.txt");
my $runs = 33;
my $start = [gettimeofday];
while ($runs) {
my $pid = fork;
# Parent
if ($pid) { $runs--; }
# Child
if ($pid == 0) {
my $pid = fork;
if ($pid == 0) {
my $pid = fork;
if ($pid == 0) {
print FILE "$runs\n";
exit;
}
print FILE "$runs\n";
exit;
}
print FILE "$runs\n";
exit;
}
}
my $elapse = tv_interval ($start);
close (FILE);
print "Elapsed time = $elapse\n";
exit;
This should create 99 lines in the output file and it does. I then change the code to:
#!/usr/bin/perl -w
use strict;
use Time::HiRes qw(usleep ualarm gettimeofday tv_interval);
open (FILE, ">testfork.txt");
my $runs = 99;
my $start = [gettimeofday];
while ($runs) {
my $pid = fork;
# Parent
if ($pid) { $runs--; }
# Child
if ($pid == 0) {
# my $pid = fork;
# if ($pid == 0) {
# my $pid = fork;
# if ($pid == 0) {
# print FILE "$runs\n";
# exit;
# }
# print FILE "$runs\n";
# exit;
# }
print FILE "$runs\n";
exit;
}
}
my $elapse = tv_interval ($start);
close (FILE);
print "Elapsed time = $elapse\n";
exit;
This should also create 99 lines in the output file, but consistently after 65 lines I get the following:
Use of uninitialized value in numeric eq (==) at testfork1.pl line 16.
I can not see why this is happening. Does this happen to anyone else running the code? If can anyone enlighten me on why?
Thank you all in advance,
Cameron
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.