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

Hello Perl Monks...

I am having a problem related to thread.

I am creating any thread by following code

my $thr=threads->create(&funName);

Now when i use "ps -ef | grep" to list my process, I see two processes running with same file name. If say the name of my file is createThread.pl then I see two threads with name createThread.

What I want is that Chnage the name of threads which are created by main process of my file, say if I assign name "createThreadChild" to child thread then after executing the "ps -ef | grep " command, I should see one process with name "createThread" and another process (which is actually the child of createThread) with name "createThreadChild"...

Thanks in Advance

Replies are listed 'Best First'.
Re: assigning a name to every threads
by moritz (Cardinal) on Apr 29, 2010 at 08:14 UTC
    The perl-5.13 development release allows assinging to $0 on linux - I don't know if that helps you with individual threads though.
      That is good, but I want this mechanism in perl 5.8.8... Please share some idea with this version of perl...
Re: assigning a name to every threads
by samarzone (Pilgrim) on Apr 29, 2010 at 09:41 UTC

    I don't think you can list threads by ps -ef. As far as I know it only lists processes. Threads and processes are different.

    If ps -ef shows two processes there are actually two processes, not the threads created by the mentioned snippet

    PS: See man ps for thread listing options

      I am sorry... you are right..

      actually I am using forks module instead of the threads module. so the thread i am talking about is actually a process internally...

      Hope, I am clear now...

      any way Thanks samarzone ...

        actually I am using forks module instead of the threads module

        In that case, you can simply assign to $0, which has been working fine for ages (on Linux, and for processes) — i.e. you don't need 5.13:

        #!/usr/bin/perl use forks; my @procs; for my $procname (qw(foo bar baz)) { push @procs, threads->new( sub { $0 = $procname; sleep 1; } ); } system "ps fT -o pid,cmd"; $_->join() for @procs; __END__ $ ./837477.pl PID CMD 10408 bash -rcfile .bashrc 5278 \_ /usr/bin/perl ./837477.pl 5279 \_ /usr/bin/perl ./837477.pl 5280 \_ foo 5281 \_ bar 5282 \_ baz 5283 \_ ps fT -o pid,cmd

        (the foo/bar/baz are your "forks-threads")

Re: assigning a name to every threads
by llancet (Friar) on Apr 29, 2010 at 11:23 UTC
    If you really need to assign thread to a name, use a hash to store them. You can store process ID in same way.
      Yeah, That i can do. But I want to change the name of process in such a way that it should be visible in ps command