Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 2: Process Management and Scheduling


Linux provides theresource limit(rlimit) mechanism to impose certain system resource usage limits on
processes. The mechanism makes use of therlimarray intask_struct, whose elements are of thestruct
rlimittype.


<resource.h>
struct rlimit {
unsigned long rlim_cur;
unsigned long rlim_max;
}

The definition is purposely kept very general so that it can accept many different resource types.


❑ rlim_curis the current resource limit for the process. It is also referred to as thesoft limit.
❑ rlim_maxis the maximum allowed value for the limit. It is therefore also referred to as the
hard limit.

Thesetrlimitsystem call is used to increase or decrease the current limit. However, the value specified
inrlim_maxmay not be exceeded.getrlimitsis used to check the current limit.


The limitable resources are identified by reference to their position in therlimarray, which is why the
kernel defines pre-processor constants to associate resource and position. Table 2-1 lists the possible
constants and their meanings. Textbooks on system programming provide detailed explanations on
best use of the various limits in practice, and the manual pagesetrlimit(2)contains more detailed
descriptions of all limits.


The numeric values differ between architectures because Linux tries to establish
binary compatibility with the specific nativeUnixsystems.

Because the limits relate to very different parts of the kernel, the kernel must check that the limits are
observed in the corresponding subsystems. This is why we encounter rlimit time and time again in later
chapters of this book.


If a resource type may be used without limits (the default setting for almost all resources),RLIM_INFINITY
is used as the value forrlim_max. Exceptions are, among others:


❑ The number of open files (RLIMIT_NOFILE, limited to 1,024 by default).
❑ The maximum number of processes per user (RLIMIT_NPROC), defined asmax_threads/2.
max_threadsis a global variable whose value specifies how many threads may be generated so
that an eighth of available RAM is used only for management of thread information, given a
minimum possible memory usage of 20 threads.

The boot-time limits for theinittask are defined inINIT_RLIMITSininclude/asm-generic-resource.h.


Notice that kernel 2.6.25, which was still under development when this book was written, will contain
one file per process in the proc filesystem, which allows for inspecting the current rlimit values:


wolfgang@meitner>cat /proc/self/limits
Limit Soft Limit Hard Limit Units
Free download pdf