Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^6: SOLUTION: Read STDOUT from child process and kill process tree

by Dirk80 (Pilgrim)
on Mar 12, 2012 at 16:40 UTC ( #959141=note: print w/replies, xml ) Need Help??

in reply to Re^5: Read STDOUT from Win32::Job while process is running
in thread Read STDOUT from Win32::Job while process is running

Thanks again to all of you helping me so much.

Here I want to post the solution of my original problem.

#!/usr/bin/perl use strict; use threads; use Thread::Queue; use Win32::JobAdd; ## A shared var to communicate progess between work thread and TK my $Q = new Thread::Queue; my $job:shared = createJobObject( 'counter_and_calc_job' ); sub work{ my $pid = open PROC, q[perl -le "$|=1; system 1, 'calc.exe'; print and select(undef,und +ef,undef,0.1) for 1 .. 1000" |] or die $!; assignProcessToJobObject( $job, $pid ); while( <PROC> ) { $Q->enqueue( $_ ); } close PROC; } threads->new( \&work )->detach; ## For lowest memory consumption require (not use) ## Tk::* after you've started the work thread. require Tk::ProgressBar; my $mw = MainWindow->new; my $pb = $mw->ProgressBar()->pack(); my $button = $mw->Button(-text => 'CANCEL', -command => sub { closeHandle( $job ) } )->pa +ck(); my $repeat; $repeat = $mw->repeat( 100 => sub { while( $Q->pending ) { my $progress = $Q->dequeue; return unless $progress; $repeat->cancel if $progress == 100; $pb->value( $progress ) } }); $mw->MainLoop;

This code solves my two problems. Being able to read from a child process, but also being able to kill the whole process tree instead of only killing the child process and to have zombie processes.

If you press the "Cancel" Button then the calculator and the child process which counts from 1 to 1000 are killed. Even if you quit the gui with the cross in the right upper corner the grandchild process "calculator" is killed.

Without a job environment in a Win32 environment the grandchild calculator would stay as a zombie process.

Feedback to this code is welcome.

Cheers, Dirk

  • Comment on Re^6: SOLUTION: Read STDOUT from child process and kill process tree
  • Download Code

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://959141]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2023-03-21 07:19 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (59 votes). Check out past polls.