(Win32) Timing out a process while reading process' output?

From: rtm (mclay_at_cfdlab.ae.utexas.edu)
Date: 09/28/04


Date: Tue, 28 Sep 2004 16:54:58 GMT

I am interested in running a process with a timeout. Also I'm
interested in analyzing the output of this process.

Under Unix, the solution is described clearly in the Perl Cookbook
"16.10: Communicating between related processes" and 16.24 "Timing
out an Operation". Enclosed below is an example showing what I want
to do under Unix.

I need to do this under Windows XP. As others have pointed out
"alarm" works under 5.8+ and fork sorta works under 5.8+ under
windows. But the unix example code below just hangs.

So the best thing I have found is Win32::Job.

------------------------------------------------------------------------
#!/usr/bin/env perl
# -*- cperl -*-
BEGIN { $^W = 1; }
use strict;

use Win32::Job;
my $job = Win32::Job->new();

my $r = $job->spawn("z:\t\junk.exe", "junk", {
                                              stdin => 'NUL', # the NUL device
                                              stdout => 'stdout.log',
                                              stderr => 'stdout.log',
                                             });
$job->run(20);
------------------------------------------------------------------------

The only problem is that I can't see how you read the output while the
process is running when using Win32::Job. Of course one can write the
output to a file then read it back in after the process is finished.

Is there another way? Is there another Win32:: something that does
what I want?

Thanks

------------------------------------------------------------------------

#!/usr/bin/env perl
# -*- cperl -*-
BEGIN { $^W = 1; }
use strict;

use IO::Handle;

pipe(READER, WRITER);

WRITER->autoflush(1);

$SIG{CHLD} = 'IGNORE';
my $pid;
eval
  {
    local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
    alarm 20;

    die "Can't fork: $!" unless defined($pid = fork);
    if ($pid)
      {
        # parent
        close WRITER;
        while(<READER>)
          {
            print "Got: $_";
          }
      }
    else
      {
        close READER;
        open(STDOUT, ">&WRITER");
        exec("junk","","") or die("exec $!");
      }
    alarm 0;
    waitpid($pid,0);
  };

if ($@)
  {
    die unless $@ eq "alarm\n"; # propagate unexpected errors
    print "Tripped Alarm\n";
    kill 9, $pid;
  }

------------------------------------------------------------------------
#include <stdio.h>
#include <math.h>

int main()
{
  int i,j,n;
  double x,y, dx;
  FILE* fp = fopen("junk.log","w");

  n = 1000000;
  dx = 0.00001;
  x = 0.0;

  for (j = 0; j < 50; j++)
    {
      for (i = 0; i < n; i++)
        {
          y = sin(x);
          x += dx;
        }
      fprintf(fp,"j: %d\n", j);
      fflush(fp);
      printf("j: %d\n", j);
    }
}



Relevant Pages

  • c++ conversion files
    ... unix to windows and windows to unix ... prompts for a destination file to save the new formatted file. ... int uw; ...
    (comp.lang.cpp)
  • Re: c++ conversion files
    ... >KALIO80 Wrote ... >>unix to windows and windows to unix ... > int uw; ...
    (comp.lang.cpp)
  • Problems with compilation
    ... under both Windows & UNIX. ... gc.c: In function `int main': ... gc.c:81: warning: assignment to `clock_t' from `double' ...
    (comp.programming)
  • Re: c++ conversion files
    ... >unix to windows and windows to unix ... but I removed the troublesome char strings ... int uw; ...
    (comp.lang.cpp)
  • Re: PNews update
    ... use strict; ... This is explained in the Exporter documentation, ... our @EXPORT_OK = (@}, qw(coninit title attr cls ... This program is probably more geared toward people with some Unix ...
    (news.software.readers)