For Programmers: Free Programming Magazines  


Home > Archive > ithreads > June 2005 > thred's pids









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author thred's pids
Mihai Vlad

2005-06-08, 9:03 pm

I'm interested how can i get the pid of a started thread in perl the $$
variabile return the pid of the process that lounches the thread... does
anyone know how can i do this, without the Thread::Signal module wich i
can't seem to be able to use?
I'm using :

This is perl, v5.8.4 built for i686-linux-thread-multi

on a Gentoo Linux (2.4.6 kernel version)

--
Mihai Vlad
Software Test Engineer
GECAD Technologies
mailto:mihai.vlad@gecadtech.com


David Greaves

2005-06-08, 9:03 pm

Mihai Vlad wrote:

> I'm interested how can i get the pid of a started thread in perl the
> $$ variabile return the pid of the process that lounches the thread...
> does anyone know how can i do this, without the Thread::Signal module
> wich i can't seem to be able to use?
> I'm using :
>
> This is perl, v5.8.4 built for i686-linux-thread-multi
>
> on a Gentoo Linux (2.4.6 kernel version)
>

perldoc threads ?

use threads;
$thread = threads->self();

or just
threads->self()->tid;


David

Mihai Vlad

2005-06-08, 9:03 pm

David Greaves wrote:

> Mihai Vlad wrote:
>
> perldoc threads ?
>
> use threads;
> $thread = threads->self();
>
> or just threads->self()->tid;
>
>
> David
>

Both of your suggestions i've tried before:

#!/usr/bin/perl

use threads;

sub function
{
sleep 60;
}

$thr1 = threads->new(\&function);
$selfvar = $thr1->self();
$tidvar = $thr1->tid();
print "$selfvar\n${$selfvar}\n$tidvar\n";

$thr1->join();


none of these return the actupa pid of the thread:( (or at least i can't interpret it)

here-s what i get:

threads=SCALAR(0x81733b4)=SCALAR(0x81733
90)
135846736
1

when these threds were running ps ax gave me these:
25234 pts/4 S+ 0:00 /usr/bin/perl ./test
25235 pts/4 S+ 0:00 /usr/bin/perl ./test
25236 pts/4 S+ 0:00 /usr/bin/perl ./test

I'm interested in the number 25236 wich i'm sure is the pid to my opened thred , how can i get this number just using perl and it's modules? :( i've tryied like anything.... now i'm trying to see the code in how a thread is created perhaps there is someth
ing there, if you know already or know a better way pls tell me.
10x


--
Mihai Vlad


David Greaves

2005-06-08, 9:03 pm

Mihai Vlad wrote:

> none of these return the actupa pid of the thread:( (or at least i
> can't interpret it)
>
> here-s what i get:
>
> threads=SCALAR(0x81733b4)=SCALAR(0x81733
90)
> 135846736
> 1
>
> when these threds were running ps ax gave me these:
> 25234 pts/4 S+ 0:00 /usr/bin/perl ./test
> 25235 pts/4 S+ 0:00 /usr/bin/perl ./test
> 25236 pts/4 S+ 0:00 /usr/bin/perl ./test
>
> I'm interested in the number 25236 wich i'm sure is the pid to my
> opened thred , how can i get this number just using perl and it's
> modules? :( i've tryied like anything.... now i'm trying to see the
> code in how a thread is created perhaps there is something there, if
> you know already or know a better way pls tell me.
> 10x


OK
That's what I expect. The tid of your thread ($thr1) is 1.

25235 that's beyond my knowledge level :)

From reading the docs, I'm pretty sure that you're not supposed to look
at it - it's implementation specific.
I'd expect some systems not to have a per-thread OS-level pid - indeed
linux may not in the future.

Why do you need to?

Are you wanting to use signals? In which case set the signal handler in
the thread and signal() the master PID.
See if that works.
I'd expect it to be threadsafe given the docs caveat:
If your Perl has been built with PERL_OLD_SIGNALS (one has to explicitly
add that symbol to ccflags, see |perl -V|), signal handling is not
threadsafe.

David



Mihai Vlad

2005-06-08, 9:03 pm

David Greaves wrote:

> Mihai Vlad wrote:
>
>
>
> OK
> That's what I expect. The tid of your thread ($thr1) is 1.
>
> 25235 that's beyond my knowledge level :)
>
> From reading the docs, I'm pretty sure that you're not supposed to
> look at it - it's implementation specific.
> I'd expect some systems not to have a per-thread OS-level pid - indeed
> linux may not in the future.
>
> Why do you need to?
>
> Are you wanting to use signals? In which case set the signal handler
> in the thread and signal() the master PID.
> See if that works.
> I'd expect it to be threadsafe given the docs caveat:
> If your Perl has been built with PERL_OLD_SIGNALS (one has to
> explicitly add that symbol to ccflags, see |perl -V|), signal handling
> is not threadsafe.
>
> David
>
>
>

No, i have a test script written in perl, that starts over 1000 threads
that read/write on a specific socket , every thread logs it's actions on
a single file called threadTID.log. Sometimes some of the threads don't
close (the script is safely made: i mean no write or read is left
unlogged or unsupervised, if a single write failes the thread is killed)
so i'm suspecting that the program i'm testing is to blame for.
Now it would be very easy to identify the logs of the hanging threads if
they would be something like thrPID.log.

Of course i've implemented a more simple solution like: writing
something at the end of a log when the thread closes:P and by this way
making it easy for my to identify the remaining threads logs.
But i was curious if i could find the threads pid and implemente like
i've written above for future knolledge, and scripts:). I know that
threads written in C and other languages know their pids, so i'm really
curious if i can find this in perl...

Regarding the signals i am aware of the %SIG hash.

--
Mihai Vlad


Elizabeth Mattijsen

2005-06-08, 9:03 pm

At 11:08 AM +0200 11/4/04, Mihai Vlad wrote:
>David Greaves wrote:
>No, i have a test script written in perl, that starts over 1000 threads


With regards to resource usage of 1000 threads, you might want to
check my article about that on Perl Monks:

http://perlmonks.org/index.pl?node_id=288022


> that read/write on a specific socket , every thread logs it's
>actions on a single file called threadTID.log. Sometimes some of the
>threads don't close (the script is safely made: i mean no write or
>read is left unlogged or unsupervised, if a single write failes the
>thread is killed) so i'm suspecting that the program i'm testing is
>to blame for.
>Now it would be very easy to identify the logs of the hanging
>threads if they would be something like thrPID.log.
>
>Of course i've implemented a more simple solution like: writing
>something at the end of a log when the thread closes:P and by this
>way making it easy for my to identify the remaining threads logs.
>But i was curious if i could find the threads pid and implemente
>like i've written above for future knolledge, and scripts:). I know
>that threads written in C and other languages know their pids, so
>i'm really curious if i can find this in perl...
>
>Regarding the signals i am aware of the %SIG hash.


From the documentation of my Thread::Signal module on CPAN:

http://search.cpan.org/~elizabeth/T...hread/Signal.pm


========================================
=============================
SYSTEMS IT DOESN'T WORK ON

Check the output of:
perl -V:cppflags

The absence of the string -DTHREADS_HAVE_PIDS is a good indication
that Thread::Signal will most certainly not work. However, if that
string does occur, it is no guarantee that Thread::Signal will work.
The only way to find out is to run the test-suite and see whether
that works.
========================================
=============================

You might also want to check the Caveats section.



Liz
Mihai Vlad

2005-06-08, 9:03 pm

I already red the article you 've posted on perlmonks, it came usefull
when i started learning about perl threads.
Now i've recompiled perl with the addition of the required flag, and
i've installed your module, wich seems to work. But i've looked throw
your code the function that i seem to need and you use :
_threadpid..... where does it come from??
plus i've tryied to use your tids2pids funcion:( not a succes:((.
Yep i've red more on Threads pids and the fact that they are on the
verge of extinction, i really don't agree with this, but if is like that
how can a user still se ps -m?? and the threads will have the same PID?
hmm curious

--
Mihai Vlad

Elizabeth Mattijsen

2005-06-08, 9:03 pm

At 3:20 PM +0200 11/4/04, Mihai Vlad wrote:
>I already red the article you 've posted on perlmonks, it came
>usefull when i started learning about perl threads.
>Now i've recompiled perl with the addition of the required flag, and
>i've installed your module, wich seems to work. But i've looked
>throw your code the function that i seem to need and you use :
> _threadpid..... where does it come from??


It's from the XS code, you should have a file Thread/Signal.xs in the
distribution.


>plus i've tryied to use your tids2pids funcion:( not a succes:((.


That depends on _threadpid.


>Yep i've red more on Threads pids and the fact that they are on the
>verge of extinction, i really don't agree with this, but if is like
>that how can a user still se ps -m?? and the threads will have the
>same PID? hmm curious


Well, if it doesn't work, you're most likely on a system that does
not support it.


Liz
Mihai Vlad

2005-06-08, 9:03 pm

At this script i get this response:

Scalars leaked: 1
USR1 ALRM
2
2
thread failed to start: Usage: Thread::Signal::_threadpid() at ./test
line 13.


Elizabeth Mattijsen

2005-06-08, 9:03 pm

At 4:31 PM +0200 11/4/04, Mihai Vlad wrote:
>At this script i get this response:
>
>Scalars leaked: 1
>USR1 ALRM
>2
>2
>thread failed to start: Usage: Thread::Signal::_threadpid() at ./test line 13.
>
>
>#!/usr/bin/perl
>
>
>use threads;
>use Thread::Signal;
>use threads::shared;
>
>
>
>sub bubu {
> @tid = Thread::Signal->tids( 'ALRM' );
> print "@tid\n";
> $x = Thread::Signal->_threadpid($tid[0]);


_threadpid is an internal subroutine, _not_ a class method.

Try:

$x = Thread::Signal::_threadpid($tid[0]);

instead.


> print "$x\n";
> }
>
>
>Thread::Signal->register;
>Thread::Signal->automatic(qw(ALRM USR1));
>@automatic = Thread::Signal->automatic;
>
>print "@automatic\n";
>$thr = threads->new(\&bubu);
>$threadtid = $thr->tid();
>print "$threadtid\n";
>
>$thr->join();


Liz
Mihai Vlad

2005-06-08, 9:03 pm

Try:

>
> $x = Thread::Signal::_threadpid($tid[0]);
>

i get the same answer:( this probablly means that my system dows not
support your module, thanks for your help so far thow

--
Mihai Vlad


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com