[29063] in Perl-Users-Digest
Perl-Users Digest, Issue: 307 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Apr 5 11:09:40 2007
Date: Thu, 5 Apr 2007 08:09:07 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 5 Apr 2007 Volume: 11 Number: 307
Today's topics:
Re: How to resolve funky sync issues with fork here. <cdalten@gmail.com>
Re: How to resolve funky sync issues with fork here. <cdalten@gmail.com>
Re: How to resolve funky sync issues with fork here. <cdalten@gmail.com>
Re: How to resolve funky sync issues with fork here. <hjp-usenet2@hjp.at>
Re: Looking for a Perl or (similar) type of script to p <bik.mido@tiscalinet.it>
Re: perl hash - creation by side effect? <bik.mido@tiscalinet.it>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 5 Apr 2007 06:34:54 -0700
From: "grocery_stocker" <cdalten@gmail.com>
Subject: Re: How to resolve funky sync issues with fork here.
Message-Id: <1175780094.786712.175120@q75g2000hsh.googlegroups.com>
On Apr 5, 6:05 am, "Peter J. Holzer" <hjp-usen...@hjp.at> wrote:
> ["Followup-To:" header set to comp.lang.perl.misc.]
> On 2007-04-04 12:22, Lew Pitcher <lpitc...@sympatico.ca> wrote:
>
>
>
> > On Apr 3, 9:27 pm, "grocery_stocker" <cdal...@gmail.com> wrote:
> >> 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 where the same, then the program in running
in parallel.
[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
------------------------------
Date: 5 Apr 2007 06:39:22 -0700
From: "grocery_stocker" <cdalten@gmail.com>
Subject: Re: How to resolve funky sync issues with fork here.
Message-Id: <1175780362.336715.173430@y66g2000hsf.googlegroups.com>
On Apr 5, 6:05 am, "Peter J. Holzer" <hjp-usen...@hjp.at> wrote:
> ["Followup-To:" header set to comp.lang.perl.misc.]
> On 2007-04-04 12:22, Lew Pitcher <lpitc...@sympatico.ca> wrote:
>
>
>
> > On Apr 3, 9:27 pm, "grocery_stocker" <cdal...@gmail.com> wrote:
> >> 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
------------------------------
Date: 5 Apr 2007 06:43:27 -0700
From: "grocery_stocker" <cdalten@gmail.com>
Subject: Re: How to resolve funky sync issues with fork here.
Message-Id: <1175780607.660846.77950@n59g2000hsh.googlegroups.com>
On Apr 5, 6:05 am, "Peter J. Holzer" <hjp-usen...@hjp.at> wrote:
> ["Followup-To:" header set to comp.lang.perl.misc.]
> On 2007-04-04 12:22, Lew Pitcher <lpitc...@sympatico.ca> wrote:
>
>
>
> > On Apr 3, 9:27 pm, "grocery_stocker" <cdal...@gmail.com> wrote:
> >> 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?
>
> hp
>
> --
> _ | Peter J. Holzer | Blaming Perl for the inability of programmers
> |_|_) | Sysadmin WSR | to write clearly is like blaming English for
> | | | h...@hjp.at | the circumlocutions of bureaucrats.
> __/ |http://www.hjp.at/| -- Charlton Wilbur in clpm
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 is the same, then the program is running
in parallel. Here is part of the output.
[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
------------------------------
Date: Thu, 5 Apr 2007 16:24:21 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: How to resolve funky sync issues with fork here.
Message-Id: <slrnf1a1kl.hh1.hjp-usenet2@yoyo.hjp.at>
["Followup-To:" header set to comp.lang.perl.misc.]
On 2007-04-05 13:34, grocery_stocker <cdalten@gmail.com> wrote:
> On Apr 5, 6:05 am, "Peter J. Holzer" <hjp-usen...@hjp.at> wrote:
>> On 2007-04-04 12:22, Lew Pitcher <lpitc...@sympatico.ca> wrote:
>>
>>
>>
>> > On Apr 3, 9:27 pm, "grocery_stocker" <cdal...@gmail.com> wrote:
>> >> 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);
>>
>> >> }
>>
>> > 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.
Your original script was running in parallel. But with the change
suggested by Lew, it isn't running in parallel any more:
> #!/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();
here you fork exaclty one child
> if ($pid) {
> # parent
> push(@childs, $pid);
> my $oldfh = select(STDOUT); $| = 1; select ($oldfh);
The parent does nothing except remember the pid of the child
>
> } elsif ($pid == 0) {
> # child
> #my $oldfh = select(STDOUT); $| = 1;
>
> print "@array\n";
> #$oldfh = select(STDOUT); $| = 1; select($oldfh);
>
> #sleep 5;
> exit(0);
The child does its work (put the sleep 5 in again to see that it takes
some time)
> } else {
> die "couldn't fork: $!\n";
> }
>
> #my $olderfh = select(STDOUT); $| = 1;
> foreach (@childs) {
> waitpid($_, 0);
here you wait for the child. After that, the child is finished.
> }
>
> $date_string = ctime(stat($file)->mtime);
> print "file $file updated at $date_string\n";
Here the parent does some more work.
>
> print "BEFORE FOR BRACKET\n";
> #$olderfh = select(STDOUT); $| = 1; select($olderfh);
>
> }
So at any point you have either a single child running (and the parent
waiting) or no child at all. There is never more than one process which
does work, so you can just forget about the fork and do everything in
the parent.
Now I realize that you probably need several processes running in
parallel, that's why I think Lew's suggestion doesn't make sense.
>
> 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 where the same, then the program in running
> in parallel.
You never change the file, so why should its timestamp change?
To return to your original question: First decide what you need to
synchronise - i.e., where (and why!) the parent needs to wait for a
child to do something before continuing or a child needs to wait for the
parent or one child needs to wait for another. Once you know that you
can decide what the appropriate synchronization mechanism is (waitpid
probably isn't - it can only wait for a child process to die), for
example semaphores, file locks, messages written to pipes, etc.
hp
--
_ | Peter J. Holzer | Blaming Perl for the inability of programmers
|_|_) | Sysadmin WSR | to write clearly is like blaming English for
| | | hjp@hjp.at | the circumlocutions of bureaucrats.
__/ | http://www.hjp.at/ | -- Charlton Wilbur in clpm
------------------------------
Date: Thu, 05 Apr 2007 15:44:36 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Looking for a Perl or (similar) type of script to parse XML files for errors
Message-Id: <r0v913lq7d15umdgln7posvb6iag1as3kv@4ax.com>
On 4 Apr 2007 15:32:20 -0700, "Will" <WBible@gmail.com> wrote:
>> java?!?
[snip]
>The app. was written in Java and produces an error file/log w/ the
>error messages and the XML data section applied to it. I'm not a java
>guy but are you asking if java is the way to go here? Thanks
I just couldn't understand what Java could have to do with the rest of
what you wrote, especially since it wasn't mentione before. Now that I
know what it has to do, I can tell you that it doesn't really have to
do with your problem: no matter what the implementation language of
the program who wrote the XML was, only the latter is relevant. Thus
you may try posting as small but supposedly complete enough fragment
of it along with an indication about the info you would like to
extract. My previous advice still applies though: this kind of
question gets asked quite frequently here. You'll find many directions
to various modules designed exactly for this purpose, and possibly
some minimal example of how to use them. Since I'm naive and I've
never had particular performance concerns, not had I to parse huge
files, when I had to, I was content with XML::Simple. YMMV.
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
------------------------------
Date: Thu, 05 Apr 2007 16:00:50 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: perl hash - creation by side effect?
Message-Id: <750a1310nma42l9do37rl6f5bu9k2bkak5@4ax.com>
On Thu, 05 Apr 2007 11:30:06 +0100, bugbear
<bugbear@trim_papermule.co.uk_trim> wrote:
>my $j = $s->{f}->{i};
>print Dumper($s->{f});
>
>The output is:
>$VAR1 = undef;
>$VAR1 = {};
>
>It appears that the assignment to $j has a side
>effect of creating a 'f' member of $s; this f has
>a value of reference to an empty hash.
What would have you expected? Anyway it's called autovivification and
has nothinf to do with "the assignment to $j". It's depends only on
your dereferencing like a hash.
>Advice, references etc welcome as usual.
http://www.perlarchive.com/___TLC/7026.shtml
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 307
**************************************