理解 ulimit -u

tri*_*eee 5 linux unix ulimit

我想了解这里发生了什么。

linvx$ ( ulimit -u 123; /bin/echo nst )
nst

linvx$ ( ulimit -u 122; /bin/echo nst )
-bash: fork: Resource temporarily unavailable
Terminated

linvx$ ( ulimit -u 123; /bin/echo one; /bin/echo two; /bin/echo three )
one
two
three

linvx$ ( ulimit -u 123; /bin/echo one & /bin/echo two & /bin/echo three )
-bash: fork: Resource temporarily unavailable
Terminated
one
Run Code Online (Sandbox Code Playgroud)

我推测前 122 个进程是由 Bash 本身使用的,其余的ulimit进程控制着我允许拥有的并发进程数。文档对此不是很清楚。我错过了什么吗?

更重要的是,对于现实世界的部署,我怎么知道什么样的ulimit才是现实的?它是一个长时间运行的守护进程,它根据需要产生工作线程,并在负载减少时收获它们。我已经让它使服务器旋转数次。最重要的限制可能是内存,我现在每个进程的内存限制为 200M,但我想弄清楚如何对子进程的数量实施限制(该程序确实允许我配置最大值,但是如何我知道那部分代码没有错误吗?)


附录:在较新的系统上,我得到了更高的数字和略有不同的行为。

xubuntu12.04$ ( ulimit -u 206; /bin/echo nst )
nst

xubuntu12.04$ ( ulimit -u 205; /bin/echo nst )
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: Resource temporarily unavailable
Terminated

xubuntu12.04$ bash --version
GNU bash, version 4.2.24(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Run Code Online (Sandbox Code Playgroud)

我相信旧系统有 Bash v3。

在 中dash,我得到了不同的行为,尽管仍然不是我期望的行为(并且调用了该选项-p而不是-u):

xubuntu12.04$ dash

$ ( ulimit -p 1; /bin/echo nst )
nst

$ ( ulimit -p 2; /bin/echo nst & /bin/echo too & )
dash: 0: Cannot fork

$ ( ulimit -p 208; /bin/echo nst & /bin/echo too & )
dash: 0: Cannot fork
nst

$ ( ulimit -p 209; /bin/echo nst & /bin/echo too & )
nst
too
Run Code Online (Sandbox Code Playgroud)

Cha*_*ffy 6

不仅是 subshel​​l 中的子进程计入限制,而且系统上的所有内容都在您的 uid 下。

因此,如果您有 200 个进程以您自己的身份在系统上的任何位置运行,那么一个进程ulimit -u 205将只能在总数达到 205 之前分叉——即五次(如果没有任何内容)。