LINUX TIPS AND TRICKS --- May 31, 2000

Published by ITworld.com, the IT problem-solving network
http://www.itworld.com/newsletters

*********************************************************************

Controlling A Process's Resource Limit
by Danny Kalev

The likelihood of a system crash caused by an ill-behaved process is
very small on Linux because each process has well-defined resource
limits and because the kernel usually doesn't allow a process to access
memory outside of its address space. However, an on-the-loose process
might still degrade the system's overall performance if it consumes
scarce resources injudiciously. For example, a process that consumes
huge amounts of memory or opens numerous files simultaneously can effect
the performance of other running processes. To ensure that system's
performance isn't affected by such a wanton process, you can limit its
resource consumption. The kernel constantly monitors a process's
resource consumption and guarantees that it doesn't exceed them. There
are two types of limits: hard limits and soft limits. A process may
reduce its hard limits but it cannot raise them -- only a super user can
raise hard limits. Soft limits are the limits actually enforced by
kernel. They may be equal to or lower than the process's hard limits, 
but they may not exceed them. 

Which resources can you limit? The following are the most important
limits that system administrators and users can set:

- Total CPU time (in seconds) used by the process.

- The maximum number of open files. 
For each open file, the system maintains an entry of file attributes and 
associated I/O buffers. These resources are released when the file is 
closed. By limiting the number of simultaneously open files, you can 
minimize the possibility of I/O bottlenecks.

- The maximum number of child processes. 
This limit controls the number of children a given process can have at 
any time. Note, however, that each child process may spawn its own 
children; therefore, this limit doesn't control the total number of a 
process's descendants. 

- The maximum amount of memory that a process can lock. 
To ensure that a specific region of mapped memory is never swapped, a 
process can explicitly lock it. This limit controls the total size of a 
process's locked memory.

- The maximum amount of RAM used. 
If the process exceeds this limit, memory swapping occurs.

Optimal setting of process limits is one of the crucial factors in
performance tuning and the health of the systems. You can obtain the 
current limits of a process and set them using the system calls 
getrlimit() and setrlimit(), respectively. These functions and their 
related data structures and constants are declared in "sys/resource.h".

Questions? Comments? Join my new Linux Software Development discussion 
on ITworld.com's Linux Forum: 
http://forums.itworld.com/webx?14@@.ee6b652 

About the author
----------------
Danny Kalev is a system analyst and software engineer with more
than 10 years of experience, specializing in C++ and
object-oriented analysis and design on various platforms including
VMS, DOS, Windows, Unix, and Linux. His technical interests involve
code optimization, networking, and distributed computing. He is
also a member of the ANSI C++ standardization committee and the
author of ANSI/ISO C++ Professional Programmer's Handbook (Que,
1999). Contact him at linuxnl@excite.com.