Ohad has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a perl code that runs a ssh process that will run forever(in a loop) until it is killed. I do it by a fork and then the child process is using "open" to run the ssh, then the child process waits for the open's output and prints it on the screen, I want to print it on a canvas I opened in the original parent process.

Problem is, it's not working, i made sure that the child process has the right reference to the text widget but for some reason, the text is not displayed on the canvas.

Code:
my $TextS=$BB5_frame->Scrolled("Text", -wrap=>'none')->pack(); $BT5_frame->Button(-text => "RUN", -command=> sub { $c_pid = fork(); if (defined($c_pid) and $c_pid == 0) { $SIG{CHLD}='IGNORE'; my $flag =0; $T_pid = open(Out ,"-|" ,"/usr/bin/ssh","-x",$Ap_host,@Pre); $SIG{TERM} = sub {kill ('TERM',$T_pid); close(Out); }; while (<Out>) { $TextS->insert('end', $_); } } })->pack(-side=>'left');
When I print the lines to STDOUT it's working.

Replies are listed 'Best First'.
Re: Piping output from child to parent Tk widget
by bluescreen (Friar) on Jul 01, 2010 at 12:21 UTC

    fork creates a new process and variables are not shared between the parent process and child process, so either you use threads ( to share variables ) or you use Inter Process Communication to communicate the child with the parent. My preference is to use threads

      Hi,

      Thanks for the quick response, after a little checking, it turns out that one needs a perl-threads build to use threads, the perl I'm using is of my work place, and they dont support a threads version of perl.

      Can you elaborate on a different sulotion?
Re: Piping output from child to parent Tk widget
by sierpinski (Chaplain) on Jul 01, 2010 at 12:35 UTC
    In addition to what was said above about IPC, if you just want to print to STDOUT (and not actually have the processes talk to each other) you can use Parallel::ForkManager to spawn the child process, and have it call 'run_on_finish' which executes on the child process before it exits. That allows you to send a return code to the parent before the child dies (good for keep track of failures, etc) but still can print to STDOUT.

    I guess I'm not exactly sure what your goal is. Either way, using Parallel::ForkManager does take a lot of complexity out of forking processes.
      Hi,

      Really thanks for the quick response.

      I dont have a problem writing to STDOUT, the issue is writing to a text widget owned by the parent process.

      The process which creates the output doesnt ever stop, it's running on a loop, so using run_on_finish wouldnt help

      Thanks again for the reply.

        What is it that you want to write to the text widget? If it's something simple, a return code from the child process might suffice. Are you writing all of the ssh output to that widget?

        If it's not just a simple return value, it sounds like IPC (something like IPC::ShareLite) might be what you need. (I've never used it, but it seems pretty straightforward from what I've seen on the POD).

        Update: Misread part of your post, edited my response accordingly.