老规矩,祭出man手册。学习man手册中的内容:man 2 getrlimit
The getrlimit() and setrlimit() system calls can be used to get and set the resource limits such as files, CPU, memory etc. associated with a process.

Each resource has an associated soft and hard limit.

soft limit: The soft limit is the actual limit enforced by the kernel for the corresponding resource.
hard limit: The hard limit acts as a ceiling for the soft limit.
The soft limit ranges in between 0 and hard limit.

实际例子:

// C program to demonstrate working of getrlimit() 
// and setlimit() 
#include <stdio.h> 
#include <sys/resource.h> 
#include <string.h> 
#include <errno.h> 
#include <unistd.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 

int main() { 

    struct rlimit old_lim, lim, new_lim; 

    // Get old limits 
    if( getrlimit(RLIMIT_NOFILE, &old_lim) == 0) 
        printf("Old limits -> soft limit= %ld \t"
        " hard limit= %ld \n", old_lim.rlim_cur, 
                                old_lim.rlim_max); 
    else
        fprintf(stderr, "%s\n", strerror(errno)); 
    
    // Set new value 
    lim.rlim_cur = 3; 
    lim.rlim_max = 1024; 

    // Set limits 
    if(setrlimit(RLIMIT_NOFILE, &lim) == -1) 
        fprintf(stderr, "%s\n", strerror(errno)); 
    
    // Get new limits 
    if( getrlimit(RLIMIT_NOFILE, &new_lim) == 0) 
        printf("New limits -> soft limit= %ld "
        "\t hard limit= %ld \n", new_lim.rlim_cur, 
                                new_lim.rlim_max); 
    else
        fprintf(stderr, "%s\n", strerror(errno)); 
    return 0; 
} 

运行结果:

[root gcc]#./getrlimit
Old limits -> soft limit= 1024   hard limit= 1048576
New limits -> soft limit= 3      hard limit= 1024
[root gcc]#
// C program to demonstrate error when a 
// process tries to access resources beyond 
// limit. 
#include <stdio.h> 
#include <sys/resource.h> 
#include <string.h> 
#include <errno.h> 
#include <unistd.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 

int main() { 

    struct rlimit old_lim, lim, new_lim; 

    // Get old limits 
    if( getrlimit(RLIMIT_NOFILE, &old_lim) == 0) 
        printf("Old limits -> soft limit= %ld \t"
        " hard limit= %ld \n", old_lim.rlim_cur, 
                            old_lim.rlim_max); 
    else
        fprintf(stderr, "%s\n", strerror(errno)); 

    // Set new value 
    lim.rlim_cur = 3; 
    lim.rlim_max = 1024; 


    // Set limits 
    if(setrlimit(RLIMIT_NOFILE, &lim) == -1) 
        fprintf(stderr, "%s\n", strerror(errno)); 
    
    // Get new limits 
    if( getrlimit(RLIMIT_NOFILE, &new_lim) == 0) 
        printf("New limits -> soft limit= %ld \t"
        " hard limit= %ld \n", new_lim.rlim_cur, 
                                new_lim.rlim_max); 
    else
        fprintf(stderr, "%s\n", strerror(errno)); 
    
    // Try to open a new file 
    if(open("foo.txt", O_WRONLY | O_CREAT, 0) == -1) 
        fprintf(stderr, "%s\n", strerror(errno)); 
    else
            printf("Opened successfully\n"); 
    
    return 0; 
} 

运行结果:

Old limits -> soft limit= 1024   hard limit= 1048576
New limits -> soft limit= 3      hard limit= 1024
Too many open files
[root gcc]#

因为我们将进程能打开的文件数目设置为3,由于已经打开了三个(标准输入 0,标准输出1,标准错误2),因此会提示打开文件超过上限,对应的错误码是:EMFILE