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

Discussion in 'Perl' started by rtm, Sep 27, 2004.

  1. rtm

    rtm Guest

    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',
    });
    $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);
    }
    }
     
    rtm, Sep 27, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TPS
    Replies:
    6
    Views:
    355
    Steven Cheng[MSFT]
    Feb 28, 2004
  2. Steve W
    Replies:
    4
    Views:
    555
    Steven Cheng[MSFT]
    Dec 23, 2004
  3. Steve W
    Replies:
    2
    Views:
    998
    Steven Cheng[MSFT]
    Dec 22, 2004
  4. Java and Swing
    Replies:
    1
    Views:
    706
    Chris Lambacher
    Oct 24, 2005
  5. Tim Golden
    Replies:
    0
    Views:
    462
    Tim Golden
    Oct 21, 2005
Loading...

Share This Page