Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^5: forking and monitoring processes

by revdiablo (Prior)
on Jan 08, 2005 at 21:23 UTC ( [id://420594]=note: print w/replies, xml ) Need Help??


in reply to Re^4: forking and monitoring processes
in thread forking and monitoring processes

There are a few problems here. The first is that by iterating on <@proc>, you're using glob to iterate on an array. It works in this case, but should not be used for that purpose. Instead, you should simply use:

for (@proc) {

The next, more critical problem, is in this code:

my $cmd = "dir $_:"; my $pid = open my $fh, "-|", $cmd # ...

As the open docs state, this form of open does not pass the command through the shell, but rather executes it directly. The shell is where word splitting (e.g. "dir C:" to "dir", "C:") normally happens, but that won't happen here. That means you are attempting to execute a program called "dir C:". As you can imagine, you probably do not have that program on your system.

Most of the time, the solution is to do something like this:

my $pid = open my $fh, "-|", "dir", "$_:" # ...

But in this case, you probably don't have a program named "dir" either. I might be wrong, but I think dir is built in to Win32's shell, cmd.exe. So you have two options. Either you can continue to use the same form of open, and call the shell yourself, or use the form of open that executes commands through the shell anyway. In this case, I would probably opt for the latter, for simplicity:

my $pid = open my $fh, "dir $_: |" # ...

The last problem in your code is my fault. It's a bug I fixed, but apparently you grabbed the code before I did so. This line:

kill 15, values %pids;

Should be changed to:

kill 15, map {$_->[0]} values %pids;

PS: If reading files in certain directories is your end goal, you might be able to accomplish it more easily with Perl builtins, such as readdir or glob, instead of shelling out to dir. But hopefully you will have learned something in the process. :-)

Replies are listed 'Best First'.
Re^6: forking and monitoring processes
by Anonymous Monk on Jan 09, 2005 at 06:11 UTC
    Hello,

    I have been trying to figure out why it is not entering the while loop. It seems to be correct since the file handles are added to the select function prior to the can_read. Just wondering what you thought. Thanks.

Re^6: forking and monitoring processes
by Anonymous Monk on Jan 08, 2005 at 21:49 UTC
    Hello revdiablo,

    Thanks for the reply. That helped understand so many things better. I changed all the things you suggested, but it still seems that the program does not enter the while loop. I even changed the hash code as you suggested, but that did not seem to be the problem. What do you advice?

    Thanks.

    use IO::Select; my %pids; my $s = IO::Select->new(); my @proc = (qw(C W)); for ( @proc ) { my $pid = open my $fh, "dir $_: |" or warn "Could not fork&open $cmd: $!" and next; print "opened $_ \n\n"; $pids{$fh} = [ $pid, $_ ]; $s->add($fh); } while (my @ready = $s->can_read) { print "entered while loop \n\n"; for my $fh (@ready) { if (eof $fh) { delete $pids{$fh}; $s->remove($fh); next; } my $line = <$fh>; if ($line =~ /Windows/) { chomp $line; print "Found 'Windows' in '$line' from $pids{$fh}[1]\n"; kill 15, map { $_->[0]} values %pids; } } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://420594]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2025-06-19 20:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.