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 @ 2017-05-16 19:51  thomas_blog  阅读(565)  评论(0编辑  收藏  举报