鍍金池/ 教程/ Linux/ getrlimit()函數(shù) Unix/Linux
outb()函數(shù) Unix/Linux
getpeername()函數(shù) Unix/Linux
io_setup()函數(shù) Unix/Linux
mknod()函數(shù) Unix/Linux
arch_prctl()函數(shù) Unix/Linux
inotify_add_watch()函數(shù) Unix/Linux
chmod()函數(shù) Unix/Linux
_exit()函數(shù) Unix/Linux
epoll_ctl()函數(shù) Unix/Linux
Unix/Linux系統(tǒng)調(diào)用
futimesat()函數(shù) Unix/Linux
oldolduname()函數(shù) Unix/Linux
sched_rr_get_interval()函數(shù) Unix/Linux
oldstat()函數(shù) Unix/Linux
pselect()函數(shù) Unix/Linux
close()函數(shù) Unix/Linux
add_key()函數(shù) Unix/Linux
clone()函數(shù) Unix/Linux
rename()函數(shù) Unix/Linux
msgop()函數(shù) Unix/Linux
getrlimit()函數(shù) Unix/Linux
linkat()函數(shù) Unix/Linux
ioprio_set()函數(shù) Unix/Linux
insw()函數(shù) Unix/Linux
munlock()函數(shù) Unix/Linux
getgid()函數(shù) Unix/Linux
chdir()函數(shù) Unix/Linux
rmdir()函數(shù) Unix/Linux
ioctl()函數(shù) Unix/Linux
lchown()函數(shù) Unix/Linux
execve()函數(shù) Unix/Linux
epoll_wait()函數(shù) Unix/Linux
fstatat()函數(shù) Unix/Linux
truncate()函數(shù) Unix/Linux
lock()函數(shù) Unix/Linux
outsb()函數(shù) Unix/Linux
chroot()函數(shù) Unix/Linux
kexec_load()函數(shù) Unix/Linux
afs_syscall()函數(shù) Unix/Linux
dup2()函數(shù) Unix/Linux
_newselect()函數(shù) Unix/Linux
inotify_rm_watch()函數(shù) Unix/Linux
recv()函數(shù) Unix/Linux
msgsnd()函數(shù) Unix/Linux
getdtablesize()函數(shù) Unix/Linux
ioperm()函數(shù) Unix/Linux
munmap()函數(shù) Unix/Linux
getpriority()函數(shù) Unix/Linux
sched_get_priority_min()函數(shù) Unix/Linux
outw_p()函數(shù) Unix/Linux
kill()函數(shù) Unix/Linux
lookup_dcookie()函數(shù) Unix/Linux
accept()函數(shù) Unix/Linux
obsolete()函數(shù) Unix/Linux
pread()函數(shù) Unix/Linux
getgroups()函數(shù) Unix/Linux
personality()函數(shù) Unix/Linux
recvfrom()函數(shù) Unix/Linux
putpmsg()函數(shù) Unix/Linux
getgroups()函數(shù) Unix/Linux
remap_file_pages()函數(shù) Unix/Linux
request_key()函數(shù) Unix/Linux
mount()函數(shù) Unix/Linux
sched_setscheduler()函數(shù) Unix/Linux
mkdirat()函數(shù) Unix/Linux
nfsservctl()函數(shù) Unix/Linux
getuid()函數(shù) Unix/Linux
prof()函數(shù) Unix/Linux
exit_group函數(shù) Unix/Linux
io_cancel()函數(shù) Unix/Linux
mmap()函數(shù) Unix/Linux
mremap()函數(shù) Unix/Linux
getitimer()函數(shù) Unix/Linux
unimplemented()函數(shù) Unix/Linux
read()函數(shù) Unix/Linux
inl()函數(shù) Unix/Linux
posix_fadvise()函數(shù) Unix/Linux
open()函數(shù) Unix/Linux
listen()函數(shù) Unix/Linux
io_destroy()函數(shù) Unix/Linux
acct()函數(shù) Unix/Linux
fcntl()函數(shù) Unix/Linux
recvmsg()函數(shù) Unix/Linux
statfs()函數(shù) Unix/Linux
oldlstat()函數(shù) Unix/Linux
outb()函數(shù) Unix/Linux
getpagesize()函數(shù) Unix/Linux
sched_yield()函數(shù) Unix/Linux
outw()函數(shù) Unix/Linux
inw_p()函數(shù) Unix/Linux
setpgid()函數(shù) Unix/Linux
quotactl()函數(shù) Unix/Linux
prctl()函數(shù) Unix/Linux
fattach()函數(shù) Unix/Linux
readahead()函數(shù) Unix/Linux
msgget()函數(shù) Unix/Linux
gethostname()函數(shù) Unix/Linux
semctl()函數(shù) Unix/Linux
munlockall()函數(shù) Unix/Linux
iopl()函數(shù) Unix/Linux
fdetach()函數(shù) Unix/Linux
getuid()函數(shù) Unix/Linux
gettid()函數(shù) Unix/Linux
pwrite()函數(shù) Unix/Linux
isastream()函數(shù) Unix/Linux
get_thread_area()函數(shù) Unix/Linux
mpx()函數(shù) Unix/Linux
io_getevents()函數(shù) Unix/Linux
lstat()函數(shù) Unix/Linux
readv()函數(shù) Unix/Linux
exit()函數(shù) Unix/Linux
inw()函數(shù) Unix/Linux
fchmod()函數(shù) Unix/Linux
mq_getsetattr()函數(shù) Unix/Linux
get_robust_list()函數(shù) Unix/Linux
_llseek()函數(shù) Unix/Linux
gettimeofday()函數(shù) Unix/Linux
nice()函數(shù) Unix/Linux
security()函數(shù) Unix/Linux
sched_get_priority_max()函數(shù) Unix/Linux
outb_p()函數(shù) Unix/Linux
intro()函數(shù) Unix/Linux
alarm()函數(shù) Unix/Linux
putmsg()函數(shù) Unix/Linux
fork()函數(shù) Unix/Linux
getpgrp()函數(shù) Unix/Linux
link()函數(shù) Unix/Linux
getcwd()函數(shù) Unix/Linux
insl()函數(shù) Unix/Linux
getsockname()函數(shù) Unix/Linux
dup()函數(shù) Unix/Linux
connect()函數(shù) Unix/Linux
select()函數(shù) Unix/Linux
futex()函數(shù) Unix/Linux
outsw()函數(shù) Unix/Linux
query_module()函數(shù) Unix/Linux
reboot()函數(shù) Unix/Linux
create_module()函數(shù) Unix/Linux
adjtimex()函數(shù) Unix/Linux
mlock()函數(shù) Unix/Linux
statvfs()函數(shù) Unix/Linux
gtty()函數(shù) Unix/Linux
epoll_create()函數(shù) Unix/Linux
bind()函數(shù) Unix/Linux
inl_p()函數(shù) Unix/Linux
select_tut()函數(shù) Unix/Linux
mincore()函數(shù) Unix/Linux
getresuid()函數(shù) Unix/Linux
getpmsg()函數(shù) Unix/Linux
getcontext()函數(shù) Unix/Linux
killpg()函數(shù) Unix/Linux
olduname()函數(shù) Unix/Linux
openat()函數(shù) Unix/Linux
pause()函數(shù) Unix/Linux
alloc_hugepages()函數(shù) Unix/Linux
ioctl_list()函數(shù) Unix/Linux
readlinkat()函數(shù) Unix/Linux
mprotect()函數(shù) Unix/Linux
getdomainname()函數(shù) Unix/Linux
readlink()函數(shù) Unix/Linux
madvise()函數(shù) Unix/Linux
bdflush()函數(shù) Unix/Linux
ipc()函數(shù) Unix/Linux
ptrace()函數(shù) Unix/Linux
getrusage()函數(shù) Unix/Linux
msync()函數(shù) Unix/Linux
faccessat()函數(shù) Unix/Linux
modify_ldt()函數(shù) Unix/Linux
get_kernel_syms()函數(shù) Unix/Linux
getpid()函數(shù) Unix/Linux
fsync()函數(shù) Unix/Linux
msgctl()函數(shù) Unix/Linux
break未實(shí)現(xiàn) Unix/Linux
sched_setparam()函數(shù) Unix/Linux
fchown()函數(shù) Unix/Linux
cacheflush()函數(shù) Unix/Linux
fdatasync()函數(shù) Unix/Linux
flock()函數(shù) Unix/Linux
pivot_root()函數(shù) Unix/Linux
inotify_init()函數(shù) Unix/Linux
fchmodat()函數(shù) Unix/Linux
keyctl()函數(shù) Unix/Linux
llseek()函數(shù) Unix/Linux
outb()函數(shù) Unix/Linux
pipe()函數(shù) Unix/Linux
io_submit()函數(shù) Unix/Linux
ppoll()函數(shù) Unix/Linux
multiplexer()函數(shù) Unix/Linux
access()函數(shù) Unix/Linux
sched_getparam()函數(shù) Unix/Linux
chown()函數(shù) Unix/Linux
readdir()函數(shù) Unix/Linux
inb_p()函數(shù) Unix/Linux
outsl()函數(shù) Unix/Linux
nanosleep()函數(shù) Unix/Linux
alloc_hugepages()函數(shù) Unix/Linux
sbrk()函數(shù) Unix/Linux
move_pages()函數(shù) Unix/Linux
fchdir()函數(shù) Unix/Linux
renameat()函數(shù) Unix/Linux
getppid()函數(shù) Unix/Linux
getsid()函數(shù) Unix/Linux
oldfstat()函數(shù) Unix/Linux
lseek()函數(shù) Unix/Linux
stat()函數(shù) Unix/Linux
path_resolution()函數(shù) Unix/Linux
getunwind()函數(shù) Unix/Linux
getsockopt()函數(shù) Unix/Linux
mmap2()函數(shù) Unix/Linux
getdents()函數(shù) Unix/Linux
fchownat()函數(shù) Unix/Linux
mkdir()函數(shù) Unix/Linux
perfmonctl()函數(shù) Unix/Linux
open()函數(shù) Unix/Linux
idle()函數(shù) Unix/Linux
poll()函數(shù) Unix/Linux
brk()函數(shù) Unix/Linux
sched_setaffinity()函數(shù) Unix/Linux
mlockall()函數(shù) Unix/Linux

getrlimit()函數(shù) Unix/Linux

getrlimit, setrlimit - 獲取/設(shè)置資源限制

內(nèi)容簡(jiǎn)介

#include <sys/time.h> 
#include <sys/resource.h>

int getrlimit(int resource, struct rlimit *rlim); 
int setrlimit(int resource, const struct rlimit *rlim);

描述

getrlimit() 和setrlimit() 獲取和分別設(shè)置資源限制。每個(gè)資源都有一個(gè)相關(guān)的軟,硬限制,由rlimit 結(jié)構(gòu)(rlim 參數(shù)兩者之定義 getrlimit() 和 setrlimit()):
struct rlimit {
    rlim_t rlim_cur;  /* Soft limit */
    rlim_t rlim_max;  /* Hard limit (ceiling for rlim_cur) */
};
The soft limit is the value that the kernel enforces for the corresponding resource. The hard limit acts as a ceiling for the soft limit: an unprivileged process may only set its soft limit to a value in the range from 0 up to the hard limit, and (irreversibly) lower its hard limit. A privileged process (under Linux: one with the CAP_SYS_RESOURCE capability) may make arbitrary changes to either limit value.

The value RLIM_INFINITY denotes no limit on a resource (both in the structure returned by getrlimit() and in the structure passed to setrlimit()).

resource must be one of:

標(biāo)簽 描述
RLIMIT_AS
  The maximum size of the process’s virtual memory (address space) in bytes. This limit affects calls to brk(2), mmap(2) andmremap(2), which fail with the error ENOMEM upon exceeding this limit. Also automatic stack expansion will fail (and generate a SIGSEGV that kills the process if no alternate stack has been made available via sigaltstack(2)). Since the value is a long, on machines with a 32-bit long either this limit is at most 2 GiB, or this resource is unlimited.
RLIMIT_CORE
  Maximum size of core file. When 0 no core dump files are created. When non-zero, larger dumps are truncated to this size.
RLIMIT_CPU
  CPU time limit in seconds. When the process reaches the soft limit, it is sent a SIGXCPU signal. The default action for this signal is to terminate the process. However, the signal can be caught, and the handler can return control to the main program. If the process continues to consume CPU time, it will be sentSIGXCPU once per second until the hard limit is reached, at which time it is sent SIGKILL. (This latter point describes Linux 2.2 through 2.6 behaviour. Implementations vary in how they treat processes which continue to consume CPU time after reaching the soft limit. Portable applications that need to catch this signal should perform an orderly termination upon first receipt of SIGXCPU.)
RLIMIT_DATA
  The maximum size of the process’s data segment (initialized data, uninitialized data, and heap). This limit affects calls tobrk() and sbrk(), which fail with the error ENOMEM upon encountering the soft limit of this resource.
RLIMIT_FSIZE
  The maximum size of files that the process may create. Attempts to extend a file beyond this limit result in delivery of a SIGXFSZsignal. By default, this signal terminates a process, but a process can catch this signal instead, in which case the relevant system call (e.g., write() truncate()) fails with the error EFBIG.
RLIMIT_LOCKS (Early Linux 2.4 only)
  A limit on the combined number of flock() locks and fcntl() leases that this process may establish.
RLIMIT_MEMLOCK
  The maximum number of bytes of memory that may be locked into RAM. In effect this limit is rounded down to the nearest multiple of the system page size. This limit affects mlock(2) andmlockall(2) and the mmap(2) MAP_LOCKED operation. Since Linux 2.6.9 it also affects the shmctl(2) SHM_LOCK operation, where it sets a maximum on the total bytes in shared memory segments (see shmget(2)) that may be locked by the real user ID of the calling process. The shmctl(2) SHM_LOCK locks are accounted for separately from the per-process memory locks established by mlock(2), mlockall(2), and mmap(2)MAP_LOCKED; a process can lock bytes up to this limit in each of these two categories. In Linux kernels before 2.6.9, this limit controlled the amount of memory that could be locked by a privileged process. Since Linux 2.6.9, no limits are placed on the amount of memory that a privileged process may lock, and this limit instead governs the amount of memory that an unprivileged process may lock.
RLIMIT_MSGQUEUE (Since Linux 2.6.8)
  Specifies the limit on the number of bytes that can be allocated for POSIX message queues for the real user ID of the calling process. This limit is enforced for mq_open(3). Each message queue that the user creates counts (until it is removed) against this limit according to the formula:
	

bytes = attr.mq_maxmsg * sizeof(struct msg_msg *) + attr.mq_maxmsg * attr.mq_msgsize

 

where attr is the mq_attr structure specified as the fourth argument to mq_open().

The first addend in the formula, which includes sizeof(struct msg_msg *) (4 bytes on Linux/x86), ensures that the user cannot create an unlimited number of zero-length messages (such messages nevertheless each consume some system memory for bookkeeping overhead).

RLIMIT_NICE (since kernel 2.6.12, but see BUGS below)
  Specifies a ceiling to which the process’s nice value can be raised using setpriority(2) or nice(2). The actual ceiling for the nice value is calculated as 20 - rlim_cur. (This strangeness occurs because negative numbers cannot be specified as resource limit values, since they typically have special meanings. For example, RLIM_INFINITY typically is the same as -1.)
RLIMIT_NOFILE
  Specifies a value one greater than the maximum file descriptor number that can be opened by this process. Attempts (open(),pipe(), dup(), etc.) to exceed this limit yield the error EMFILE.
RLIMIT_NPROC
  The maximum number of threads that can be created for the real user ID of the calling process. Upon encountering this limit,fork() fails with the error EAGAIN.
RLIMIT_RSS
  Specifies the limit (in pages) of the process’s resident set (the number of virtual pages resident in RAM). This limit only has effect in Linux 2.4.x, x < 30, and there only affects calls tomadvise() specifying MADV_WILLNEED.
RLIMIT_RTPRIO (Since Linux 2.6.12, but see BUGS)
  Specifies a ceiling on the real-time priority that may be set for this process using sched_setscheduler(2) andsched_setparam(2).
RLIMIT_SIGPENDING (Since Linux 2.6.8)
  Specifies the limit on the number of signals that may be queued for the real user ID of the calling process. Both standard and real-time signals are counted for the purpose of checking this limit. However, the limit is only enforced for sigqueue(2); it is always possible to use kill(2) to queue one instance of any of the signals that are not already queued to the process.
RLIMIT_STACK
  The maximum size of the process stack, in bytes. Upon reaching this limit, a SIGSEGV signal is generated. To handle this signal, a process must employ an alternate signal stack (sigaltstack(2)).
RLIMIT_OFILE is the BSD name for RLIMIT_NOFILE.

返回值

On success, zero is returned. On error, -1 is returned, and errno is set appropriately.

錯(cuò)誤

標(biāo)簽 描述
EFAULT rlim points outside the accessible address space.
EINVAL resource is not valid; or, for setrlimit(): rlim->rlim_cur was greater than rlim->rlim_max.
EPERM An unprivileged process tried to use setrlimit() to increase a soft or hard limit above the current hard limit; theCAP_SYS_RESOURCE capability is required to do this. Or, the process tried to use setrlimit() to increase the soft or hard RLIMIT_NOFILE limit above the current kernel maximum (NR_OPEN).

BUGS

In older Linux kernels, the SIGXCPU and SIGKILL signals delivered when a process encountered the soft and hard RLIMIT_CPU limits were delivered one (CPU) second later than they should have been. This was fixed in kernel 2.6.8.

In 2.6.x kernels before 2.6.17, a RLIMIT_CPU limit of 0 is wrongly treated as "no limit" (like RLIM_INFINITY). Since kernel 2.6.17, setting a limit of 0 does have an effect, but is actually treated as a limit of 1 second.

A kernel bug means that RLIMIT_RTPRIO does not work in kernel 2.6.12; the problem is fixed in kernel 2.6.13.

In kernel 2.6.12, there was an off-by-one mismatch between the priority ranges returned by getpriority(2) and RLIMIT_NICE. This had the effect that actual ceiling for the nice value was calculated as 19 - rlim_cur. This was fixed in kernel 2.6.13.

Kernels before 2.4.22 did not diagnose the error EINVAL for setrlimit() when rlim->rlim_cur was greater than rlim->rlim_max.

注意

A child process created via fork(2) inherits its parents resource limits. Resource limits are preserved across execve(2).

遵循于

SVr4, 4.3BSD, POSIX.1-2001. RLIMIT_MEMLOCK and RLIMIT_NPROC derive from BSD and are not specified in POSIX.1-2001; they are present on the BSDs and Linux, but on few other implementations. RLIMIT_RSS derives from BSD and is not specified in POSIX.1-2001; it is nevertheless present on most implementations.RLIMIT_MSGQUEUERLIMIT_NICERLIMIT_RTPRIO, and RLIMIT_SIGPENDING are Linux specific.

另請(qǐng)參閱