By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,183 Members | 1,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,183 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 when run on XP.

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',

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?


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

use IO::Handle;



my $pid;
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;
print "Got: $_";
close READER;
open(STDOUT, ">&WRITER");
exec("junk","","") or die("exec $!");
alarm 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);
printf("j: %d\n", j);

Jul 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.