-
Notifications
You must be signed in to change notification settings - Fork 1
/
022_resource_limit.c
46 lines (36 loc) · 1.08 KB
/
022_resource_limit.c
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
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <sys/resource.h>
int main() {
struct rlimit rlim;
// Get current file limit
int resource = RLIMIT_NOFILE;
if (getrlimit(resource, &rlim) == -1) {
perror("getrlimit");
return 1;
}
printf("Current soft limit for open files: %ld\n", rlim.rlim_cur);
printf("Current hard limit for open files: %ld\n", rlim.rlim_max);
// Set new soft limit for open files
rlim.rlim_cur = 1024;
if (setrlimit(resource, &rlim) == -1) {
perror("setrlimit");
return 1;
}
printf("New soft limit for open files: %ld\n", rlim.rlim_cur);
// Get current memory limit
resource = RLIMIT_DATA;
if (getrlimit(resource, &rlim) == -1) {
perror("getrlimit");
return 1;
}
printf("Current soft limit for memory: %ld bytes\n", rlim.rlim_cur);
printf("Current hard limit for memory: %ld bytes\n", rlim.rlim_max);
// Set new soft limit for memory
rlim.rlim_cur = 1024 * 1024; // 1 MB
if (setrlimit(resource, &rlim) == -1) {
perror("setrlimit");
return 1;
}
printf("New soft limit for memory: %ld bytes\n", rlim.rlim_cur);
return 0;
}