Previous Table of Contents Next


Module 146
ulimit (ksh) / limit (csh)

DESCRIPTION

The internal ulimit command sets an automatic resource limit. The limit applies to the current process and all processes it spawns. The limit is set as a maximum number of resources a process can have. The ulimit command allows you to:

*  Set the maximum size of a core file.
*  Set the maximum size of data segment or heap.
*  Set the maximum size of a file you can create (a user can only reduce the size).
*  Set the maximum file descriptor.
*  Set the maximum size of stack segments.
*  Set the maximum CPU time allowed.
*  Set the maximum size of virtual memory.
*  Display the current maximum size limit of any of the above.

C Shell
The csh version of ulimit, named limit, allows the same type of resource control as the ksh's ulimit.These limits can be removed by using the unlimit command.

COMMAND FORMAT

Following is the general format of the ulimit command.

     ulimit [ -[HS]a|cdfnstv ]
     ulimit [ -[HS]c|d|f|n|s|t|v ] limit
     ulimit [ limit ]

C Shell
limit [ -h ] [ resource [ limit ] ]
unlimit [ -h ] [ resource [ limit ] ]

Options

The following options may be used to control how ulimit functions. If the limit argument is specified, then the options perform as described; otherwise, they display the current limits.

-H Set or display hard limits. A user cannot increase hard limits.
-S Set or display soft limits. A user can increase or decrease soft limits. The maximum soft limit must be less than the hard limit value.
-a Display all limits.
-c Sets the size of the core file to limit kilobytes.
-d Sets the size of the data area to limit kilobytes.
-f Set the size limit for files written by the current shell and its child processes to n blocks. The largest file you can create is n kilobytes.
-s Sets the size of the stack space to limit kilobytes.
-t Sets each process to a specified cpu time limit of limit seconds.
-v Sets the size of virtural memory to limit kilobytes.
If no option is given and limit is provided, the -f option is assumed.
If no option is given and limit is not provided, then the current ulimit for -f (file size) is displayed.

C Shell
-h Sets the hard limits instead of the soft limits. Only a priviledged user (root) can set hard limits.

Arguments

Only one argument may be passed to the ulimit command.

limit Set the file size limit to n blocks for all files created by the current shell and its child processes.

C Shell
Where resource is one of the following.
cputime Same as ksh -t
filesize Same as ksh -f
datasize Same as ksh -d
stacksize Same as ksh -s
coredumpsize Same as ksh -c
The limit can be scaled in the csh. The scaling factors are:
limith Hours (cpu time only)
limitk Kilobytes
limitm Megabytes
mm:ss Minutes and seconds (cpu time only)

If no arguments are provided the current limits are displayed.

APPLICATIONS

The ulimit command is used to control resource limits. It is beneficial in a testing environment, where you may have a program that enters an endless loop while writing to a file. By having ulimit set you can only write n number of blocks to disk before the system kills (terminates) your program. It may also be used in application programs to control the size of files a user may generate while using the application.

Usually it is used to reduce the maximum file size for users. The super-user can use ulimit to increase the upper limit of file sizes if necessary.

TYPICAL OPERATION

In this activity you use the ulimit command to set your maximum file size to one. Begin at the shell prompt.


C Shell
If you are using the csh, replace the ulimit command with limit f in the following exercises.

Bourne and Korn Shell Only (sh and ksh)
Since the ulimit command resets the maximum size file you can create, you should enter a subshell. This will keep ulimit from changing your login shells file size limit.
To do this type ksh and press Return.

1.  First find out what your current file size limit is by typing ulimit (limit f in csh) and pressing Return.
2.  Type ulimit 1 (limit f 1 in csh) and and press Return to set your maximum file size to one block.
3.  Now type cat /etc/passwd > passwd and press Return. Notice the cat command displays a message about the number of blocks written and an output error. The maximum size your copy of the password file can attain is one block. If your system's passwd file is less than one block in size you will not see this error.
4.  List out the size of each password file by typing ls -l /etc/passwd passwd and pressing Return.
  cj> ls -l /etc/passwd passwd
  -rw-r--r--  1   root     root    8197  Jan  1 17:23 /etc/passwd
  -rw-r--r--  1   mylogin  ts       512  Jan  1 17:23 passwd

Notice the file size in column five. The /etc/passwd file is 8197 bytes, while the new password file is only 512 bytes (1 block). The first 1 block of data was copied and the remainder was discarded.

Bourne and Korn Shell Only (sh and ksh)
Return to your login shell by typing exit and pressing Return or by pressing Ctrl-D.

5.  Type ulimit (limit f in csh) and press Return to see your original ulimit setting.
6.  Turn to Module 147 to continue the learning sequence.


Previous Table of Contents Next