APUE course source code
guowenxue
2 days ago 9c22371ef5059a2e46226ee90a0667ffad65b574
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*********************************************************************************
 *      Copyright:  (C) 2025 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  limit.c
 *    Description:  This file is setrlimit() example program.
 *
 *        Version:  1.0.0(10/28/2025)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "10/28/2025 10:40:37 AM"
 *
 ********************************************************************************/
 
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
 
int main(void)
{
    struct rlimit limit;
 
    /* 获取当前限制 */
    getrlimit(RLIMIT_NOFILE, &limit);
    printf("Max. fd limite: soft=%ld, hard=%ld\n", limit.rlim_cur, limit.rlim_max);
 
    /* 修改软限制为最多可打开的文件描述个数(硬限制)  */
    limit.rlim_cur = limit.rlim_max;
 
    /* 设置当前限制 */
    if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
        perror("setrlimit");
        exit(EXIT_FAILURE);
    }
 
    printf("Max. fd set to %ld successfully!\n", limit.rlim_cur);
 
    return 0;
}