我想用/etc/security/limits.conf
和nproc值限制我的计算机上每个用户的进程数。
我在这里读到Linux不能区分进程和线程吗?
我当前的每位用户nproc限制为1024,但是如果这还包括线程,那么从我的角度来看,它太低了。的手册页limits.conf
仅提及nproc的“过程” ,仅此而已。
//使用Boost //在C ++中编辑示例代码// 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;
}
测试(删除了一些行):
$ 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)
我的笔记本电脑闲置时使用约130个进程。因此,nproc或更广泛的Linux不会区分进程和线程。在我看来,这是合理的,因为线程不仅会耗尽进程,还会耗尽资源。