验证方法

使用非root账户,运行如下python脚本:

import threading
import time

########################################################################
class TestThread(threading.Thread):
def __init__(self):
"""Constructor"""
threading.Thread.__init__(self)

def run(self):
while True:
print 'haha'
time.sleep(2)

if __name__ == '__main__':
count = 1000
for i in range(count):
athread = TestThread()
athread.start()

当count接近1024时,运行时出现无法创建thread的提示。

使用root账户执行,无相同报错。

非root账户执行ulimit -a

ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 62820
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 1024
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited

max user processes参数配置为1024。

此项配置会限制最大用户进程为1024,而root用户则没有此限制。

使用root用户执行的结果如下:

ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 62820
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 62820
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited

此处max user processes设置为62820

修改方法

CentOS 6.x

使用root用户修改配置文件

vim /etc/security/limits.d/90-nproc.conf

# Default limit for number of user's processes to prevent
# accidental fork bombs.
# See rhbz #432903 for reasoning.

* soft nproc 1024
admin soft nproc 10240
root soft nproc unlimited

针对使用的admin用户增加配置

CentOS 7.x

vim /etc/security/limits.d/20-nproc.conf

再次运行上述python脚本,count参数配置为10000运行正常。