cat /sys/fs/cgroup/pids/parent/pids.max = "max"
我按照 https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v1/pids.html 创建了它
考虑这个演示问题的 Python 代码:
from os import fork, getpid
from time import sleep
i=0
print( "pid = %d " % getpid())
with open("/proc/%d/limits" % getpid(), "r") as f:
print(f.read())
try:
while fork():
i+=1
except BaseException as e:
print(i)
print(e)
sleep(10)
print("done")
exit(1)
我的输出:
pid = 18091
Limit Soft Limit Hard Limit Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size 8388608 unlimited bytes
Max core file size 0 unlimited bytes
Max resident set unlimited unlimited bytes
Max processes 999999 999999 processes
Max open files 1024 1048576 files
Max locked memory 67108864 67108864 bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 31412 31412 signals
Max msgqueue size 819200 819200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us
10227
[Errno 11] Resource temporarily unavailable
done
在 Linux 中,您有pid_max
限制:
$ cat /proc/sys/kernel/pid_max
32768
但是,如果您的 Linux 在 systemd 上运行,您可能会遇到user-slice
限制:
对于root
$ cat /sys/fs/cgroup/pids/user.slice/user-0.slice/pids.max
对于当前登录的用户:
$ cat /sys/fs/cgroup/pids/user.slice/user-$(id -u).slice/pids.max
10813
systemd 等效项将是:
$ systemd-analyze dump | sed -n "/-> Unit user-$(id -u).slice:/,/-> Unit /p"| grep -e "TasksMax="
TasksMax=10813
据man logind.conf
UserTasksMax= Sets the maximum number of OS tasks each user may run concurrently. > This controls the TasksMax= setting of the per-user slice unit, see systemd.resource-control(5) for details. If assigned the special value "infinity", no tasks limit is applied. Defaults to 33%, which equals 10813 with the kernel's defaults on the host, but might be smaller in OS containers.
这个限制在/etc/systemd/logind.conf
中定义,即使它可能被注释掉,33%
32768
之外。
#UserTasksMax=33%
修改UserTasksMax
限制或增加sysctl kernel.pid_max
时,必须重新启动systemd-logind
服务:
service systemd-logind restart
在 64 位系统上,您应该能够将值增加到 2^22,即:4194304
sysctl kernel.pid_max=4194304