21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

limits are usually set much higher than they need to be, which ends up allowing any<br />

given application to use up far more resources than it ordinarily should.<br />

Unix provides a mechanism by which an application can self-impose restrictive limits<br />

on the resources that it uses. It’s a good idea for the programmer to lower the limits<br />

to a point where the application can run comfortably, but if something<br />

unexpected happens (such as a memory leak or, more to the point, a denial of service<br />

attack), the limits cause the application to begin failing without bringing down<br />

the rest of the system with it.<br />

Discussion<br />

Operating system resources are difficult for an application to control; the pooling<br />

approach used in threads and sockets is difficult to implement when the application<br />

does not explicitly allocate and destroy its own resources. System resources such as<br />

memory, CPU time, disk space, and open file descriptors are best managed using system<br />

quotas. The programmer can never be sure that system quotas are enabled when<br />

the application is running; therefore, it pays to be defensive and to write code that is<br />

reasonably aware of system resource management.<br />

The most basic advice will be long familiar from lectures on good programming practice:<br />

• Avoid the use of system calls when possible.<br />

• Minimize the number of filesystem reads and writes.<br />

• Steer away from CPU-intensive or “tight” loops.<br />

• Avoid allocating large buffers on the stack.<br />

The ambitious programmer may wish to replace library and operating system<br />

resource management subsystems, by such means as writing a memory allocator that<br />

enforces a maximum memory usage per thread, or writing a scheduler tied to the system<br />

clock which pauses or stops threads and processes with SIGSTOP signals after a<br />

specified period of time. While these are viable solutions and should be considered<br />

for any large-scale project, they greatly increase development time and are likely to<br />

introduce new bugs into the system.<br />

Instead, you may wish to voluntarily submit to the resource limits enforced by system<br />

quotas, thereby in effect “enabling” quotas for the application. This can be done<br />

with the setrlimit( ) function, which allows the resources listed in Table 13-1 to be<br />

limited. Note, however, that not all systems implement all resource limits listed in<br />

this table. Exceeding any of these limits will cause runtime errors such as ENOMEM<br />

when attempting to allocate memory after RLIMIT_DATA has been reached. On BSDderived<br />

systems, two exceptions are RLIMIT_CPU and RLIMIT_FSIZE, which raise the<br />

SIGXCPU and SIGXFSZ signals, respectively.<br />

728 | Chapter 13: Other Topics<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!