ulimit用法

ulimit

-a:显示当前所有的资源限制
-c:core文件大小
-f:设置创建文件的最大值
-n:设置内核可以同时打开的文件描述符的最大值
-p:设置管道缓冲区的最大值
-s:设置堆栈的最大值
-u:用户最多可开启的程序数目

创建文件的最大值

# ulimit -f 10
# cat vim-6.4.tar.bz2 > test
File size limit exceeded (core dumped)
# ll test
-rwxrwxrwx 1 root root 10240 May 16  2017 test*
# ulimit -f unlimited //无限制

文件描述符的最大值(临时)

# ulimit -n //默认
1024
# ulimit -n 10240 //修改
# ulimit -n 
10240
int i = 1;
while(i++)
{
    int fd = open("./hello.tk", O_RDONLY);
    if(fd < 0)
    {
        perror("open");
        printf("i = %d\n", i);
        return -1;
    }
}

# ./a.out 
open: Too many open files
i = 10239

limits.conf

# vi /etc/security/limits.conf
<domain>        <type>  <item>  <value>

<domain> can be:
        - a user name
        - a group name, with @group syntax
        - the wildcard *, for default entry
        - the wildcard %, can be also used with %group syntax, for maxlogin limit
        - NOTE: group and wildcard limits are not applied to root. To apply a limit to the root user, <domain> must be the literal username root.

<type> can have the two values:
        - "soft" for enforcing the soft limits
        - "hard" for enforcing hard limits

<item> can be one of the following:
        - core - limits the core file size (KB)
        - data - max data size (KB)
        - fsize - maximum filesize (KB)
        - memlock - max locked-in-memory address space (KB)
        - nofile - max number of open files
        - rss - max resident set size (KB)
        - stack - max stack size (KB)
        - cpu - max CPU time (MIN)
        - nproc - max number of processes
        - as - address space limit (KB)
        - maxlogins - max number of logins for this user
        - maxsyslogins - max number of logins on the system
        - priority - the priority to run user process with
        - locks - max number of file locks the user can hold
        - sigpending - max number of pending signals
        - msgqueue - max memory used by POSIX message queues (bytes)
        - nice - max nice priority allowed to raise to values: [-20, 19]
        - rtprio - max realtime priority
        - chroot - change root to directory (Debian-specific)

文件描述符的最大值(永久)

*      soft    nofile     10240
*      hard    nofile     10240
# reboot
# ulimit -n
10240

系统文件描述符的最大值

# cat /proc/sys/fs/file-max
100367
# echo 100367 > /proc/sys/fs/file-max
posted @   thomas_blog  阅读(568)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示