Well, very seldom do you see someone trying to write to xterms as a means of output, so here is a bit of a thread example, that shows how neat a gui can be. This works on the idea that threads can share filehandles thru the fileno. Threads are not always the best solution, because they tend to gain memory, but for 1 shot scripts, it's easy. If you really need to fork, search for shared memory ipc, or sockets ipc.
#!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; use Tk; use Tk::Pane; my @colors = qw(wheat lightyellow khaki lightgreen lightsteelblue skyblue powderblue bisque beige snow ); my %shash; #share(%shash); #will work only for first level keys my %thash; #start threads before any Tk is called for my $thrnum(1..10){ share ($shash{$thrnum}{'go'}); share ($shash{$thrnum}{'fileno'}); share ($shash{$thrnum}{'pid'}); share ($shash{$thrnum}{'die'}); $shash{$thrnum}{'go'} = 0; $shash{$thrnum}{'fileno'} = -1; $shash{$thrnum}{'pid'} = -1; $shash{$thrnum}{'die'} = 0; $thash{$thrnum}{'thread'} = threads->new(\&work,$thrnum)->detach(); } my $mw = MainWindow->new(-background => 'gray50'); $mw->geometry('600x600'); my $sp = $mw->Scrolled('Pane', -scrollbars =>'osoe', )->pack(-expand=>1,-fill=>'both'); for my $thrnum(1..10){ $thash{$thrnum}{'frame'} = $sp->pack(-expand=>1,-fill=>'both'); $thash{$thrnum}{'label'} = $thash{$thrnum}{'frame'}->Label( -bg=>'black',-fg=>'yellow', -text => "Thread $thrnum")->pack(); my $color = shift @colors; $thash{$thrnum}{'display'} = $thash{$thrnum}{'frame'}->Scrolled( 'Text',-bg=>$color)->pack(); } my $startb = $mw->Button( -text => 'Start', -command=>sub{ for my $thrnum(1..10){ $shash{$thrnum}{'go'} = 1; $mw->after(100); #give time for thread to set up 100 mse +c my $fileno = $shash{$thrnum}{'fileno'}; print "fileno_m $fileno\n"; open (my $fh, "<&=$fileno") or warn "$!\n"; # filevent works but may not work on win32, but you can use a timer i +nstead $mw->fileevent(\*$fh, 'readable', [\&fill_text_widget,$thash{$thrnum} +{'display'},$fh]); } })->pack(); my $stopb = $mw->Button( -text => 'Stop', -command=>sub{ for my $thrnum(1..10){ $shash{$thrnum}{'die'} = 1; } exit; } )->pack(); MainLoop; ######################### sub fill_text_widget { my($widget,$fh) = @_; my $text = <$fh>; $widget->insert('end',"$text"); $widget->yview('end'); } sub work{ my $thrnum = shift; print "thread $thrnum starting\n"; $|++; while(1){ if($shash{$thrnum}{'die'} == 1){print 'return'; return }; if ( $shash{$thrnum}{'go'} == 1 ){ if($shash{$thrnum}{'die'} == 1){ print 'return';return }; my $pid = open(FH, "top -b |" ) or warn "$!\n"; my $fileno = fileno(FH); print "fileno_t->$fileno\n"; $shash{$thrnum}{'fileno'} = $fileno; $shash{$thrnum}{'go'} = 0; #turn off self before returning }else { select(undef,undef,undef,.1) } #short sleep } return }

I'm not really a human, but I play one on earth Remember How Lucky You Are

In reply to Re^3: help with Fork and terminal output by zentara
in thread help with Fork and terminal output by bittis

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.