如何更改每个进程的最大打开文件数的默认值?

如何更改每个进程的最大打开文件数的默认值?

我将最大打开文件数改为 20000。但是我仍然遇到限制,我发现每个进程都有限制。我想知道如何更改每个进程的默认限制?

ubuntu@ip-172-16-137-139:~$ cat /proc/1237/limits
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             31538                31538                processes 
Max open files            1024                 4096                 files
Max locked memory         65536                65536                bytes     
Max address space         unlimited            unlimited            bytes     
Max file locks            unlimited            unlimited            locks     
Max pending signals       31538                31538                signals   
Max msgqueue size         819200               819200               bytes     
Max nice priority         0                    0                    
Max realtime priority     0                    0                    
Max realtime timeout      unlimited            unlimited            us        

ubuntu@ip-172-16-137-139:~$ 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) 31538
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 20000               <- changed this
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 31538
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

答案1

getrlimit 和 setrlimit 调用控制大多数每个进程的资源分配限制。RLIMIT_NOFILE控制文件描述符的最大数量。您需要适当的权限才能调用它。

相关内容