I was originally going to use threads but I discovered my laptop Mandriva Linux system wasn't compiled with threads so I thought I'd give forking a try.

I've got the basics working. Comments Welcome.

Here is the code...
#!/usr/bin/perl -w use Tk; use Tk::widgets qw/LabEntry ROText/; use vars qw/$mw $VERSION/; use IO::Handle; use strict; our ($c , $text, $mw); $SIG{CHLD} = sub {wait}; $SIG{PIPE} = sub {print STDERR "pipe failure.\n"; exit}; do_pipe(); $mw = MainWindow->new; init(); $text->insert('end', "Initial Text\n"); my $line; print "In parent about to send...\n"; pipe_out( "Parent Pid $$ is sending this\n"); print "In parent sent...\n"; chomp($line = <PR>); print "parent just read -->$line<--\n"; print "In parent about to send...\n"; print "Parent Pid $$ just read this: `$line'\nEOF"; MainLoop(); close PR; close PW; exit; sub do_fork_stuff { my $line = <PR>; $text->insert('end', $line); $text->yview('end'); } # end do_fork sub init { $VERSION = '1.0'; $mw->title("frog $VERSION"); $mw->Button(-text => "Exit", -command => sub { exit })->pack(-side => 'bottom', -expand => 1, -fill => 'x'); $mw->Button(-text => "fork stuff", -command => sub { do_fork_stuff() })->pack(-side => 'top', + -expand => 1, -fill => 'x'); $c = $mw->Canvas( -width => 1, -height => 1, -background => 'dark slate gray', )->pack; my $mainimage = $c->Photo(-file => 'table.gif'); $c->createImage(0, 0, -image => $mainimage, -tag => 'mainimage', -anchor => 'nw', ); #$c->configure(-width => $mainimage->width, -height => $mainimage- +>height); $c->configure(-width => 400, -height => 300); my $green = '#d5dac1'; my $font = 'courier 12'; my $f = $c->Frame( -width => 270, -height => 110, -background => $green, -relief => 'sunken', -borderwidth => 3, ); $f->packPropagate(0); $c->createWindow(70, 150, -anchor => 'nw', -window => $f); $text= $f->Scrolled('ROText', -scrollbars => 'oe')->pack; } # end init sub pipe_out { return unless defined $_[0]; my($bytes, $offset, $sysdata, $sysstat, $wait); $sysdata = join '', @_, "EOF\n"; $bytes = length $sysdata; $offset = 0; $mw->fileevent(\*PW, 'writable' => sub { while ($bytes > 0) { $sysstat = syswrite PW, $sysdata, $bytes, $offset; die "ipadm: syswrite error $!" unless defined $sysstat; $bytes -= $sysstat; $offset += $sysstat; } $wait++; }); $mw->waitVariable(\$wait); $mw->fileevent(\*PW, 'writable' => ''); } # end pipe_out sub do_pipe{ pipe CR, PW or die "cr/pw pipe $!"; pipe PR, CW or die "pr/cw pipe $!"; if (my $pid = fork) { close CR or die "Client socket close failed: $!"; close CW or die "Client socket close failed: $!"; PW->autoflush(1); } elsif (defined $pid) { close PR; close PW; print "in child waiting...\n"; open STDIN, "<&CR" or die "STDIN open $!"; open STDOUT, ">&CW" or die "STDOUT open $!"; open STDERR, ">&CW" or die "STDERR open $!"; STDOUT->autoflush(1); STDERR->autoflush(1); chomp($line = <STDIN>); print "Child Pid $$ just read this: `$line'\n"; print "fork 1\n"; print "fork 2\n"; print "fork 3\n"; print "fork 4\n"; print "fork 5\n"; print "fork 6\n"; print "fork 7\n"; print "fork 8\n"; print "fork 9\n"; print "fork 10\n"; print "fork 11\n"; print "fork 12\n"; print "fork 13\n"; print "fork 14\n"; print "fork 15\n"; print "fork 16\n"; print "fork 17\n"; print "fork 18\n"; print "fork 19\n"; print "fork 20\n"; die "exec warp $!"; } else { die "fork error: $!"; } }

In reply to Re^2: Tk and using a pipe to read command results by drake50
in thread Tk and using a pipe to read command results by drake50

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.