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

Hi everyone
I have the following problem, i am writting a script which at some point needs to do some parallel processing, the way of going about this for me is to use fork in perl to start new child processes and have each one then do what it should. My problem though is that i want the user to have a clear view of what messages each child process is sending to the current terminal (the script will be running on a Ubuntu box) and rather than all messages from all children appearing on the same terminal have each child process have its own terminal. Is there any way of creating a new terminal for eahc child process assigning that child process to output to that terminal?
Thank you!

Replies are listed 'Best First'.
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.

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

      Would it be possible to open a filehandle to the /dev returned by tty in each xterm?

      And then have each fork read/write to a different filehandle?

      Heatseeker Cannibal
        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;

        I'm not really a human, but I play one on earth Remember How Lucky You Are
        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.


        I'm not really a human, but I play one on earth Remember How Lucky You Are
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 (κς,πμ,πλ)
      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!
        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.

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