I want to limit the number of processes per user on my machine, with /etc/security/limits.conf
and the nproc value.
I have read here that Linux dosen't distinguish between processes and threads?
My current nproc limit per user is 1024, but if this includes also threads, it is too low in my point of view. The man-page of limits.conf
only mentions "process" for nproc and nothing else.
// edit
// sample code in C++ with Boost
// g++ -o boost_thread boost_thread.cpp -lboost_thread
#include <unistd.h>
#include <iostream>
#include <boost/thread.hpp>
using namespace std;
int counter;
void print_thread(int i) {
counter++;
cout << "thread(" << i << ") counter " << counter << "\n";
sleep(5);
counter--;
}
int main() {
int i = 0;
int max = 1000000;
while (i < max) {
boost::thread(print_thread, i);
i++;
}
return 0;
}
test (removed some lines):
$ ulimit -u
1024
$ ./thread
...
...
...
thread(828) counter 828
thread(829) counter 829
thread(830) counter 830
thread(831) counter 831
thread(832) counter 832
thread(610) counter thread(833833) counter 834
thread(834) counter 835
thread(835) counter 836
thread(836) counter 837
thread(837) counter 838
thread(838) counter 839
thread(839) counter 840
thread(840) counter 841
thread(841) counter 842
thread(842) counter 843
thread(843) counter 844
thread(844) counter 845
thread(845) counter 846
thread(846) counter 847
thread(847) counter 848
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::thread_resource_error> >'
what(): boost::thread_resource_error
Aborted (core dumped)
My laptop uses ~130 processes while being idle. So nproc, or Linux in a wider view, doesn't disinguish between processes and threads. Which seems reasonable to me, because threads could also exhausting, not only processes.
Best Answer
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 returningEAGAIN
. Thepthread_create(3)
documentation says the following about this error: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 inlimits.conf
(withnproc
),ulimit -u
orsetrlimit(2)
.