Add Book to My BookshelfPurchase This Book Online

Chapter 9 - System Configuration and Resource Limits

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

Process Resource Limits
There are also several limits that are applied on a per-process basis. Many of these limits can be changed by the process, and are meant to aid in stopping “runaway” behavior.
All versions of UNIX provide the ulimit system call, although its behavior is slightly different in SVR4:
    #include <ulimit.h>
    long ulimit(int cmd, long newlimit);
The values of cmd are:
UL_GETFSIZE
Returns the maximum file size, in 512-byte block units, that the process may create. Any size file may be read, regardless of the value of this limit.
UL_SETFSIZE
Sets the maximum file size limit to the value in newlimit. Any process may decrease this value, but only a process with superuser permissions may increase it.
UL_GETMEMLIM
Returns the maximum amount of memory the process may use. This command is not available in HP-UX 10.x.
UL_GETMAXBRK
Returns the maximum amount of memory the process may use. This command is only available in HP-UX 10.x.
UL_GETDESLIM
Returns the maximum number of files the process may have open. This command is not available in HP-UX 10.x.
Upon successful completion, ulimit returns a non-negative value. If an error occurs, it returns -1 and sets the external integer errno to describe the error.
A more general interface to limits was first introduced by BSD UNIX, and later adopted by SVR4:
    #include <sys/time.h>
    #include <sys/resource.h>
    int getrlimit(int resource, struct rlimit *rlp);
    int setrlimit(int resource, const struct rlimit *rlp);
Each call to either getrlimit or setrlimit applies to a single resource, identified by resource. There are two limits to each resource, a current (soft) limit, and a maximum (hard) limit. Any process can change soft limits to any value less than or equal to the hard limit. Only a process with superuser permissions may raise the hard limit, but any process may (irreversibly) lower the hard limit. Limits may be specified as “infinity” by setting them to the constant RLIM_INFINITY; in this case, the operating system sets the maximum value.
The rlp parameter is a pointer to a structure of type struct rlimit:
    struct rlimit {
        rlim_t    rlim_cur;
        rlim_t    rlim_max;
    };
The possible resources are:
RLIMIT_CORE
The maximum size of a core file, in bytes, that the process can create. A limit of 0 prevents the creation of a core file. The writing of a core file terminates when this size is reached, even if the file is incomplete.
RLIMIT_CPU
The maximum amount of processor time, in seconds, that the process can use. This is a soft limit only; there is no hard limit. When the limit is exceeded, the system will send the process a SIGXCPU signal (see Chapter 10, Signals).
RLIMIT_DATA
The maximum size of the process' data segment, in bytes. When this limit is reached, calls to malloc and other memory allocation routines will fail. This resource limit is not available in HP-UX 10.x.
RLIMIT_FSIZE
The maximum size of a file, in bytes, that the process can create. A limit of 0 prevents file creation. When this limit is exceeded, the process will receive a SIGXFSZ signal.
RLIMIT_NOFILE
The maximum number of file descriptors (and, hence, open files) that the process may create. When this limit is exceeded, further attempts to open files will fail.
RLIMIT_STACK
The maximum size, in bytes, of the process' stack. The system will not automatically grow the stack beyond this limit. When this limit is reached, the process will receive a SIGSEGV signal. This resource limit is not available in HP-UX 10.x.
RLIMIT_VMEM
The maximum size of the process' mapped address space, in bytes. When a process exceeds this limit, further calls to malloc and other memory allocation functions will fail, as will calls to mmap. Finally, the system will no longer automatically grow the process' stack. This resource limit is not available in HP-UX 10.x.
Upon successful completion, both calls return 0. Otherwise, -1 is returned and errno is set to indicate the error.
Porting Notes
On older versions of UNIX, the ulimit function can only be used to change the maximum file size. It takes a single parameter, the new value of the limit.

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback