Re: help with Fork and terminal output
by zentara (Cardinal) on Aug 19, 2008 at 17:12 UTC
|
If you used some sort of GUI (Tk, Gtk2, Wx), you could open a small window for each fork to output to. Alternatively, you could open a separate xterm on each fork.
| [reply] |
|
|
| [reply] |
|
|
I came up with a minimal example of the fork writing to it's own xterm. It's kindof weird, trying get the ppid and pid, but this worked on linux. (At least it shows the concept). A second delay is needed to get the pids to showup properly.
#!/usr/bin/perl
use warnings;
use strict;
use Proc::ProcessTable;
my $time = time();
my $pid;
my $pid1 = open(PH,"| xterm -T $time -e bash");
print "pidinit $pid1\n";
sleep 1;
for my $p (@{new Proc::ProcessTable->table}){
if($p->ppid == $pid1){
$pid = $p->pid;
}
}
print "$pid\n";
open(FH,">/proc/$pid/fd/1") or warn $!;
for(1..1000){print STDOUT $_,"\n"; print FH $_.'a',"\n";sleep 1;}
close FH;
| [reply] [d/l] |
|
|
I was just trying that out, without luck. It might be best to ask that on another top node, so the low level c guys who understand dup, stdout, etc. can answer it. I'm sure it can be done, but there are problems, like the difference in pid between the xterm and the bash shell it uses....once you get the right pid, you can write to the file descriptors, as long as you own them. It would be easier for me to do it in a Tk or GTk2 gui, so I have control over the exact filehandles. A cheap way out would be to use xmessage and let each fork write an xmessage, with it's own title.
| [reply] |
Re: help with Fork and terminal output
by massa (Hermit) on Aug 19, 2008 at 23:22 UTC
|
You can open child processes in screen(1).
[]s, HTH, Massa (κς,πμ,πλ)
| [reply] |
|
|
Hey everyone, thank you for your replies, not sure what you ment with your reply Massa, can you elaborate a bit more please?
Zentara i will try your solution now and see how that works for me. Thanks!
| [reply] |
|
|
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.
| [reply] [d/l] |