["Followup-To:" header set to comp.lang.perl.misc.]
Here is a copy a persons code I saw on a blog. The only difference
here is that I enable warnings use my().
#!/usr/bin/perl -w
my @array = qw(ugh geeze blah test smith bob homes point);
my $num = "10";
for(1..$num) {
my $pid = fork();
if ($pid) {
# parent
push(@childs, $pid);
} elsif ($pid == 0) {
# child
print "@array\n\n";
sleep 5;
exit(0);
} else {
die "couldn't fork: $!\n";
}
print "BEFORE FOR BRACKET\n";
}
print "AFTER FOR BRACKET\n";
foreach (@childs) {
waitpid($_, 0);
}
And here is what I get when I run it a few times... [snip]
[cdalten@localhost perl]$ ./par.pl [...]
BEFORE FOR BRACKET
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
ugh geeze blah test smith bob homes point
I presume that the above apparent out-of-order results is what you are
concerned about? [...]
How would I get them in sync?
Would I have to insert another wait() into the child? Just need some ideas
here.
Move your waitpid() call so that it executes immediately prior to
your
print "BEFORE FOR BRACKET\n";
statement. This way, the parent will wait for the child to complete
(and thus write the array) before it proceeds to generate the BEFORE
FOR BRACKET line.
If you do that, what's the point of forking in the first place?
Maybe I'm really not understanding what you are saying. The script is
a stripped down version of a wrapper program. Anyhow, I think I got it
running parallel. Here is the output.
#!/usr/bin/perl -w
use File::stat;
use Time::localtime;
my $file="/home/cdalten/perl/out";
my @array = qw(ugh geeze blah test smith bob homes point);
my $num = "50";
for(1..$num) {
my $pid = fork();
if ($pid) {
# parent
push(@childs, $pid);
my $oldfh = select(STDOUT); $| = 1; select ($oldfh);
} elsif ($pid == 0) {
# child
#my $oldfh = select(STDOUT); $| = 1;
print "@array\n";
#$oldfh = select(STDOUT); $| = 1; select($oldfh);
#sleep 5;
exit(0);
} else {
die "couldn't fork: $!\n";
}
#my $olderfh = select(STDOUT); $| = 1;
foreach (@childs) {
waitpid($_, 0);
}
$date_string = ctime(stat($file)->mtime);
print "file $file updated at $date_string\n";
print "BEFORE FOR BRACKET\n";
#$olderfh = select(STDOUT); $| = 1; select($olderfh);
}
#$oldestfh = select(STDOUT); $| = 1; select($oldestfh);
print "AFTER FOR BRACKET\n";
#$date_string = ctime(stat($file)->mtime);
#print "file $file updated at $date_string\n";
#print " $atime, $mtime \n";
#$oldestfh = select(STDOUT); $| = 1; select($oldestfh);
#foreach (@childs) {
# waitpid($_, 0);
#}
Yes, I know that when I redirect a fork to a output file, I need to
probably flush the buffer. However, I was just testing to see if I
would get the same timestamp on the print statements for all the
forks. If the timestamps are the same, then the program is running
in parallel. Here is part of the ouput.
[cdalten@localhost perl]$ ./par.pl > out
[cdalten@localhost perl]$ more out
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET
ugh geeze blah test smith bob homes point
file /home/cdalten/perl/out updated at Thu Apr 5 06:28:54 2007
BEFORE FOR BRACKET