如何增加子进程的数量

How to increase number of child proceses

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 中定义,即使它可能被注释掉,32768 中的 33%

#UserTasksMax=33%

当您修改 UserTasksMax 限制或增加 sysctl kernel.pid_max 时,您必须重新启动 systemd-logind 服务:

service systemd-logind restart

在 64 位系统上,您应该能够将值增加到 2^22,即:4194304

sysctl kernel.pid_max=4194304