Skip to content

Instantly share code, notes, and snippets.

@xdevelnet
Forked from shinaisan/getrlimit.c
Created March 12, 2019 22:40
Show Gist options
  • Save xdevelnet/f6f4293f136003dae25bb41f127e0b5d to your computer and use it in GitHub Desktop.
Save xdevelnet/f6f4293f136003dae25bb41f127e0b5d to your computer and use it in GitHub Desktop.
getrlimit sample.
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#define RLIMIT(a) {#a, RLIMIT_##a}
struct {char *s; int resnum;} r[] = {
RLIMIT(CORE), RLIMIT(CPU), RLIMIT(DATA), RLIMIT(FSIZE), RLIMIT(MEMLOCK), RLIMIT(NOFILE),
RLIMIT(NPROC), RLIMIT(RSS), RLIMIT(STACK)
};
void print_rlimit(struct rlimit *r, const char *name) {
size_t cur = r->rlim_cur; // Soft limit
size_t max = r->rlim_max; // Hard limit
printf("RLIMIT_%s :rlim_cur => %zu, :rlim_max => %zu\n", name, cur, max);
}
int main() {
size_t i = 0;
while(i < sizeof(r)/sizeof(r[0])) {
struct rlimit rlim;
getrlimit(r[i].resnum, &rlim);
print_rlimit(&rlim, r[i++].s);
}
return EXIT_SUCCESS;
}
all:
cc --std=c99 -Wall -Werror getrlimit.c -o getrlimit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment