in reply to Re^2: help with Fork and terminal output
in thread help with Fork and terminal output

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