Linux threads are implemented as a separate process but sharing the same address space as other threads. By default they are hidden in the ps
command, but can be seen with the -L
flag.
For example:
% ps -fp 2642
UID PID PPID C STIME TTY TIME CMD
polkitd 2642 1 0 Dec09 ? 00:00:48 /usr/lib/polkit-1/polkitd --no-d
% ps -fLp 2642
UID PID PPID LWP C NLWP STIME TTY TIME CMD
polkitd 2642 1 2642 0 7 Dec09 ? 00:00:18 /usr/lib/polkit-1/pol
polkitd 2642 1 2680 0 7 Dec09 ? 00:00:00 /usr/lib/polkit-1/pol
polkitd 2642 1 2683 0 7 Dec09 ? 00:00:30 /usr/lib/polkit-1/pol
polkitd 2642 1 2685 0 7 Dec09 ? 00:00:00 /usr/lib/polkit-1/pol
polkitd 2642 1 2687 0 7 Dec09 ? 00:00:00 /usr/lib/polkit-1/pol
polkitd 2642 1 2688 0 7 Dec09 ? 00:00:00 /usr/lib/polkit-1/pol
polkitd 2642 1 2692 0 7 Dec09 ? 00:00:00 /usr/lib/polkit-1/pol
We can see that polkitd
really consists of 7 threads. They all have the same process ID, but different thread IDs (LWP) but show up as different processes in the ps
listing, because they are different processes in the kernel.
This can have impact, e.g. on ulimit
constraints. A common issue on RedHat and derivatives is that the default PAM configuration limits your processes
% cat /etc/security/limits.d/20-nproc.conf
# Default limit for number of user's processes to prevent
# accidental fork bombs.
# See rhbz #432903 for reasoning.
* soft nproc 4096
root soft nproc unlimited
On heavy java web apps the number of processes can exceed this and cause application failures.
clone()
syscall is a Linux invention. It is essentially afork()
while some data structures of the child process (memory map, file descriptors, signal stack and so on) can remain the same (not copy! Same by address!) as the parent. It is a very different thing than the Posix standard says (essentially, there is a separate threads api). Now, after a decade the smoke disappeared from the battlefield of the mailing lists, this is the state what you can see. It is not so bad - what is bad, that threads are still not so lightweight on Linux. – peterh Dec 30 '18 at 22:14clone()
, the posix threads library was included into the glibc, but there is a minimal thread support in the kernel. As far I know, it is still not N:M threading, which is still bad, bad, bad. Although real N:M threads are probably incompatible with the essence of the Unix API (where kernel calls are still synchronous). – peterh Dec 30 '18 at 22:26