Re: How do I end processes that time out?

From: J. Romano (jl_post_at_hotmail.com)
Date: 07/23/04


Date: 22 Jul 2004 23:13:00 -0700


> On 2004-07-19, J. Romano <jl_post@hotmail.com> wrote:
> >
> > I finally got it to work by not killing the child's $pid
> > (process id), but $pid + 1. For some reason, the pid
> > assigned to the child process by the OS was one more than
> > what was returned from the open() statement. Would anyone
> > know why?

Ilmari Karonen <usenet@vyznev.invalid> replied in message
news:<slrncfo8pm.14p.usenet@yhteiskone.vyznev.net>...
>
> Presumably because perl hands the command to a shell for parsing. The
> $pid you get is for the shell, not for the actual command executed.
> For some reason, killing the shell has different effects on different
> platforms.
>
> (The reason why killing $pid+1 appears to work is simply that process
> id numbers are often assigned sequentially, so the pid of the actual
> command often just happens to be one greater than that of the shell.)

   Your explanation makes sense, Ilmari. So I would think that
killing $pid+1 will usually work in my case, but won't work 100% of
the time (and therefore should not be relied on).

> You can avoid this by using the list form of open(). Simply replace
>
> "perl -e '$tinyProgram'"
>
> in your example code with
>
> "perl", "-e", $tinyProgram
>
> and everything should work as expected.

   Oddly enough, that doesn't work for me. When I use the line:

      my $pid = open(CHILD, '-|', 'perl', '-e', $tinyProgram);

I get the following fatal error message at run-time:

      Can't use an undefined value as filehandle reference ...

This happens even with the following line:

      my $pid = open(CHILD, '-|', 'ls', '-l');

but doesn't happen when I use the one-argument form of "ls", like
this:

      my $pid = open(CHILD, '-|', 'ls');

   I can't figure out why it's giving me an error message, because
according to "perldoc -f open", the following four lines should be
more or less equivalent:

      open(FOO, "cat -n '$file'|");
      open(FOO, '-|', "cat -n '$file'");
      open(FOO, '-|') || exec 'cat', '-n', $file;
      open(FOO, '-|', "cat", '-n', $file);

so if the line:

      my $pid = open(CHILD, '-|', "perl -e' $tinyProgram");

works, likewise the line:

      my $pid = open(CHILD, '-|', 'perl', '-e', $tinyProgram);

should also work, just as you said it would. But it doesn't. I keep
getting the fatal run-time error message that I can't use an undefined
value as a filehandle reference when I use the list form of open().
Am I missing something?

(Just so you know, this same error happens on two separate Unix
machines. Their "perl -v" output is:

   This is perl, v5.6.1 built for alpha-netbsd
   This is perl, v5.6.1 built for i386-linux
)

   I did get around this error, however. I used the third form of the
open() statements listed above (the one with the exec() call).
Therefore, I replaced the line:

      my $pid = open(CHILD, '-|', "perl -e' $tinyProgram");

with:

      my $pid = open(CHILD, '-|') || exec 'perl', '-e', $tinyProgram;

and everything worked correctly (meaning that I also received the
correct $pid used to kill the process).

   So in the end, I solved my problem of killing child processes that
time out, but now I don't understand why the list form of open() keeps
giving me a fatal error message. If anyone knows why, please let me
know.

   Anyway, thanks for all your help. I appreaciate it.

   -- Jean-Luc



Relevant Pages

  • Re: What is wrong with this simple command?
    ... more than the two commands and the error message to guess at the ... When you execute a OS shell commmand (like, ... route as it is a very expensive method for executing shell commands. ... believe the error messages and start looking for an enormous bug. ...
    (comp.lang.tcl)
  • RE: pam_acct_mgmt(): user account has expired (was Re: LoginProblem)
    ... That could indeed be, since the fields have a shell on the end, nothing ... I enter my normal login and then my password. ... >> The last error message will repeat with the number getting ... > within the questions and newbies mailing lists produced no leads for ...
    (freebsd-questions)
  • Re: dont know how to make
    ... The error message really means it can't find the library, ... You're getting the error because SKIPBUILD evaluates to true, ... and that .lib isn't built. ... But still i'm not able to see the windows shell. ...
    (microsoft.public.windowsce.embedded)
  • Re: Media Center Exrender
    ... I don't have the linksys brand, ... the HP version (I think they are identical though looking at the shell ... have reinstalled and tried switiching from my wireless A to a wireless ... and just cycles through the menu and then error message, ...
    (microsoft.public.windows.mediacenter)
  • Loopback with Device::SerialPort?
    ... on a Linux machine correctly by connecting two serial ports on the ... The problem is that when the child process in the example below tries ... what could cause this sort of error message anywhere in the ...
    (comp.lang.perl.misc)