C語言系統資源控制(getrlimit && setrlimit)

來源:互聯網
上載者:User

每一個進程都有自己的一組資源限制,在(*)inux系統中我們可以通過
#include <sys/resource.h>
int getrlimit(int resource, struct rlimit *rlim);
int setrlimit(int resource, const struct rlimit *rlim);
這2個API來取得和設定資源
getrlimit用來取得setrlimit用來設定 這二個參數都需要一個要控制的資源 比如控制CPU、記憶體、檔案描述符個數等等的控制,作為第一個參數傳入,第二個參數是一個rlimit的結構體地址(指標),他的結構如下定義:
定義放在標頭檔/usr/include/bits/resource.h中
struct rlimit
       {
         /* The current (soft) limit.       */
         rlim_t rlim_cur;
         /* The hard limit.       */
         rlim_t rlim_max;
       };
結構體中 rlim_cur是要取得或設定的資源軟式節流的值,rlim_max是硬限制
這兩個值的設定有一個小的約束:
1) 任何進程可以將軟式節流改為小於或等於硬限制
2) 任何進程都可以將硬限制降低,但普通使用者降低了就無法提高,該值必須等於或大於軟式節流
3) 只有超級使用者可以提高硬限制
一個無限的限制由常量RLIM_INFINITY指定(The       value       RLIM_INFINITY       denotes no limit on a resource )

RLIMIT_AS
                   The       maximum       size       of       the       process鈙       virtual memory (address
                   space) in bytes.       This limit affects calls to       brk(2),       mmap(2)
                   and       mremap(2), which fail with the error ENOMEM upon exceeding
                   this limit. Also automatic stack expansion will fail (and       gen-
                   erate       a SIGSEGV that kills the process when no alternate stack
                   has been made available).        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       cre-
                   ated.       When nonzero, larger dumps are truncated to this size.
設定最大的core檔案,當值為0時將禁止core檔案非0時將設定產生的最大core檔案大小為設定的值
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 sent SIGXCPU       once       per       second       until       the       hard       limit       is
                   reached,       at which time it is sent SIGKILL.       (This latter point
                   describes Linux 2.2 and 2.4 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.)
CPU時間的最大量值(秒),當超過此軟式節流時向該進程發送SIGXCPU訊號
RLIMIT_DATA

                   The maximum size of the       process鈙       data       segment       (initialized
                   data,       uninitialized data, and heap).       This limit affects calls
                   to brk() 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 SIGXFSZ signal.       By default, this signal terminates a pro-
                   cess, but a process can catch this       signal       instead,       in       which
                   case the relevant system call (e.g., write(), truncate()) fails
                   with the error EFBIG.
可以建立的檔案的最大位元組長度,當超過此軟式節流時向進程發送SIGXFSZ
RLIMIT_MEMLOCK

                   The maximum number of bytes       of       virtual       memory       that       may       be
                   locked into RAM using mlock() and mlockall().
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.
每個進程能夠開啟的最多檔案數。更改此限制將影響到sysconf函數在參數_SC_CHILD_MAX中的傳回值
RLIMIT_OFILE is the BSD name for RLIMIT_NOFILE.
這裡BSD系統中RLIMIT_NOFILE的別名
RLIMIT_NPROC

                   The maximum number of processes that can       be       created       for       the
                   real       user       ID       of the calling process.       Upon encountering this
                   limit, fork() fails with the error EAGAIN.
每個實際使用者ID所擁有的最大子進程數,更改此限制將影響到sysconf函數在參數_SC_CHILD_MAX中返回的值
RLIMIT_RSS

                   Specifies the limit (in pages) of the       process鈙       resident       set
                   (the number of virtual pages resident in RAM).       This limit only
                   has effect in Linux 2.4 onwatrds, and there only affects       calls
                   to madvise() specifying MADVISE_WILLNEED.
最大駐記憶體集位元組長度(RSS)如果實體儲存體器供不應求則核心將從進程處取回超過RSS的部份
RLIMIT_STACK

                   The maximum size of the process stack, in bytes.       Upon reaching
                   this limit, a SIGSEGV signal is generated.       To handle this sig-
                   nal,       a       process must employ an alternate signal stack (sigalt-
                   stack(2)).
棧的最大長度
RLIMIT——VMEM 可映照地址空間的最大位元組長茺,這影響到mmap函數
這些限制影響到調用進程並由子進程繼承! 可以在SHELL中預設這些值ulimit命令設定

小試牛刀(代碼只是示範,並沒有作錯誤處理)
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/resource.h>
int main(void)
{
     struct rlimit r;
     if(getrlimit(RLIMIT_NOFILE,&r)<0)
     {
         fprintf(stderr,"getrlimit error/n");
         exit(1);
     }
     printf("RLIMIT_NOFILE cur:%d/n",r.rlim_cur);
     printf("RLIMIT_NOFILE max:%d/n",r.rlim_max);

     /** set limit **/
     r.rlim_cur=100;
     r.rlim_max=200;
     if (setrlimit(RLIMIT_NOFILE,&r)<0)
     {
         fprintf(stderr,"setrlimit error/n");
         exit(1);
     }

     /** get value of set **/
     if(getrlimit(RLIMIT_NOFILE,&r)<0)
     {
         fprintf(stderr,"getrlimit error/n");
         exit(1);
     }
     printf("RLIMIT_NOFILE cur:%d/n",r.rlim_cur);
     printf("RLIMIT_NOFILE max:%d/n",r.rlim_max);
     return 0;
}
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
:!gcc test.c                                                                                                                                                    

:!./a.out
RLIMIT_NOFILE cur:1024
RLIMIT_NOFILE max:1024
RLIMIT_NOFILE cur:100
RLIMIT_NOFILE max:200

 

 

原帖出自 http://hi.baidu.com/phps/blog/item/7e3ba44410cf9580b3b7dc81.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.