Does GNU/Linux counts processes and threads together when I limit their number?
Solution 1:
The nproc
limit you are talking about applies to runnable entities, it is thus limiting threads (and therefore, processes containing them). Every process has at least one thread (the primary thread), so that only threads can be run. Strictly speaking, processes are not "runnable".
This answer explains the real difference between threads and processes in Linux.
I tested the code in daya's answer (also added sleep(1);
in thread code) and unlike him (?!), I hit the limit when too many threads were created: pthread_create()
was returning EAGAIN
. The pthread_create(3)
documentation says the following about this error:
EAGAIN
Insufficient resources to create another thread, or a system-imposed limit on the number of threads was encountered. The latter case may occur in two ways: the RLIMIT_NPROC soft resource limit (set via setrlimit(2)), which limits the number of process for a real user ID, was reached; or the kernel's system-wide limit on the number of threads, /proc/sys/kernel/threads-max, was reached.
I see no mention of a specific per-thread limit in the kernel source, I see only RLIMIT_NPROC
there, which is the limit you can change in limits.conf
(with nproc
), ulimit -u
or setrlimit(2)
.