Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you are talking about the <code>getpid()</code> system call then yes, PID is unique per process. <em>Except</em>, that is, if you are using threads on older versions of the Linux kernel. Then each thread may have its own process-id.</p> <p>To quote from <a href="http://www.linuxquestions.org/questions/programming-9/pthread-problem-65271/" rel="nofollow">this discussion</a>:</p> <blockquote> <p>Kernel 2.4.20 uses <a href="http://en.wikipedia.org/wiki/Native_POSIX_Thread_Library" rel="nofollow">NPTL (Native posix thread library)</a> and this is the kernel shipped with RH9. RH8 uses Kernel 2.4.18 which doesn't implement NPTL (Meaning each thread gets its own PID and therefore a good description of it's status in /proc). NPTL is a "real" implementation of POSIX threads meaning that the threads share alot more including the PID. It is more efficient way of running threads for a couple of reasons, however, I don't know of any easy tricks to debug these kind of threads. How do you know when your thread is sleeping versus waiting on a semaphore, or which threads have died in a process with lots of threads, etc. </p> </blockquote> <p>From the <a href="http://en.wikipedia.org/wiki/Native_POSIX_Thread_Library" rel="nofollow">wikipedia link on NPTL</a>:</p> <blockquote> <p>NPTL has been part of Red Hat Enterprise Linux since version 3, and in the Linux kernel since version 2.6. It is now a fully integrated part of the GNU C Library.<a href="http://en.wikipedia.org/wiki/Native_POSIX_Thread_Library" rel="nofollow">2</a></p> </blockquote> <p>Under the covers even the 2.6.X kernels have a virtual-process for threads. You can see the thread process-ids with <code>ps auxf</code>:</p> <pre><code>root 2501 0.0 0.3 244448 25576 ? Ss Jul03 0:11 /usr/sbin/httpd apache 2716 0.0 0.5 384776 46696 ? S Oct14 0:17 \_ /usr/sbin/httpd apache 2717 0.0 0.5 382208 44304 ? S Oct14 0:11 \_ /usr/sbin/httpd </code></pre> <p>The following program spits out the same pid for both main and thread under Linux kernel 2.6.18. The self id returned from <code>pthread_self()</code> identifies the thread uniquely.</p> <pre><code>#include &lt;pthread.h&gt; void foo() { printf("thread: pid = %d, self = %ld\n", getpid(), pthread_self()); } main() { pthread_t thread; printf("main: pid = %d, self = %ld\n", getpid(), pthread_self()); pthread_create(&amp;thread, 0L, foo, 0L); pthread_join(thread, 0L); } </code></pre> <p>The output is:</p> <pre><code>main: pid = 13246, self = 46912496175248 thread: pid = 13246, self = 1084229952 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload