Is there a case where PID does not end up being unique? Is it the best way to identify a process (or a thread)? I read that previous versions of Linux had a different approach
Thanks
PIDs use a control loop feedback or process variable to monitor where the output should be. These usually come in the form of sensors and meters. PIDs come in many different forms including standalone units and PLC programming. We can use our input and output cards along with programming software to set up a PID.
– In a PID Controller, P stands for Proportional, I stands for Integral, and D stands for Derivative. The PID Controller is responsible for ensuring that the Process remains as close to the desired value as possible regardless of various disruptions.
A PID (Proportional Integral Derivative) controller works by controlling an output to bring a process value to a desired set point.
Most process controllers, PLC, and DCS loop controllers sold today have Autotuning capability. The PID controller learns how the process responds to a change in setpoint, and suggested PID settings.
PIDs can take about 32k values (sysctl kernel.pid_max
can change it), so they are reused quickly enough. pid + start time may be better. systemd attaches per-process information on extended attributes in cgroupfs (procfs would also work) to avoid the ambiguity of a pid->attr mapping.
Threads and processes share the same namespace (you can see it in /proc/<pid>/task/<taskid>
), with <pid>
= <taskid>
for the process' initial thread. Pid namespaces limit the list of visible pids, but they don't introduce any overlap; pids and task ids remain unique while their owner is running.
If you are talking about the getpid()
system call then yes, PID is unique per process. Except, that is, if you are using threads on older versions of the Linux kernel. Then each thread may have its own process-id.
To quote from this discussion:
Kernel 2.4.20 uses NPTL (Native posix thread library) 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.
From the wikipedia link on NPTL:
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.2
Under the covers even the 2.6.X kernels have a virtual-process for threads. You can see the thread process-ids with ps auxf
:
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
The following program spits out the same pid for both main and thread under Linux kernel 2.6.18. The self id returned from pthread_self()
identifies the thread uniquely.
#include <pthread.h>
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(&thread, 0L, foo, 0L);
pthread_join(thread, 0L);
}
The output is:
main: pid = 13246, self = 46912496175248
thread: pid = 13246, self = 1084229952
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With