C语言之进程对资源使用的限制

core文件的最大字节数

#include <sys/resource.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    //在Linux系统中,Resouce limit指在一个进程的执行过程中,它所能得到的资源的限制,比如进程的corefile的最大值,虚拟内存的最大值等。
    struct rlimit rlim_new;
    struct rlimit rlim;

    //getrlimit查询设定最大的core文件,若成功返回值为0
    if (getrlimit(RLIMIT_CORE, &rlim)==0) {
        printf("old soft limit:%d\n", rlim.rlim_cur);
        printf("old hard limit:%d\n", rlim.rlim_max);
        //一个无限的限制由常量RLIM_INFINITY指定
        //rlim_t rlim_cur; /*当前(软)限制*/
        //rlim_t rlim_max; /*硬限制*/
        rlim_new.rlim_cur = rlim_new.rlim_max = RLIM_INFINITY;
        if (setrlimit(RLIMIT_CORE, &rlim_new)!=0) {
            /* failed. try raising just to the old max */
            rlim_new.rlim_cur = rlim_new.rlim_max = rlim.rlim_max;
            (void) setrlimit(RLIMIT_CORE, &rlim_new);
        }
    }

    if ((getrlimit(RLIMIT_CORE, &rlim)!=0) || rlim.rlim_cur==0) {
        exit(1);
    }
    printf("new soft limit:%d\n", rlim.rlim_cur);
    printf("new hard limit:%d\n", rlim.rlim_max);
}

 

Tags: c语言

上一篇: C语言之忽略SIGPIPE信号   下一篇: C语言之IP处理函数

提交疑问

回顶部