[23517] in Perl-Users-Digest
Perl-Users Digest, Issue: 5725 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Oct 29 14:11:08 2003
Date: Wed, 29 Oct 2003 11:05:14 -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, 29 Oct 2003 Volume: 10 Number: 5725
Today's topics:
Re: [OT] DOS window. <usenet@morrow.me.uk>
Re: [OT] DOS window. <spikeywan@bigfoot.com.delete.this.bit>
Re: [OT] DOS window. <usenet@morrow.me.uk>
array reference expected (bnapus)
calculate an average <jaddo@yahoo.com.>
Re: Call Perl Scripts from Other Dir in Linux, Path Mes <no@no.no>
Re: Call Perl Scripts from Other Dir in Linux, Path Mes <jurgenex@hotmail.com>
Re: Can't do setuid and file permision denied errors <abigail@abigail.nl>
Re: Checkbox - database checkbox, if checked gives val (randy)
Re: Conditional look-ahead? <usenet@morrow.me.uk>
Re: DHCP client tool <jwillmore@remove.adelphia.net>
Re: DOS window. <spikeywan@bigfoot.com.delete.this.bit>
Re: DOS window. <pasdespam_desmond@zeouane.org>
Re: DOS window. (Malcolm Dew-Jones)
fork process to handle fifo input (Ole)
Re: fork process to handle fifo input <usenet@morrow.me.uk>
Re: fork process to handle fifo input <nobull@mail.com>
Re: fork process to handle fifo input <nobull@mail.com>
Re: How to generate this list? (Mark Jason Dominus)
Re: How to generate this list? <usenet@morrow.me.uk>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 29 Oct 2003 16:07:10 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: [OT] DOS window.
Message-Id: <bnooje$hr4$2@wisteria.csv.warwick.ac.uk>
Desmond Coughlan <pasdespam_desmond@zeouane.org> wrote:
> The above might not make sense, but in short, if you run 'command.com' on a
> Windows 98 machine, you're getting rid of the GUI (after a fashion), and
> accessing what is running _behind_ it, whereas 'cmd.exe' on Windows 2000
> _spawns_ a shell _from_ the OS.
>
> Erm ... did the above make sense ?
Not entirely... :)
It's also wrong. You are running an instance of command.com quite
separate from that which underlies Win98. If Windows supported proper
process trees, it would look like this:
command.com
|
\ win.com
|
\ WINOLDAP, whatever that is
|
\ command.com
Ben
--
If you put all the prophets, | You'd have so much more reason
Mystics and saints | Than ever was born
In one room together, | Out of all of the conflicts of time.
ben@morrow.me.uk |----------------+---------------| The Levellers, 'Believers'
------------------------------
Date: Wed, 29 Oct 2003 16:04:37 -0000
From: "Richard S Beckett" <spikeywan@bigfoot.com.delete.this.bit>
Subject: Re: [OT] DOS window.
Message-Id: <bnoohf$oar$1@newshost.mot.com>
> I would probably install MSYS from http://mingw.org/msys.shtml.
>
> You realise this has nothing to do with Perl... :)
What do you mean it has nothing to do with perl!!?
What else would you use a DOS window for? ;-)
--
R.
GPLRank +79.699
------------------------------
Date: Wed, 29 Oct 2003 16:47:56 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: [OT] DOS window.
Message-Id: <bnoqvs$j8o$1@wisteria.csv.warwick.ac.uk>
"Richard S Beckett" <spikeywan@bigfoot.com.delete.this.bit> wrote:
> > I would probably install MSYS from http://mingw.org/msys.shtml.
> >
> > You realise this has nothing to do with Perl... :)
>
> What do you mean it has nothing to do with perl!!?
>
> What else would you use a DOS window for? ;-)
I believe the correct response at this point is either 'fdisk c:/' or
'loadlin', but that's another question entirely... ;)
Ben
--
Like all men in Babylon I have been a proconsul; like all, a slave ... During
one lunar year, I have been declared invisible; I shrieked and was not heard,
I stole my bread and was not decapitated.
~ ben@morrow.me.uk ~ Jorge Luis Borges, 'The Babylon Lottery'
------------------------------
Date: 29 Oct 2003 10:50:50 -0800
From: bnapus@yahoo.com (bnapus)
Subject: array reference expected
Message-Id: <f9310ac.0310291050.57f38688@posting.google.com>
Perlers -
I want to assign a value to an annonymous hash:
$hash->[$i][$j]{Score} = 1;
Why won't it accept a scalar? I get an error about perl expecting an
address to an array. What I envisioned was a hash within a 2D array.
I ended up using this:
$hash->[$i]{$j}{Score} = 1;
and it does what I want it to - just curious.
Josh
------------------------------
Date: Thu, 30 Oct 2003 05:57:32 -0800
From: "Jack" <jaddo@yahoo.com.>
Subject: calculate an average
Message-Id: <3fa00e3e@dnews.tpgi.com.au>
I need to calculate an average of every last-3-values.
my ($av, $add, @A);
Foreach my $item ( @data ) {
If ( $#A == 2) {
$add = o;
shift @A;
push @A, $item
foreach my $j ( @A ) { $add += $j };
} else {
push @A, $item;
if ( $#A == 2 ) {
foreach my $j ( @A ) { $add += $j };
}
}
$av = $add/3;
I feel there is a better way or faster code than this
thanks for comments
------------------------------
Date: Wed, 29 Oct 2003 18:06:43 GMT
From: "Public Interest" <no@no.no>
Subject: Re: Call Perl Scripts from Other Dir in Linux, Path Messed Up
Message-Id: <TkTnb.198640$0v4.15463763@bgtnsc04-news.ops.worldnet.att.net>
> You want to put
> chdir '/perlfiles';
> or so at the start of your Perl script.
>
Another problem is Perl Script under Shell and under CGI use different dir
systems. How can I make sure that both will work properly?
/ under shell means root dir
/ under cgi or http means http://domain.com/
sometimes. cgi use a cgi-bin dir which is virtual so domain.com/cgi-bin and
domain.com/htmlfiles are not under the same root but rather
/www/html/htmlfiles/ and www/cgi-bin.
the only thing that works in both is ./ ../
What I think of is still use relative path for portabilty of scripts under
shell and web server. I want to add something to change pwd enviroment, but
not sure if it is possible. PWD is not accessable under CGI. CGI has an env
as script_path or scrip_url.
----
Thank you guys for letting me know I should put ./ for executable shell
scripts...
------------------------------
Date: Wed, 29 Oct 2003 18:21:26 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Call Perl Scripts from Other Dir in Linux, Path Messed Up
Message-Id: <GyTnb.4714$Q9.3071@nwrddc02.gnilink.net>
Public Interest wrote:
> Another problem is Perl Script under Shell and under CGI use
> different dir systems. How can I make sure that both will work
> properly?
>
> / under shell means root dir
> / under cgi or http means http://domain.com/
Dear public
That is BS. You are confusing URLs (or URIs) with file names resp. directory
pathes.
The one has nothing to do with the other and it is only for convenience that
many web servers try to interpret a URL path as a directory path of a file
system. However it is really up to the web server how to interpret any URL
and what to return in each case. In fact very often the "target HTML file"
doesn't even exist but is created on the fly by some script or pulled out of
a database or publishing system or a gazillion of other sources.
jue
------------------------------
Date: 29 Oct 2003 17:18:39 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Can't do setuid and file permision denied errors
Message-Id: <slrnbpvtjf.fk5.abigail@alexandra.abigail.nl>
Chris (chrisw@lu.csi.com.ph) wrote on MMMDCCXI September MCMXCIII in
<URL:news:14142d1f.0310281705.661c55be@posting.google.com>:
\\
\\ I'm a newbie with perl scripts
\\
\\ I have a chpass.pl which is being executed by a change password web
\\ utility page.
And what makes you think this is a good idea? Would you let your
kids drive in NASCAR races right after they learned how to use
how to steer?
Changing passwords via a web interface is a dumb thing to do.
Changing the passwords by directly opening /etc/shadow is dangerous.
Doing that with a program written in a language you just started
to use sounds like a bloody stupid idea to me.
On the boxes I've adminned, there has only be one way for a user
to set a new password: appear in person, and if needed with an ID.
Abigail
--
perl -MLWP::UserAgent -MHTML::TreeBuilder -MHTML::FormatText -wle'print +(
HTML::FormatText -> new -> format (HTML::TreeBuilder -> new -> parse (
LWP::UserAgent -> new -> request (HTTP::Request -> new ("GET",
"http://work.ucsd.edu:5141/cgi-bin/http_webster?isindex=perl")) -> content))
=~ /(.*\))[-\s]+Addition/s) [0]'
------------------------------
Date: 29 Oct 2003 10:11:42 -0800
From: searsdvdtech@yahoo.com (randy)
Subject: Re: Checkbox - database checkbox, if checked gives value of 1 .. how to sum ?
Message-Id: <506a5f6b.0310291011.65db9865@posting.google.com>
This is a Perl 5 Based Database . if you can ... Please go to
www.baseportal.com, and you can setup your own perl 5 based database
or create a page based in Perl 5.
So with that out of the way.... I created a database with a field
named "comp"
it is a checkbox type of field, i can not put it any other terms .
Also the field
has two default values: if checked = 1, if no check 0. That much works
good .
it displays in a page , as a colum and if checked has value of 1 in
the record that checked. Now at the bottom of that colum i would like
the total of the field
"comp".. to display the sum of all the records that have been checked.
I hope this helps... Thanks for all your help.
see page at http://baseportal.com/cgi-bin/baseportal.pl?htx=/searsdvdtech/testforperl.
Has anyone looked at this Page? It might help you to understand the
structure of the output of the database.
Thanks Again..
Randy M.
------------------------------
Date: Wed, 29 Oct 2003 16:09:37 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Conditional look-ahead?
Message-Id: <bnooo1$hr4$3@wisteria.csv.warwick.ac.uk>
Brian Harnish <bharnish@technologist.com> wrote:
> -----BEGIN xxx SIGNED MESSAGE-----
Not wishing to cause trouble or anything, but is it *really* necessary
to sign a message which is itself shorter than the signature?
Ben
--
The cosmos, at best, is like a rubbish heap scattered at random.
- Heraclitus
ben@morrow.me.uk
------------------------------
Date: Wed, 29 Oct 2003 16:19:30 GMT
From: James Willmore <jwillmore@remove.adelphia.net>
Subject: Re: DHCP client tool
Message-Id: <20031029111929.200f7de8.jwillmore@remove.adelphia.net>
On 29 Oct 2003 05:32:35 -0800
pelohh@compuserve.de (Peter Lohmeier) wrote:
> want to test DHCP responses ...
>
> Do you mean, it is possible to send a DHCP request with Active State
> Perl under W2k ??
Not sure. Try using 'ppm' to 'search' for a DHCP module.
>
> And if so, would it be possible to set a special MAC Address also ??
>
Huh? That's a very difficult task to perform. The MAC address is a
hardware address. So, you _could_ use different NIC's to change the
MAC address - but that's not practical :-)
I'm thinking that you _could_ do it in Perl, but it's a very difficult
task.
What is it you're trying to test?
--
Jim
Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.
a fortune quote ...
The light at the end of the tunnel is the headlight of an
<approaching train.
------------------------------
Date: Wed, 29 Oct 2003 16:06:24 -0000
From: "Richard S Beckett" <spikeywan@bigfoot.com.delete.this.bit>
Subject: Re: DOS window.
Message-Id: <bnookq$oht$1@newshost.mot.com>
> I _think_ that it'll fail, for the simple reason that Windows 98 is a GUI
> sitting on top of a shell, and Win 2000 is a native 32-bit OS.
>
> The above might not make sense, but in short, if you run 'command.com' on
a
> Windows 98 machine, you're getting rid of the GUI (after a fashion), and
> accessing what is running _behind_ it, whereas 'cmd.exe' on Windows 2000
> _spawns_ a shell _from_ the OS.
>
> Erm ... did the above make sense ?
Not at all! ;-)
--
R.
GPLRank +79.699
------------------------------
Date: Wed, 29 Oct 2003 17:21:16 +0100
From: Desmond Coughlan <pasdespam_desmond@zeouane.org>
Subject: Re: DOS window.
Message-Id: <s06571-0f42.ln1@zeouane.org>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
Richard S Beckett <spikeywan@bigfoot.com.delete.this.bit> wrote ...
>> I _think_ that it'll fail, for the simple reason that Windows 98 is a GUI
>> sitting on top of a shell, and Win 2000 is a native 32-bit OS.
>>
>> The above might not make sense, but in short, if you run 'command.com' on
> a
>> Windows 98 machine, you're getting rid of the GUI (after a fashion), and
>> accessing what is running _behind_ it, whereas 'cmd.exe' on Windows 2000
>> _spawns_ a shell _from_ the OS.
>>
>> Erm ... did the above make sense ?
> Not at all! ;-)
Ignore it, anyway: I was apparently wrong. :-\
- --
Desmond Coughlan |desmond [at] zeouane [dot] org
http://www.zeouane.org/
-----BEGIN xxx SIGNATURE-----
Version: PGPfreeware 5.0i for non-commercial use
Charset: noconv
iQA/AwUBP5/a62iD+5zjSqyTEQJsQACbBNF/FiUNyOEMnUgTucMEXS4fXVIAoNNw
HoZcvC7hjw52pk5ymKCRnLKe
=6eTr
-----END PGP SIGNATURE-----
------------------------------
Date: 29 Oct 2003 09:09:09 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: DOS window.
Message-Id: <3f9ff435@news.victoria.tc.ca>
Richard S Beckett (spikeywan@bigfoot.com.delete.this.bit) wrote:
: Guys,
: I've been writing perl scripts at work on a w2k machine, but as I'm now
: doing a personal project, I thought I'd better do it at home.
: For various reasons, I'm still running w98 at home, and have discovered that
: the w98 dos window is particularly crap, as I can't specify a buffer size.
: If I put cmd.exe from my w2k machine onto the w98 machine, will it run OK,
no
: and give me the dos window I'm used to, or is this a bad idea?
fine idea, it just won't work
: Are there any better ways around this?
Might I suggest linux? You don't have to reformat your hard drive if you
get an install that uses the dos format.
You know of course that you can control the number of lines in the
window, I think you can get at least 55 lines, and I know for 100%
certainty you can get 43 cause that's ega mode.
People used to use emacs as a programming front end to solve the
limitations of terminals, so perhaps emacs could do the same for you.
I wonder if any of the various command.com replacements (4dos is one
example) provide their own improvements on the dos box window.
Otherwise, pipe all output into temporary files and use your favourite
editor to examine the output.
I often use notepad as a "scratch pad" for my input on windows. I format
more complex commands in a notepad window (always saved in a file called
notes.txt in the directory of the project) and then cut and paste the
commands to run them. The file ends up being a good document of my test
cases, so I gain something even though the use of the file is gludgy.
$0.2
------------------------------
Date: 29 Oct 2003 08:56:13 -0800
From: oviaudmurat@web.de (Ole)
Subject: fork process to handle fifo input
Message-Id: <c640408c.0310290856.63cffcf1@posting.google.com>
hi everybody,
i've got a little problem with a program i'm working on. That program
has the purpose to collect log entries generated by iptables through
syslog-ng. I have redirected the logs coming from iptables to a
named pipe (or fifo) ( "/dev/ipt_fifo" ). The program then forks
processes that
must collect the log entries coming through the fifo and prepare them
to
be saved for further analysis.
Now i have the following code:
( the program forks 5 processes that work on the input of the named
pipe.
after having collected a certain amount of information, the
processes save
the data to a file. )
##########################################################################
#!/usr/bin/perl -w
use POSIX;
use strict;
my $pid;
my $childId;
my $child_pids = [ ];
our $zombies = 0;
##########################################################################
for ( my $num = 0; $num < 5; $num++ ) {
if (( $pid = fork ( ) ) > 0 ) { # parent process
push ( @$child_pids, $pid );
}elsif ( $pid == 0 ) {
my $FIFO;
my $i = 1;
my $reports = [];
my $my_pid = getpid ( ) ||die "Cannot getpid ( ) : $!";
while ( 1 ) {
open ( $FIFO, "</dev/ipt_fifo" );
while ( <$FIFO> ) {
# analyze log data... ( left here ) #
push( @$reports, $_ );
print $my_pid . " : " . $i . "--Size of \$reports\t:\t" .
scalar( @$reports ) . "\n";
if (( $i % 1024 ) == 0 ) {
saveReport ( $reports,$my_pid );
undef ( $reports );
print "OK\n";
$i = 0;
}
$i++;
}
close ( $FIFO );
}
}
}
wait_zombies ( $pid );
sub wait_zombies {
my $pid = $_[ 0 ];
if ( $pid > 0 ) {
my $chId;
do {
$chId = waitpid( -1, 0 );
print "Caught child no $chId\n";
}until ( $chId == - 1 );
}
}
sub saveReport {
my $reports = $_[ 0 ];
my $my_pid = $_[ 1 ];
open ( FH, ">>log_fork_childno_$my_pid.log" );
print "saving reports [PID = $my_pid]";
foreach ( @$reports ) {
print ".";
print FH $_;
}
close ( FH );
}
##################################################################
Now i have 2 problems:
1.) How do I trap e.g. a "kill -9" command, directed to the parent
process
in order to collect to child processes before quiting. Now the
child
processes get init for as parent.
2. ) I recognize a strange behaviour: for a while, the processes
collect
data from syslog through the fifo as they should. But then, it
seems
that syslog is generating approx 200 entires per second. But i
think
that it has to do with process synchronization.
Questions:
1. ) Is it possible that, if two processes try to read simultaneously
from
the named pipe or fifo, they lock themselves somehow ?
2. ) How can manage it to have only one child process trying to read
from
the fifo ?
I would be very thankful if someone might help me out here.
I need this filter daemon, because it is the topic of the final
examination
of my training.
Thanks in advance for your interprocesscommunication!
Greetings Ole Viaud-Murat.
------------------------------
Date: Wed, 29 Oct 2003 17:35:01 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: fork process to handle fifo input
Message-Id: <bnoto5$k9a$1@wisteria.csv.warwick.ac.uk>
oviaudmurat@web.de (Ole) wrote:
> hi everybody,
> Now i have the following code: ( the program forks 5 processes that
> work on the input of the named pipe. after having collected a
> certain amount of information, the processes save the data to a
> file. )
I have to confess that I don't really understand the description
above; however, I shall offer what help I can...
> #!/usr/bin/perl -w
> use POSIX;
> use strict;
You want
use subs qw/wait_zombies saveReport/;
here. You alse want to choose one of under_scores and studlyCaps and
stick to it.
> my $pid;
> my $childId;
> my $child_pids = [ ];
> our $zombies = 0;
Why 'our'? The only reason is if you need to get a this var from
outside this file... as far I I can see, you have only one file.
You want
$\ = "\n";
here: for why see below where I deal with your print()s.
> ##########################################################################
>
> for ( my $num = 0; $num < 5; $num++ ) {
This is C. Perl would be:
for my $num (0..5) {
> if (( $pid = fork ( ) ) > 0 ) { # parent process
Where do you check if the fork failed?
Better:
$pid = fork;
defined $pid or die "can't fork: $!";
if($pid) { # parent
> push ( @$child_pids, $pid );
> }elsif ( $pid == 0 ) {
...in which case this can just be
} else { # kid
> my $FIFO;
> my $i = 1;
> my $reports = [];
> my $my_pid = getpid ( ) ||die "Cannot getpid ( ) :
> $!";
It is clearer to use 'or' rather than ||, as then you do not need the
() for getpid.
The current process's pid is available in $$: see perldoc perlvar.
> while ( 1 ) {
Where do you exit this loop?
> open ( $FIFO, "</dev/ipt_fifo" );
No need for the brackets.
As Tad would say :) always, yes *always* check the return of open.
open $FIFO, "</dev/ipt_fifo" or die "can't open fifo: $!";
> while ( <$FIFO> ) {
> # analyze log data... ( left here ) #
> push( @$reports, $_ );
> print $my_pid . " : " . $i . "--Size of \$reports\t:\t" .
> scalar( @$reports ) . "\n";
print "$$ : $i--Size of \$reports\t:\t" . @$reports;
No need for \n as you set $\ above: see perldoc perlvar.
> if (( $i % 1024 ) == 0 ) {
$i will never be greater that 1024: no need for modulus.
if($i == 1024) {
> saveReport ( $reports,$my_pid );
> undef ( $reports );
> print "OK\n";
> $i = 0;
> }
> $i++;
> }
> close ( $FIFO );
close can fail as well.
close $FIFO or die "colsing fifo failed: $!";
> }
> }
> }
>
> wait_zombies ( $pid );
$pid holds the pid of the last child you forked. Why are you passing
only this?
> sub wait_zombies {
> my $pid = $_[ 0 ];
my $pid = shift;
> if ( $pid > 0 ) {
Is this an attempt to make up for not checking the return value of
fork()? Otherwise it is pointless...
> my $chId;
> do {
> $chId = waitpid( -1, 0 );
> print "Caught child no $chId\n";
> }until ( $chId == - 1 );
> }
> }
>
>
>
> sub saveReport {
> my $reports = $_[ 0 ];
> my $my_pid = $_[ 1 ];
my $reports = shift;
my $my_pid = shift;
or
my ($reports, $my_pid) = @_;
according to taste.
> open ( FH, ">>log_fork_childno_$my_pid.log" );
open FH, ">>log_fork_childno_${my_pid}.log"
or die "can't open log no. $my_pid: $!";
> print "saving reports [PID = $my_pid]";
> foreach ( @$reports ) {
/Please/ try and be consistent. Do you spell it 'for' or 'foreach'?
> print ".";
> print FH $_;
> }
> close ( FH );
... or die "can't close log $my_pid: $!";
> }
> ##################################################################
>
> Now i have 2 problems:
> 1.) How do I trap e.g. a "kill -9" command, directed to the parent
> process in order to collect to child processes before quiting. Now
> the child processes get init for as parent.
You can't. That's the point of SIGKILL: it can't be trapped. If you
want to trap other signals, look at %SIG in perlvar.
> 2. ) I recognize a strange behaviour: for a while, the processes
> collect data from syslog through the fifo as they should. But then,
> it seems that syslog is generating approx 200 entires per
> second. But i think that it has to do with process synchronization.
I'm afraid I don't understand what you mean... but you could be right:
you certainly have sync problems.
> Questions:
>
> 1. ) Is it possible that, if two processes try to read
> simultaneously from the named pipe or fifo, they lock themselves
> somehow ?
Each byte fed into the pipe comes out exactly once, to exactly one of
the processes reading it. Absolutely no guarantees about which. It is
generally a bad idea to have more than one process on either end of a
fifo.
> 2. ) How can manage it to have only one child process trying to read
> from the fifo ?
a. Use a lockfile to synchronise access.
b. Have a 'reader' process that reads data from the fifo and passes it
to the other children for processing.
c. Don't fork()... :)
> I would be very thankful if someone might help me out here. I need
> this filter daemon, because it is the topic of the final examination
> of my training.
Well, you must examine your own conscience about that... I would
suggest you need to at least read the question a bit more carefully to
find out what is *actually* required, in particular, what these
multiple child processes are supposed to do.
Ben
--
Like all men in Babylon I have been a proconsul; like all, a slave ... During
one lunar year, I have been declared invisible; I shrieked and was not heard,
I stole my bread and was not decapitated.
~ ben@morrow.me.uk ~ Jorge Luis Borges, 'The Babylon Lottery'
------------------------------
Date: 29 Oct 2003 17:46:24 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: fork process to handle fifo input
Message-Id: <u9smlcszrj.fsf@wcl-l.bham.ac.uk>
I'm more than once since there are a number of distinct issues here.
oviaudmurat@web.de (Ole) writes:
>
> 1.) How do I trap e.g. a "kill -9" command, directed to the parent
> process
You can't trap "kill -9" that's the whole point of the "-9"!
If you want to give a process a chance to clean up after itself then
you don't send it SIGKILL, you send it SIGINT or SIGTERM or SIGQUIT.
This, of course, has nothing particular to do with Perl.
You can trap trappable signals using %SIG.
> Now the child processes get init for as parent.
Your children can poll their parent pid to detect this or you can have
a FIFO running from the children to the parent so that they get a
SIGPIPE when the parent dies.
This, of course, has nothing particular to do with Perl.
> 2. ) I recognize a strange behaviour: for a while, the processes
> collect data from syslog through the fifo as they should. But then,
> it seems that syslog is generating approx 200 entires per
> second. But i think that it has to do with process synchronization.
>
> Questions:
>
> 1. ) Is it possible that, if two processes try to read
> simultaneously from the named pipe or fifo, they lock themselves
> somehow ?
Unlikely. At worst they'll get garbled data, but if the writing
application is not splitting messages accross multiple write()
syscalls the this shouldn't happen.
This, of course, has nothing particular to do with Perl.
> 2. ) How can manage it to have only one child process trying to read
> from the fifo ?
A semaphore. (e.g. a lock on a file, or even the FIFO iself in your
OS allows that.).
This, of course, has nothing particular to do with Perl.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: 29 Oct 2003 18:21:12 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: fork process to handle fifo input
Message-Id: <u9oevzucpz.fsf@wcl-l.bham.ac.uk>
oviaudmurat@web.de (Ole) writes:
> #!/usr/bin/perl -w
> use POSIX;
> use strict;
Unless you need backward compatabiliy you should probably "use
warnings" rather than have a -w in the shebang. But I know you don't
need backward compatabiliy as you are using a feature that postdates
the warnings pragma elsewhere in your script.
> my $pid;
> my $childId;
> my $child_pids = [ ];
> our $zombies = 0;
Always declare all variables as lexcally scoped in the smallest
applicable scope unless there is a positive reason to do otherwise.
This keeps you code a lot tighter. This does not only apply in Perl.
Do not think of declarations as "a way to keep strict quiet" and shove
them all up the top. Appart from introducing some bugs it also means
you find yourself declaring variables that are no longer used.
> for ( my $num = 0; $num < 5; $num++ ) {
In Perl that is more ideomatically written as
for my $num ( 0 .. 4 ) {
Or, since you never use $num, you may want to let for() use it's
default iterator
for ( 1 .. 5 ) {
> if (( $pid = fork ( ) ) > 0 ) { # parent process
> push ( @$child_pids, $pid );
> }elsif ( $pid == 0 ) {
In Perl (unlike C) fork() does not return a negative number on
failure.
To see that it does return on failure: perldoc -f fork()
> my $FIFO;
> my $i = 1;
> my $reports = [];
> my $my_pid = getpid ( ) ||die "Cannot getpid ( ) : $!";
> while ( 1 ) {
> open ( $FIFO, "</dev/ipt_fifo" );
You should always check open() succeded. If you really think it's not
gonna fail so can't be bothered with any elegant recovery code the
very least you should do is die. BTW: now would be a good time to
have declared $FIFO.
open ( my $FIFO, "</dev/ipt_fifo" ) or die $!;
> while ( <$FIFO> ) {
> # analyze log data... ( left here ) #
> push( @$reports, $_ );
> print $my_pid . " : " . $i . "--Size of \$reports\t:\t" .
> scalar( @$reports ) . "\n";
> if (( $i % 1024 ) == 0 ) {
Since $i can never be >1024 that is more simply written as
if ( $i == 1024 ) {
> saveReport ( $reports,$my_pid );
> undef ( $reports );
Earlier you initialised $reports to [], now you reinitialize to undef.
Either will do but it's neater to be consistant.
> print "OK\n";
> $i = 0;
> }
> $i++;
> }
Don't you want to call saveReport here too to save the tail?
> close ( $FIFO );
If you hadn't delared $FIFO in too wide a scope in the first place you
wouldn't have needed the explicit close() because $FIFO would go out
of scope here anyhow.
> }
> }
You have forgotten the final else block on your if .. elsif .. else to
handle the 3 possible return states from fork().
> }
>
> wait_zombies ( $pid );
Why are you passing $pid here? What it the 5th fork failed?
I think you mean:
wait_zombies() if @$child_pids;
Or maybe just
wait_zombies();
> sub wait_zombies {
> my $pid = $_[ 0 ];
> if ( $pid > 0 ) {
> my $chId;
> do {
> $chId = waitpid( -1, 0 );
> print "Caught child no $chId\n";
> }until ( $chId == - 1 );
> }
> }
As a general rule it's wastefull to wrap a loop inside an "if"
constuct that simply tests for a condition under which the loop would
exit immediately anyhow.
sub wait_zombies {
while ( (my $chId = waitpid( -1, 0 )) != -1) {
print "Caught child no $chId\n";
}
}
> sub saveReport {
> my $reports = $_[ 0 ];
> my $my_pid = $_[ 1 ];
Fetching sutroutinne arguments more ideomatically written as:
my $reports = shift;
my $my_pid = shift;
Or:
my ($reports,$my_pid) = @_;
> open ( FH, ">>log_fork_childno_$my_pid.log" );
Hey! You knew about using lexically scoped file handles above but now
you've forgotten.
open ( my $FH, ">>log_fork_childno_$my_pid.log" ) or die $!;
> print "saving reports [PID = $my_pid]";
> foreach ( @$reports ) {
> print ".";
> print FH $_;
> }
> close ( FH );
If you'd uses a lexically scoped file handle you wouldn't need the
explicit close.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Wed, 29 Oct 2003 16:20:25 +0000 (UTC)
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: How to generate this list?
Message-Id: <bnopc9$722$1@plover.com>
In article <bnjig5$4lj$1@reader1.panix.com>,
David Combs <dkcombs@panix.com> wrote:
>>$ perl -le'print for glob "{AB,CD}XY"'
>>ABXY
>>CDXY
>>$ perl -le'print for glob "{A,B}{C,D}XY"'
>>ACXY
>>ADXY
>>BCXY
>>BDXY
>>
>>So it passes a string of all the alternatives to glob which creates a
>>list of the alternatives.
>
>Yes, the clever curly-bracket notation from csh -- forms
>the cartesian product.
>
>Also works if they're *nested*.
>
>Now this, for John, Abigail, MJD, etc:
>
> "YOUR TASK, MR. PHELPS, IS TO PROGRAM THAT CSH ALGORITHM"
>
>How about an efficient algorithm on doing that?
Funny you should mention that, because I was using that as a basic
example of a 'Cartesian product' in chapter VII of my book
(http://perl.plover.com/book/chap07.html) so I was planning to write
that anyway. In fact, I thought I had written it already, but if I
did I can't find it. I think the delay was occasioned by the fact
that I thought of two or three different ways to do it, but I wasn't
sure which one I liked best.
I do have analogous code which takes a regular expression and
generates a list of all the strings that will match the regex. This
includes your problem as a sub-case, because the regex might be
/^(A|B)(C|D)XY$/
which is equivalent to the "{A,B}{C,D}XY" example you have above, only
with different notation.
Note that when each pair of curly braces contains only a finite number
of alternatives, the problem is quite easy, because you can generate
the result with what is esentially a set of nested 'for' loops. For
example, when the expression to expand is
foo{x,y,z}{1,2,3}.txt
you want to execute some code that is equivalent to
for $a ('x', 'y', 'z') {
for $b (1, 2, 3) {
# do something with "foo$a$b.txt"
}
}
The problem becomes much more interesting when when one or more of the
sets of alternatives mught be infinite. In that case the 'nested for
loop' approach no longer works. Suppose the expression to expand is
foo{a,b,c,...,z,aa,ab,ac,...}{1,2,3,...}.txt
Then the analogous 'for' loops are
for $a ('a', 'b', 'c', ..., 'z', 'aa', 'ab', ...) {
for $b (1, 2, 3, ...) {
# do something with "foo$a$b.txt"
}
}
but, because the inner loop is infinite, the program never gets to the
second iteration of the outer loop, and so only produces strings that
begin with "fooa". So you need a different (and more complicated)
strategy if any of the alternatives might contain infinite sets.
Anyway, the problem in the finite case is not that hard, and I am sure
you would be able to solve it if you put your mind to do it. I would
suggest that you first try to solve the problem for the case where the
string contains only one curly-brace expression. That will give you
code that takes a string and eliminates a single set of curly braces
from it, yielding a list of strings. Once you do that, it's easy to
deal with many sets of curly braces: just call your one-curly-brace
handler repeatedly to eliminate one set of curly braces after another.
------------------------------
Date: Wed, 29 Oct 2003 16:52:30 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: How to generate this list?
Message-Id: <bnor8e$j8o$2@wisteria.csv.warwick.ac.uk>
mjd@plover.com (Mark Jason Dominus) wrote:
> The problem becomes much more interesting when when one or more of the
> sets of alternatives mught be infinite. In that case the 'nested for
> loop' approach no longer works. Suppose the expression to expand is
>
> foo{a,b,c,...,z,aa,ab,ac,...}{1,2,3,...}.txt
>
> Then the analogous 'for' loops are
>
> for $a ('a', 'b', 'c', ..., 'z', 'aa', 'ab', ...) {
> for $b (1, 2, 3, ...) {
> # do something with "foo$a$b.txt"
> }
> }
>
> but, because the inner loop is infinite, the program never gets to the
> second iteration of the outer loop, and so only produces strings that
> begin with "fooa". So you need a different (and more complicated)
> strategy if any of the alternatives might contain infinite sets.
This is of course equivalent to the problem of enumerating the
rationals, which was solved IIRC by Cantor... :)
Ben
--
"If a book is worth reading when you are six, * ben@morrow.me.uk
it is worth reading when you are sixty." - C.S.Lewis
------------------------------
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.
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 V10 Issue 5725
***************************************