Saturday, October 24, 2009

Resource Limits in Linux

Linux provides two system calls to control the maximum system resource consumption. They are setrlimits and getrlimits.


getrlimit() and setrlimit() get and set resource limits respectively. Each resource has an associated soft and hard limit, as defined by the rlimit structure (the rlim argument to both getrlimit() and setrlimit()):


struct rlimit {

rlim_t rlim_cur; /* Soft limit */

rlim_t rlim_max; /* Hard limit (ceiling for rlim_cur) */

};


The soft limit is the value that the kernel enforces for the corresponding resource. The hard limit acts as a ceiling for the soft limit: an unprivileged process may only set its soft limit to a value in the range from 0 up to the hard limit, and (irreversibly) lower its hard limit. A privileged process may make arbitrary changes to either limit value.


Format of the system calls:
int getrlimit(int resource, struct rlimit *rlim);

int setrlimit(int resource, const struct rlimit *rlim);


Usage:
{

struct rlimit lim;
if(getrlimit(RLIMIT_STACK, &lim) <>

{

perror("getrlimit");

exit(1);

}
}

These resource limits could also be set from the shell for the processes started by the shell using the shell command uname.


The resources (RLIMIT_) that could be controlled are:

  • Maximum size of the core file. (RLIMIT_CORE) When 0 no core dump files are created. When non-zero, larger dumps are truncated to this size.
  • Maximum size of the process’s data segment. This includes initialized data, uninitialized data, and heap. (RLIMIT_DATA).
  • Maximum scheduling priority (RLIMIT_NICE nice value).
  • Maximum size of the file that the process may create (RLIMIT_FSIZE). Attempts to extend a file beyond this limit result in delivery of a SIGXFSZ signal.
  • Maximum number of pending signals (RLIMIT_SIGPENDING).
  • Maximum size that may be locked into RAM. In effect this limit will round to the nearest multiple of system page size (RLIMIT_MEMLOCK).
  • Maximum resident set size (RLIMIT_RSS).
  • Maximum number of open file descriptors (RLIMIT_NOFILE). Pipe size in 512-byte blocks
  • Maximum number of bytes in POSIX message queues.( RLIMIT_MSGQUEUE)Maximum rt priority (RLIMIT_RTPRIO).Maximum stack size (RLIMIT_STACK).
  • Maximum amount of cpu time in seconds. When the process reaches the soft limit, it is sent a SIGXCPU signal. The default action for this signal is to terminate the process (RLIMIT_CPU).
  • Maximum number of processes available to a single user (RLIMIT_NPROC)
  • Maximum amount of virtual memory in bytes. (RLIMIT_AS)Maximum number of file locks (RLIMIT_LOCKS).

No comments:

Post a Comment