Skip to content

Instantly share code, notes, and snippets.

@jrelo
Last active February 2, 2017 15:04
Show Gist options
  • Save jrelo/c75c2a3fdb55cba1e62fb2475f0401c1 to your computer and use it in GitHub Desktop.
Save jrelo/c75c2a3fdb55cba1e62fb2475f0401c1 to your computer and use it in GitHub Desktop.
NOFILE rlimit get/set/test
#include <sys/resource.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
(gdb) set $rlim = &{0ll, 0ll}
# the number 7 here is important.
(gdb) print getrlimit(7, $rlim)
$1 = 0
(gdb) print *$rlim
$2 = {1024, 4096}
# update the value retrieved above with getrlimit
(gdb) set *$rlim[0] = 1024*4
(gdb) print *$rlim
$3 = {4096, 4096}
(gdb) print setrlimit(7, $rlim)
$4 = 0
*/
int main ()
{
// Define and object of structure
// rlimit.
struct rlimit rl;
// First get the limit on open files
getrlimit (RLIMIT_NOFILE, &rl);
printf("\n Default value is : %lld\n", (long long int)rl.rlim_cur);
// Change the limit
rl.rlim_cur = 4; // 3 are for stdin, stdout, stderr and one extra
// Now call setrlimit() to set the
// changed value.
setrlimit (RLIMIT_NOFILE, &rl);
// Again get the limit and check
getrlimit (RLIMIT_NOFILE, &rl);
printf("\n Default value now is : %lld\n", (long long int)rl.rlim_cur);
// Try opening more than one file
FILE *fp = NULL;
int i=0;
for (i=0; i<2; i++)
{
fp = NULL;
fp = fopen("test.txt","r");
if(NULL == fp)
{
printf("\n fopen failed [%d]\n", i);
return -1;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment