Is my ulimit exceeded

From Linuxintro
Revision as of 14:43, 22 May 2015 by imported>ThorstenStaerk (ThorstenStaerk moved page Ulimit to Is my ulimit exceeded)

Ulimit is a bash command that allows you to set and read shell restrictions (limits) like the maximum number of open files that are allowed for the user:

# ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
file size               (blocks, -f) unlimited
pending signals                 (-i) 32768
max locked memory       (kbytes, -l) 32
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 32768
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

You can permanently set the limits in /etc/security/limits.conf. You will have to re-login afterwards. To set the number of file descriptors for all users, the syntax is:

*                hard    nofile           10000
*		 soft    nofile           10000

If you read ulimit's man page take care: ulimit -v is flagged as per-process limit, however -n is also a per-process limit. In the above case, every process can have up to 1024 file descriptors. You can check this with a simple C program that does nothing but open files:

main.c

#include <stdio.h>

int main()
{
  FILE *handle;
  for (int i=1;i<2000;i++)
  {
    if (!(handle=fopen("testfile", "wb"))) {printf("this did not work\n");};
  }
  while (true){};
  char x[1];
  fwrite(x,1,1,handle);
}

Compile this file with the command

g++ main.c 

Run this program (and send it to the background) then with the command

./a.out &

Now find out the process ID:

# ps -A|grep a.out
29232 pts/3    00:04:15 a.out

Now go into the process' file descriptor directory:

cd /proc/29232/fd

And count the number of files:

# ll | wc -l
1025

If you want to, find out if the process has drawn the right limits value:

# cat /proc/29232/limits|grep open
Max open files            1024                 4096                 files