ajeet@perl has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Monks,

I am having problem with debugging in perl threads,say i have 5 threads, now what i want is that bind each thread to a separate tty/pty, so that output and progress of each thread can independently tracked.

I need this very much in case of debugging, where i have not any clue about different threads.

I tried some resources from net, but they does not seem to work.

Please help me out....Thanks in Advance

Replies are listed 'Best First'.
Re: tty for each perl threads
by BrowserUk (Patriarch) on Apr 05, 2010 at 20:10 UTC

    Personally, I'd suggest not doing this. As soon as you start tracing/logging each thread to a different console, you loose sight of any ordering. Better to trace to a single console, serialise the access to that console and prefix your messages with the thread id.

    my $semErr :shared; sub twarn { lock $semErr; my $tid = thread->tid; warn sprintf("[%3d]", $tid), @_; } ... twarn "Something happened\n";

    You can then filter stderr to concentrate on individual threads.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: tty for each perl threads
by almut (Canon) on Apr 05, 2010 at 18:40 UTC

    You could try opening STDOUT (and/or STDERR) to the respective pty of the terminal emulator where the output is to be routed to, i.e. something like

    open STDOUT, ">", "/dev/pts/7" or die $!;

    To figure out which pty is associated with a certain terminal/shell, you can for example use

    $ ls -l /proc/$$/fd/1 lrwx------ 1 almut almut 64 2010-03-22 00:16 /proc/25932/fd/1 -> /dev/ +pts/7

    (on Linux, that is — paths may be different on other systems)

      What about using the 'tty' command? It should be available in Linux:
      me@host> tty /dev/pts/53 my $tty = `/bin/tty`;
      I *thought* that was standard for all *nix systems, but at the least it works on Solaris and several Linux distros.
      Thank You...got it