[29961] in Perl-Users-Digest
Perl-Users Digest, Issue: 1204 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jan 16 18:09:43 2008
Date: Wed, 16 Jan 2008 15:09:07 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 16 Jan 2008 Volume: 11 Number: 1204
Today's topics:
Re: Chat client/server print failed <smallpond@juno.com>
Re: Chat client/server print failed <zentara@highstream.net>
Re: In perl 5.10, is $needle ~~ @haystack binary search <kkeller-usenet@wombat.san-francisco.ca.us>
Re: In perl 5.10, is $needle ~~ @haystack binary search <g_m@remove-comcast.net>
Re: In perl 5.10, is $needle ~~ @haystack binary search <brian.d.foy@gmail.com>
Re: In perl 5.10, is $needle ~~ @haystack binary search <abigail@abigail.be>
incorrect errno/perror with IO::socket->new <brandon.mayfield@att.net>
Re: incorrect errno/perror with IO::socket->new xhoster@gmail.com
Re: Shrink large file according to REG_EXP <uri@stemsystems.com>
Re: Shrink large file according to REG_EXP <simon.chao@fmr.com>
Re: Shrink large file according to REG_EXP <uri@stemsystems.com>
Re: Shrink large file according to REG_EXP <simon.chao@fmr.com>
Re: Shrink large file according to REG_EXP <mgjv@tradingpost.com.au>
Re: Wait for background processes to complete <pgodfrin@gmail.com>
Re: Wait for background processes to complete <pgodfrin@gmail.com>
Re: Wait for background processes to complete xhoster@gmail.com
Re: Wait for background processes to complete <pgodfrin@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 16 Jan 2008 11:39:48 -0800 (PST)
From: smallpond <smallpond@juno.com>
Subject: Re: Chat client/server print failed
Message-Id: <b0c34ebf-ada2-4a4f-85b3-cd831681e2e6@l1g2000hsa.googlegroups.com>
On Jan 16, 1:02 pm, Ted Zlatanov <t...@lifelogs.com> wrote:
> On Wed, 16 Jan 2008 08:14:09 -0800 (PST) smallpond <smallp...@juno.com> wrote:
>
> s> This is the shortest example you could do to show the problem?
>
> ...and you had to quote all 700+ lines of it? I'd rather see
> top-quoting than this.
>
> Ted
My apologies. The Google browser client hides quoted text and I
sometimes
forget to trim replies.
--S
------------------------------
Date: Wed, 16 Jan 2008 20:48:18 GMT
From: zentara <zentara@highstream.net>
Subject: Re: Chat client/server print failed
Message-Id: <s6rso3ph1u8nugm9es0j2n5oo2m6j3fopf@4ax.com>
On Tue, 15 Jan 2008 16:32:52 -0800 (PST), deadpickle
<deadpickle@gmail.com> wrote:
>This is a chat client wrote in perl Gtk2. THe problem that I am
>running into is that when you type and click send I get a "print() on
>closed filehandle GEN0 at chat-client.pl line 332" error. This error
>is the print statement in the send_msg_all sub. I cant figure out how
>the file handle is closed and am wondering if anyone can see why. I'll
>leave the server running for testing purposes.
I'm sorry to say, that this complex set of scripts is a PITA to deal
with.
After fixing the many wordwrap problems, and host mismatches,
I got to see your problem.
It's too complex for me to see how to fix it, without alot of work.
Simply put, you need a bi-directional client so the client can
function properly. The way it is setup, your $conn only works
for the first connection, then is closed. So you are printing to a
closed socket. You need a bi-directional client, OR some loop
that keeps the client alive switching between send and recv mode.
Something like
while($select->can_read){
do stuff with the socket
}
Google for select loop examples.
Try starting your server, and testing it with this bi-directional
client. It dosn't follow your original connection protocol, but
it stays alive.
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
#use IO::Select;
use IO::Socket;
require Tk::ROText;
# create the socket
my $host = shift || 'localhost';
my $port = 6666;
my $socket = IO::Socket::INET->new(
PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp',
);
defined $socket or die "ERROR: Can't connect to port $port on $host:
$!\n";
print STDERR "Connected to server ...\n";
my $mw = new MainWindow;
my $log = $mw->Scrolled(qw/ROText -scrollbars ose/)->pack;
my $txt = $mw->Entry()->pack(qw/-fill x -pady 5/);
$mw ->bind('<Any-Enter>' => sub { $txt->Tk::focus });
$txt->bind('<Return>' => [\&broadcast, $socket]);
$mw ->fileevent($socket, readable => sub {
my $line = <$socket>;
unless (defined $line) {
$mw->fileevent($socket => readable => '');
return;
}
$log->insert(end => $line);
});
MainLoop;
sub broadcast {
my ($ent, $sock) = @_;
my $text = $ent->get;
$ent->delete(qw/0 end/);
print $sock $text, "\n";
}
__END__
zentara
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
------------------------------
Date: Wed, 16 Jan 2008 11:23:31 -0800
From: Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us>
Subject: Re: In perl 5.10, is $needle ~~ @haystack binary search?
Message-Id: <j62265xaj1.ln2@goaway.wombat.san-francisco.ca.us>
On 2008-01-16, nolo contendere <simon.chao@fmr.com> wrote:
> On Jan 16, 1:19Â pm, Jim Cochrane <allergic-to-s...@no-spam-
> allowed.org> wrote:
>>
>> Sorry for interrupting and pardon my ignorance, but what is the ~~
>> operator? Â I don't see it in 'perldoc perlop' (for perl 5.8.6). Â And I
>> cannot figure out how to search for "~~" in google.
>
> This is for Perl 5.10.
>
> Check out Equality Operators:
> "smart match"
> http://perldoc.perl.org/perlop.html
Or try
http://perldoc.perl.org/perlsyn.html#Smart-matching-in-detail
which has the full chart which Joost referenced.
--keith
--
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information
------------------------------
Date: Wed, 16 Jan 2008 14:48:42 -0500
From: "~greg" <g_m@remove-comcast.net>
Subject: Re: In perl 5.10, is $needle ~~ @haystack binary search?
Message-Id: <C9mdnWSIaNodwBPanZ2dnUVZ_hOdnZ2d@comcast.com>
> O (N) where N is the size of @haystack, with the assumption that
> comparing two scalar can be done in constant time.
>
> This is optimal.
>
>
> But does this help you? $needle ~~ @haystack might first sleep for a
> week or so, and you wouldn't notice that in "big-O".
>
>
> Abigail
> --
right, thanks.
Best case 1, avg case N/2, worst case N.
(I'm mainly interested in how it scales, so the constant factor doesn't matter.)
~~
I think I know now what I was thinking last night!
Everything about perl 5.10 seems faster.
(You don't have to measure it. You feel it.)
So I was thinking - there must be nothing it doesn't do better.
A linear search - in better than linear time?
Sure - why not.
:)
~greg
------------------------------
Date: Wed, 16 Jan 2008 16:27:00 -0600
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: In perl 5.10, is $needle ~~ @haystack binary search?
Message-Id: <160120081627008140%brian.d.foy@gmail.com>
In article <13os50l4a053gb9@corp.supernews.com>, bugbear
<bugbear@trim_papermule.co.uk_trim> wrote:
> Joost Diepenmaat wrote:
> > "~greg" <g_m@remove-comcast.net> writes:
> >
> >> thanks,
> >> ~greg
> >
> > No. It can't be, since @haystack is not guaranteed to be sorted.
> >
> > perlop says:
> >
> > for $a ~~ $b and $b ~~ $a:
> >
> > $a $b Type of Match Implied Matching Code
> > ====== ===== ===================== =============
> > Array Num array contains number grep $_ == $b, @$a
> > Array Any array contains string grep $_ eq $b, @$a
>
> Given that the equivalent code looks fine, I fail to see the need
> for ~~ at all?
>
> BugBear
You're only looking at two of the cases. The smart match handles many
cases, so it handles those as well. The "matching code" isn't
nessicarily the code it actually runs (read the notes after that table
in perlop).
Additionally, smart matching appears to be faster:
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2007-12/msg00644.
html
------------------------------
Date: 16 Jan 2008 22:45:34 GMT
From: Abigail <abigail@abigail.be>
Subject: Re: In perl 5.10, is $needle ~~ @haystack binary search?
Message-Id: <slrnfot28d.t74.abigail@alexandra.abigail.be>
_
~greg (g_m@remove-comcast.net) wrote on VCCLI September MCMXCIII in
<URL:news:C9mdnWSIaNodwBPanZ2dnUVZ_hOdnZ2d@comcast.com>:
!!
!! > O (N) where N is the size of @haystack, with the assumption that
!! > comparing two scalar can be done in constant time.
!! >
!! > This is optimal.
!! >
!! >
!! > But does this help you? $needle ~~ @haystack might first sleep for a
!! > week or so, and you wouldn't notice that in "big-O".
!! >
!! >
!! > Abigail
!! > --
!!
!! right, thanks.
!!
!! Best case 1, avg case N/2, worst case N.
Why do you think the average case in N/2? Sure, if all your matches
actually succeed, and the thing you are looking for occurs only once
in your array, and can appear on any index with equal probability,
the average case is N/2. But then, if all your matches always succeed
there's no point in asking whether there's a match. The less likely
your match succeeds, the more your average will go towards N. The more
the thing you're looking for appears in the array (as duplicates), the
more your average will go towards 1.
!! (I'm mainly interested in how it scales, so the constant factor doesn't matter.)
!! ~~
!!
!! I think I know now what I was thinking last night!
!!
!! Everything about perl 5.10 seems faster.
!! (You don't have to measure it. You feel it.)
!!
!! So I was thinking - there must be nothing it doesn't do better.
!!
!! A linear search - in better than linear time?
!! Sure - why not.
Cannot be done without preprocessing.
Abigail
--
perl -we '$| = 1; $_ = "Just another Perl Hacker\n"; print
substr $_ => 0, 1 => "" while $_ && sleep 1 => 1'
------------------------------
Date: Wed, 16 Jan 2008 13:28:16 -0800 (PST)
From: brandon <brandon.mayfield@att.net>
Subject: incorrect errno/perror with IO::socket->new
Message-Id: <d3b7320c-30dc-4ac4-a552-7d7049a10d9d@1g2000hsl.googlegroups.com>
I have been having fits getting meaningful errno/perror values out of
Perl from Socket->new() and I am hoping someone here might know what
is going on. The errno/perror values seem to work fine on my Linux
servers but AIX, HP, and Sun are not giving me a correct failure
value. I have remote servers set up so that I can get various types of
failures (no daemon, firewall blocking, no route, etc.) and I can test
all these conditions with telnet to see what the perror string should
be, and my Linux servers match that.
It should be just a matter of getting extern int errno in the
interpreter when I ask for it with $!, no?
Here is my sample code :
------------------------------------------------
#!/usr/bin/perl
use IO::Socket;
use strict;
my($server_ip, $server_port) = ("172.16.18.96", "10");
my($timeout) = 5;
my($firsterr) = "";
my($socket);
print "\$! = $!\n";
$! = 0;
print "\$! = $!\n";
$socket = IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => $server_ip,
PeerPort => $server_port,
Timeout => $timeout
);
if ( $firsterr eq "" ) { $firsterr = $!; }
print "\$! = $!\n";
if ( $socket ) {
close $socket;
print " OK : Port $server_port is OPEN\n";
else {
# Other error detected
print " Use OS problem determination procedures to determine
the \n";
print " source of the perror (errno) string : \n";
print " \t $firsterr\n";
}
exit 0;
----------------------
In this particular case (IP/port) on my subnet, this server exists
and has no process listening on the port so I fully expect to get
(some OS specific form of) a perror string : "connection refused".
This is what I get on all the OS's when I telnet to this server and
port, and is also what I get from this script on Linux. For the other
OS's I am getting errno/perror values that apparently do not have
anything to do with the fact there is no listening process :
OS / Ver perl -v result
RH WS2u1 v5.6.1 "Connection refused"
RH WS3u5 v5.8.0 "Connection refused"
RH AS4u3 v5.8.5 "Connection refused"
AIX 5.3 v5.8.0 "A system call received a parameter that is
not valid."
AIX 5.3 v5.8.2 "A system call received a parameter that is not
valid."
AIX 5.1 v5.6.0 "A system call received aparameter that is not
valid."
AIX 5.2 v5.8.0 "A system call received aparameter that is not
valid."
AIX 4.3 v5.005_03 "A file descriptor does not refer to
an open file."
AIX 4.3 v5.005_03 "A file descriptor does not refer to
an open file."
HP 11.11 v5.8.0 "Invalid argument"
HP 11.00 v4.0 <problem w/ use>
HP 10.20 v4.0 <problem w/ use>
Sun 5.7 v5.8.5 "Connection refused"
Sun 5.8 v5.005_03 "Bad file number"
Sun 5.9 v5.6.1 "Connection refused"
Sun 5.10 v5.8.4 "Connection refused"
If I change nothing other than the IP and port values in the script
(to something that will allow a connection), I get a successful socket
creation. This is just an example of the script I am developing, the
original also writes to and reads from the socket, and that is working
splendidly on all the test boxes. I need to use the errorno/perror to
point the user towards what needs to be checked on the host, client,
or network if there is a failure however.
Also, I have also noticed similar behavior opening ports sucessfully.
The above server had telnet running and I can open port 23
successfully but I get various inexplicable $! errors after the
new().
Thanks, Brandon
------------------------------
Date: 16 Jan 2008 22:00:37 GMT
From: xhoster@gmail.com
Subject: Re: incorrect errno/perror with IO::socket->new
Message-Id: <20080116170040.136$2w@newsreader.com>
brandon <brandon.mayfield@att.net> wrote:
> I have been having fits getting meaningful errno/perror values out of
> Perl from Socket->new() and I am hoping someone here might know what
> is going on. The errno/perror values seem to work fine on my Linux
> servers but AIX, HP, and Sun are not giving me a correct failure
> value. I have remote servers set up so that I can get various types of
> failures (no daemon, firewall blocking, no route, etc.) and I can test
> all these conditions with telnet to see what the perror string should
> be, and my Linux servers match that.
After the "primary" error, IO::Socket might make more system calls
before it returns to your code. Those other system calls might set errno,
thus overwriting the previous value of $! set by the "primary" error.
Because of this, IO::Socket tries to set $@ to be the most meaningful error
message it can manage, considering the mishmash of system calls that are
going on behind the scenes. You should look in $@ instead of or in
addition to $!. (I'm saying this will solve the problem, only that it is
the first thing I'd try.)
...
>
> Also, I have also noticed similar behavior opening ports sucessfully.
> The above server had telnet running and I can open port 23
> successfully but I get various inexplicable $! errors after the
> new().
After a successful system call, $! contains whatever it contained
*before* that system call. Looking in $! is meaningful only after
an unsuccessful system call.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
------------------------------
Date: Wed, 16 Jan 2008 19:17:02 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Shrink large file according to REG_EXP
Message-Id: <x763xt1sy8.fsf@mail.sysarch.com>
>>>>> "nc" == nolo contendere <simon.chao@fmr.com> writes:
nc> check out /REGEX/o
obsolete and probably useless.
nc> and qr/REGEX/
we still haven't seen his code so that is not a solution. more likely
his loops are clunky and slow and his regexes are worse.
nc> ...also, if you keep a history of which filters get used the most,
nc> stick those at the top. this will speed up the file processing if the
nc> trend does not change. may want to do this periodically in case it
nc> does change.
or which are the slowest regexes and speed those up. there are too many
ways to optimize unknown code. let's see if the OP will actually post
some data and code.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Architecture, Development, Training, Support, Code Review ------
----------- Search or Offer Perl Jobs ----- http://jobs.perl.org ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Wed, 16 Jan 2008 11:22:37 -0800 (PST)
From: nolo contendere <simon.chao@fmr.com>
Subject: Re: Shrink large file according to REG_EXP
Message-Id: <410f5482-ad07-460e-838f-e0d2c9a4fe72@s19g2000prg.googlegroups.com>
On Jan 16, 2:17=A0pm, Uri Guttman <u...@stemsystems.com> wrote:
> >>>>> "nc" =3D=3D nolo contendere <simon.c...@fmr.com> writes:
>
> =A0 nc> check out /REGEX/o
>
> obsolete and probably useless.
>
really? is this since 5.10?
> =A0 nc> and qr/REGEX/
>
> we still haven't seen his code so that is not a solution. more likely
> his loops are clunky and slow and his regexes are worse.
>
> =A0 nc> ...also, if you keep a history of which filters get used the most,=
> =A0 nc> stick those at the top. this will speed up the file processing if =
the
> =A0 nc> trend does not change. may want to do this periodically in case it=
> =A0 nc> does change.
>
> or which are the slowest regexes and speed those up. there are too many
> ways to optimize unknown code. let's see if the OP will actually post
> some data and code.
>
yeah, Xho already suggested the speed-up-the-slowest-regex solution,
so I was going for something different. you're right though, code +
data would help enormously.
------------------------------
Date: Wed, 16 Jan 2008 19:54:32 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Shrink large file according to REG_EXP
Message-Id: <x7y7apzgug.fsf@mail.sysarch.com>
>>>>> "nc" == nolo contendere <simon.chao@fmr.com> writes:
nc> On Jan 16, 2:17 pm, Uri Guttman <u...@stemsystems.com> wrote:
>> >>>>> "nc" == nolo contendere <simon.c...@fmr.com> writes:
>>
>> nc> check out /REGEX/o
>>
>> obsolete and probably useless.
>>
nc> really? is this since 5.10?
since at least when qr// came in. also dynamic regexes (those with
interpolation) are not recompiled unless some variable in them
changes. this is what /o was all about in the early days of perl5. so
it's purpose of not recompiling has been moot for eons. and qr// even
makes it even more useless.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Architecture, Development, Training, Support, Code Review ------
----------- Search or Offer Perl Jobs ----- http://jobs.perl.org ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Wed, 16 Jan 2008 12:00:28 -0800 (PST)
From: nolo contendere <simon.chao@fmr.com>
Subject: Re: Shrink large file according to REG_EXP
Message-Id: <28bd0a8f-98c7-4e27-8dab-6d6d060fc72b@s13g2000prd.googlegroups.com>
On Jan 16, 2:54=A0pm, Uri Guttman <u...@stemsystems.com> wrote:
> >>>>> "nc" =3D=3D nolo contendere <simon.c...@fmr.com> writes:
>
> =A0 nc> On Jan 16, 2:17=A0pm, Uri Guttman <u...@stemsystems.com> wrote:
> =A0 >> >>>>> "nc" =3D=3D nolo contendere <simon.c...@fmr.com> writes:
> =A0 >>
> =A0 >> =A0 nc> check out /REGEX/o
> =A0 >>
> =A0 >> obsolete and probably useless.
> =A0 >>
>
> =A0 nc> really? is this since 5.10?
>
> since at least when qr// came in. also dynamic regexes (those with
> interpolation) are not recompiled unless some variable in them
> changes. this is what /o was all about in the early days of perl5. so
> it's purpose of not recompiling has been moot for eons. and qr// even
> makes it even more useless.
Ok, thanks for the info.
------------------------------
Date: Thu, 17 Jan 2008 07:43:55 +1100
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: Shrink large file according to REG_EXP
Message-Id: <slrnfosr4b.rpv.mgjv@martien.heliotrope.home>
On Wed, 16 Jan 2008 10:02:20 -0800,
Jim Gibson <jimsgibson@gmail.com> wrote:
> In article
><ab9782ce-07b5-4841-84e2-88cff0dee2b5@v67g2000hse.googlegroups.com>,
> thellper <thellper@gmail.com> wrote:
>
>> The problem is that this solution is slow.
>> I'm now reading line by line the whole file, and then I'm applying the
>> reg_exp... but it is very slow.
>> I've noticed that the time to read and write the file without doing
>> anything is very small, so I'm loosing a lot of time for my
>> reg_exps... .
> If your program is I/O bound, then it might be faster to work on
> different parts simultaneously.
If the process is I/O bound, then it's unlikely that it'll speed up if
you work on multiple parts simultaneously, unles you can guarantee that
those multiple parts are going to come from a different part of your I/O
subsystem, i.e. ones that don't compete with each other for resources.
Given that it's one single file as input, it's very unlikely that you'll
be able to pick your parts to work on in such a way that you avoid I/O
contention.
You might see some improvement if you're lucky, but you could also see a
marked decrease in total I/O speed, if you're unlucky.
Splitting a process in multiple worker processes generally only is
better if each worker process can then utilise a piece of hardware that
wasn't used before, like another I/O system, or another CPU.
> However, you are going to suffer some
> head thrashing as your multiple processes attempt to read different
> parts of the same file at the same time.
Indeed, at least, if your file is on a single disk. If it's on a RAID
system, the O/S might be able to avoid contention on disks. Or not. For
linear access patterns you generally do get some improvement.
> If your program is cpu bound, then splitting up the work won't help
> unless you are using a multi-processor system.
Indeed.
But CPU bound processes can benefit from algorithm improvements, or even
small tweaks to code if that code is in a place that gets executed a
lot.
Profiling would be able to identify that.
> If, as you say, reading the file without doing any processing is quick
> enough, then it is the processing of the data that is the bottleneck.
Agree :) It also is really the only bit which is likely to be
Perl-specific. All the previous is not.
Martien
--
|
Martien Verbruggen | The Second Law of Thermodenial: In any closed
| mind the quantity of ignorance remains
| constant or increases.
------------------------------
Date: Wed, 16 Jan 2008 13:00:14 -0800 (PST)
From: pgodfrin <pgodfrin@gmail.com>
Subject: Re: Wait for background processes to complete
Message-Id: <f89eece1-9256-4505-ae74-7b09e990012d@i72g2000hsd.googlegroups.com>
On Jan 15, 5:49 pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth pgodfrin <pgodf...@gmail.com>:
>
> > On Jan 14, 2:48 pm, xhos...@gmail.com wrote:
> > > pgodfrin <pgodf...@gmail.com> wrote:
> > > > On Jan 14, 11:11 am, xhos...@gmail.com wrote:
>
> > > > > $ perl -wle 'use strict; fork or exec "sleep " . $_*3 foreach 1..3 ; \
> > > > > my $x; do {$x=wait; print $x} until $x==-1'
>
> <snippage>
>
> > It seems to me that firing off say 5 processes with the '&' character
> > to send them to the background is 5 parallel processes, while
> > executing them off one at a time is sequential. Your code (fork or
> > exec "sleep" ... ) waits for each sleep process to complete - so that
> > is what I meant by "waiting sequentially".
>
> No, I think you don't understand how fork works. It *is* rather
> confusing until you're used to it. Read the documentation again (both
> Perl's and your system's), or find a good Unix programming book.
>
> > Technically speaking you're
> > right - but the idea is to have tasks run in parallel versus
> > sequentially, which is ostensibly faster.
>
> The tasks do run in parallel with Xho's version.
>
>
>
> > To make a long story short, this is how I solved the problem. It seems
> > that the PIDs of the commands run via system() and the '&' background
> > thing end up belonging to the same Process Group - seen in the ps
> > command, plus a little extra:
>
> <snip>
> > So I wrap the ps command and do some looping:
>
> > for (;;)
> > {
> > open PGRP, "ps -C cp h |" ;
>
> Use lexical filehandles and three-or-more-arg open.
> Check the return value.
>
> open my $PGRP, '-|', 'ps', '-C', 'cp', 'h'
> or die "couldn't fork ps: $!";
>
> > @pidlist=<PGRP> ;
> > if ($#pidlist<0) {die "\nNo more processes\n" ;}
>
> IMHO any use of $#ary is an error; certainly in this case you should be
> using @pidlist instead.
>
> @pidlist or die "No more processes\n";
>
> This will run around in a tight loop running probably hundreds of ps
> processes per second. This is not a effective use of your system's
> resources, to say the least. If you must poll like this you need a sleep
> in there somewhere to limit the damage.
>
> > }
>
> > It's not pretty but it works...
>
> Yuck. The whole point of the wait syscall is to avoid nastiness like
> that. I suggest you learn how it works, or use a module written by
> someone who does (you have been given at least two suggestions so far),
> or stick to shell.
>
> > But, I believe this is an architectural FLAW with Perl.
>
> No. Firstly, the only flaw here is in your understanding; secondly, if
> there was a flaw it would be in Unix, not Perl, since Perl just exposes
> the underlying system interfaces.
>
> Ben
darn... still can't make it wait!
pg
------------------------------
Date: Wed, 16 Jan 2008 13:46:44 -0800 (PST)
From: pgodfrin <pgodfrin@gmail.com>
Subject: Re: Wait for background processes to complete
Message-Id: <d28202c0-efb1-40a5-9e1e-80349e23f5ac@x69g2000hsx.googlegroups.com>
Hi Ben,
Well - it seems I have made some progress.But - I still need some
advice...
Here's my code:
#!/usr/bin/perl
use POSIX ":sys_wait_h";
$SIG{CHLD} = sub { wait }; # an 'installed' signal handler
$f=0 ;
while (</fausb/sample/*.txt>)
{
$f += 1;
if ($pid = fork)
{
print "Fork $f pid: $pid\n" ;
print "Copying $_ ($pid)\n";
# exec() NEVER returns...
exec("cp $_ $_.old") ;
} elsif (defined $pid)
{
print "Found child...($pid)\n" ;
} elsif ($! =~ /No more process/)
{
print "Fork returned no more processes\n";
} else
{
die "Fork error.\n";
} # end fork
} # end while
print "\n<<<<< End of exercise >>>>>\n";
exit;
So - this code ends up starting 6 forks, and then falls out of the
while(<*.txt>) loop - printing the 'End of exercise' message. A 'ps -
ef |grep fk' (fktest5 is the name of this program) shows it's no
longer running, but the cp command indeed are still running.
What I would like to do is wait for the cp processes to finish before
executing the exit statement.
Any ideas?
thanks,
pg
------------------------------
Date: 16 Jan 2008 22:17:40 GMT
From: xhoster@gmail.com
Subject: Re: Wait for background processes to complete
Message-Id: <20080116171742.765$cZ@newsreader.com>
pgodfrin <pgodfrin@gmail.com> wrote:
> Hi Ben,
> Well - it seems I have made some progress.But - I still need some
> advice...
>
> Here's my code:
>
> #!/usr/bin/perl
> use POSIX ":sys_wait_h";
> $SIG{CHLD} = sub { wait }; # an 'installed' signal handler
You probably don't need a signal handler. And if you want it,
you should aware that that signal can be fired even when there is
no child ready to be waited for, leading to blocking in the wait.
So you would probably want to do a nonblocking waitpid instead.
>
> $f=0 ;
> while (</fausb/sample/*.txt>)
> {
> $f += 1;
> if ($pid = fork)
> {
> print "Fork $f pid: $pid\n" ;
> print "Copying $_ ($pid)\n";
> # exec() NEVER returns...
> exec("cp $_ $_.old") ;
> } elsif (defined $pid)
> {
> print "Found child...($pid)\n" ;
> } elsif ($! =~ /No more process/)
> {
> print "Fork returned no more processes\n";
> } else
> {
> die "Fork error.\n";
> } # end fork
> } # end while
> print "\n<<<<< End of exercise >>>>>\n";
## On Linux, wait returns -1 when there are no living children to wait for.
1 until -1==wait();
> exit;
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
------------------------------
Date: Wed, 16 Jan 2008 14:29:16 -0800 (PST)
From: pgodfrin <pgodfrin@gmail.com>
Subject: Re: Wait for background processes to complete
Message-Id: <74d5d8cb-b16b-476a-9116-71cc0d0362ac@e23g2000prf.googlegroups.com>
On Jan 16, 4:17 pm, xhos...@gmail.com wrote:
> pgodfrin <pgodf...@gmail.com> wrote:
> > Hi Ben,
> > Well - it seems I have made some progress.But - I still need some
> > advice...
>
> > Here's my code:
>
> > #!/usr/bin/perl
> > use POSIX ":sys_wait_h";
> > $SIG{CHLD} = sub { wait }; # an 'installed' signal handler
>
> You probably don't need a signal handler. And if you want it,
> you should aware that that signal can be fired even when there is
> no child ready to be waited for, leading to blocking in the wait.
> So you would probably want to do a nonblocking waitpid instead.
>
>
>
>
>
> > $f=0 ;
> > while (</fausb/sample/*.txt>)
> > {
> > $f += 1;
> > if ($pid = fork)
> > {
> > print "Fork $f pid: $pid\n" ;
> > print "Copying $_ ($pid)\n";
> > # exec() NEVER returns...
> > exec("cp $_ $_.old") ;
> > } elsif (defined $pid)
> > {
> > print "Found child...($pid)\n" ;
> > } elsif ($! =~ /No more process/)
> > {
> > print "Fork returned no more processes\n";
> > } else
> > {
> > die "Fork error.\n";
> > } # end fork
> > } # end while
> > print "\n<<<<< End of exercise >>>>>\n";
>
> ## On Linux, wait returns -1 when there are no living children to wait for.
> 1 until -1==wait();
>
> > exit;
>
> Xho
>
> --
> --------------------http://NewsReader.Com/--------------------
> The costs of publication of this article were defrayed in part by the
> payment of page charges. This article must therefore be hereby marked
> advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
> this fact.
Thanks Xho - I've removed the signal handler, but it seems wait always
returns -1 so - the loop is a nop? Where in the code should it go?
thanks,
pg
------------------------------
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 1204
***************************************