Get thread pid

Jean-Paul Calderone exarkun at divmod.com
Fri Jan 30 11:11:27 EST 2009


On Fri, 30 Jan 2009 06:56:10 -0800 (PST), Alejandro <alejandro.weinstein at gmail.com> wrote:
>On Jan 30, 4:00 am, Ove Svensson <ove.svens... at jeppesen.com> wrote:
>> Pidis a process identifier. Threads are not processes. All your threads
>> execute within the context if a single process, hence they should have
>> the samepid. Threads may have athreadid but it is not the same as thepid.
>
>According to this document (http://heather.cs.ucdavis.edu/~matloff/
>Python/PyThreads.pdf), at least in Linux, threads are process:
>
>"Here each thread really is a process, and for example will show up on
>Unix systems when one runs the appropriate ps process-list command,
>say ps axH. The threads manager is then the OS."
>
>If you look at my original post, pstree does show different PIDs for
>the threads.

That document is quite misleading.  Threads are not processes.  They are
*similar* to processes.  They share many qualities of processes.  But they
are not processes.  The "H" option for ps is *explicitly* documented (in
recent versions of ps) as a way to make ps lie to you:

       H               Show threads as if they were processes

In times long since past, threads on Linux were even more like processes
than they are now.  Then, it might have even been defensible to say that
they were processes, but that was an implementation detail.  These days,
threads on Linux are provided by something called NPTL (whereas they used
to be provided by something called LinuxThreads).  When using NPTL, threads
do not have distinct PIDs.

So, if you want to learn more about threads on Linux, you should check out
the NPTL documentation.  It is a much more reliable authority than any
Python documentation regarding the nature of threads on Linux.

Hope this helps,

Jean-Paul



More information about the Python-list mailing list