Skip to content

Instantly share code, notes, and snippets.

@maoe
Created July 14, 2010 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maoe/475235 to your computer and use it in GitHub Desktop.
Save maoe/475235 to your computer and use it in GitHub Desktop.
#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct rlimit rlimit_t;
int main(void)
{
rlimit_t *r = (rlimit_t *) malloc(sizeof(struct rlimit));
getrlimit(RLIMIT_NOFILE, r);
printf("FILE: cur %d, max %d\n", (int) r->rlim_cur, (int) r->rlim_max);
rlimit_t *new_r = (rlimit_t *) malloc(sizeof(struct rlimit));
new_r->rlim_cur = (rlim_t) 2048;
new_r->rlim_max = (rlim_t) 2048;
if (setrlimit(RLIMIT_NOFILE, new_r)) {
perror("setrlimit failed");
}
getrlimit(RLIMIT_NOFILE, r);
printf("FILE: cur %d, max %d\n", (int) r->rlim_cur, (int) r->rlim_max);
free(new_r);
free(r);
return 0;
}
@maoe
Copy link
Author

maoe commented Jul 14, 2010

maoe@maoe ~/coding/c/reslimit
$ gcc -Wall -std=c99 reslimit.c 
maoe@maoe ~/coding/c/reslimit
$ ./a.out 
FILE: cur 1024, max 1024
setrlimit falied: Operation not permitted
FILE: cur 1024, max 1024
maoe@maoe ~/coding/c/reslimit
$ sudo ./a.out 
FILE: cur 1024, max 1024
FILE: cur 2048, max 2048

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment