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 | |
|
Re^6: forking and monitoring processes
by Anonymous Monk on Jan 08, 2005 at 21:49 UTC |