精华内容
下载资源
问答
  • getrlimit

    千次阅读 2012-05-20 09:17:44
     getrlimit,setrlimit - 控制系统资源的最大使用量。[1]‍‍[2] 大纲  #include  int getrlimit(int resource, struct rlimit *rlp);  int setrlimit(int resource, const struct rlimit *rlp);

    nix系统函数

    名称

      getrlimit,setrlimit - 控制系统资源的最大使用量。[1]‍‍[2]

    大纲

      #include <sys/resource.h>
      int getrlimit(int resource, struct rlimit *rlp);
      int setrlimit(int resource, const struct rlimit *rlp);

    描述

      在操作系统中,我们能够通过函数getrlimit()、setrlimit()分别获得、设置每个进程能够创建的各种系统资源的限制使用量。
      调用getrlimit()或者setrlimit()来操作指定资源的操作上限。资源限制是一对值:一个指定了当前(软)限制,另一个则代表一个最大(硬)限制。软限制能够被一个进程改变,只要保证它不大于硬限制。一个进程能够(不能够撤回的)降低它的硬件限制,前提是大于等于软限制。仅当一个进程工作在一个系统超级用户权限下时能够提高它的硬件限制。调用setrlimit()来改变硬限制和软限制。限制值可能是一个“无穷大”值:RLIM_INFINITY。参数rlp是一个指向结构体rlimit的指针,该结构体包含如下成员:
      rlim_t rlim_cur; /*当前(软)限制*/
      rlim_t rlim_max; /*硬限制*/
      类型rlim_t是一个算术数据类型,等同于类型int,size_t,和off_t。
      资源限制类型概要描述如下:
      RLIMIT_CORE
      core文件的最大字节数,若其值为0则阻止创建core文件。
      RLIMIT_CPU
      CPU时间的最大量值(秒),当超过此软限制时,向该进程发送S I G X C P U信号。
      RLIMIT_DATA
      一个进程的数据段最大字节长度。数据段中初始化数据、非初始化数据以及堆的总和。当调用函数brk动态改变一个进程的数据段大小时,若失败,errno值将被设置为ENOMEM。
      RLIMIT_FSIZE
      可以创建的文件的最大字节长度。当超过此软限制时,则向该进程发送SIGXFSZ信号。
      RLIMIT_NOFILE
      每个进程能打开的最多文件数。
      RLIMIT_STACK
      栈的最大字节长度。系统不会动态增加栈的大小限制。
      在进程内部,setrlimit()将增加限制以适合您的栈大小,而不会移动当前内存段来允许增长。为了保证进程的栈能够增长,限制必须首先改变以适应运行进程使用的新栈大小。
      在一个多线程的进程中,若在线程中调用setrlimit(),而该线程不是主线程,则对于栈的限制没有任何改变。使用参数RLIMIT_STACK来调用setrlimit()仅仅能够影响主线程的栈,并且该工作本来就应该由主线程来完成。
      当超越栈大小限制时,信号SIGSEGV会发给进程。如果进程忽略该信号,或者捕捉该信号但是没有设置处理措施,该信号默认设置为SIG_DFL处理方案。
      RLIMIT_VMEM
      可映照地址空间的最大字节长度。
      RLIMIT_AS
      进程可用内存最大字节数。
      因为限制信息存储在每个进程的信息中,shell创建限制命令必须直接调用这两个系统函数来影响该shell以后创建的所有进程。
      下面资源当前限制的值会影响相对应宏定义的展开。
      限制 宏定义展开
      RLIMIT_FSIZE FCHR_MAX
      RLIMIT_NOFILE OPEN_MAX
      当使用函数getrlimit(),如果一个资源限制能够被正确赋值为类型rlim_t,则该值将被返回;否则,如果该资源限制等同于相应的硬件限制,返回值是RLIM_SAVED_MAX;否则返回值是RLIM_SAVED_CUR。
      当使用函数setrlimit(),如果请求新的限制值为RLIM_INFINITY,新的限制将是“无限制”;否则如果请求的新的限制为RLIM_SAVED_MAX,新的限制将被保存为硬件限制;如果请求的新的限制为RLIM_SAVED_CUR,新的限制将被保存为软限制;否则新的限制将赋值为请求的值。另外,如果相应传入的限制值正确,则该限制将作为新的限制值。
      设置一个限制为RLIM_SAVED_MAX或者RLIM_SAVED_CUR的结果是未知的,除非在之前调用getrlimit以了解相应的软限制或者硬限制值。
      一个限制允许其值大于RLIM_INFINITY。

    返回值

      成功完成后,getrlimit()和setrlimit()返回0。否则,返回-1并设置errno指定相应错误。

    错误‍

      函数getrlimit()和函数setrlimit()失败情况如下:
      EFAULT
      参数rlp指向非法地址。
      EINVAL
      指定了一个无效的资源;或者在调用函数setrlimit()时新的rlim_cur值超过了新的rlim_max值。
      EPERM
      调用函数setrlimit试图增加最大限制值,但该进程并不属于超级用户。
      函数setrlimit可能失败情况:
      EINVAL
      指定的限制值不能去减低限制值因为当前用法已经大于该指定限制值。


    为了支持多用户同时登录以及多个应用连接,BSD UNIX系统给系统管理员提供了控制系统资源的许多方法。这种资源限制包括CPU时间、内存使用量以及磁盘使用量。资源控制允许你调整系统到最佳的使用率。UNIX的早期版本中,一些在编译时设置的系统限制如果需要修改,则需要重新编译整个系统。然而,如果并非所有的运行中的系统资源都需要重新编译整个系统,那么现代的BSD系统可以调整大多数这些资源的限制。

      本章阐述和进程相关的限制,包括系统端和用户使用的。我们将会看到如何发现这些限制以及怎么修改之,还将阐述进程是如何查询它的资源使用率。

    7.2  确定系统限制

    getrlimit,setrlimit

    getrlimit允许一个进程查询所受的的系统限制.这些系统限制通过一对硬/软限制对来指定。当一个软限制被超过时,进程还可以继续,当然这取决于限制的类型,同时一个信号会发送给进程。另一方面,进程不可以超过它的硬限制。软限制值可以被进程设置在位于0和最大硬限制间的任意值。硬限制值不能被任何进程降低,仅仅超级用户可以增加之。

    #include <sys/types.h>

        #include <sys/time.h>

        #include <sys/resource.h>

    int   getrlimit(int resource, struct rlimit *rlp);

    int   setrlimit(int resource, const struct rlimit *rlp);

    getrlimit和setrlimit都使用下面的数据结构:

    struct rlimit {

    rlim_t rlim_cur;

    rlim_t rlim_max;

    };

    我们来看每个成员变量。rlim_cur为指定的资源指定当前的系统软限制。rlim_max将为指定的资源指定当前的系统硬限制。

    getrlimit和setrlimit函数的第一个参数是资源参数。这个参数用来指定进程获取信息的那个资源。可能的资源值列于下面。你也可以在/usr/include/sys/resource.h中找到它们:

    #define   RLIMIT_CPU   0     /* cpu time in milliseconds */

    RLIMIT_CPU资源限制指定一个进程可以取得CPU执行任务的毫秒数。一般地,一个进程仅仅有一个软限制而没有硬限制。如果超出软限制,进程会收到一个SIGXCPU信号。

       #define   RLIMIT_FSIZE 1     /* maximum file size */

    RLIMIT_FSIZE限制指定一个进程可以创建的最大文件大小,以字节为单位。比如,如果RLIMIT_FSIZE设置为0,那么进程将根本不能创建文件。如果进程超出此限制,就会发出SIGFSZ信号。

    #define   RLIMIT_DATA 2     /* data size */

    RLIMIT_DATA 限制指定一个进程数据段可占据的最大字节值。一个进程的数据段就是放置动态内存的一个区域(C/C++中用malloc()分配的内存)。如果超出限制,分配新内存的操作将会遭到失败。

    #define   RLIMIT_STACK 3     /* stack size */

    RLIMIT_STACK限制指定进程栈可占据的最大字节数。一旦超出硬限制,进程会收到SIGSEV信号。

    #define   RLIMIT_CORE 4     /* core file size */

    RLIMIT_CORE限制指定了进程可以创建的最大core文件的大小。如果此限制设为0,将不能创建。另外,当达到此限制时,所有正在写core文件的进程都将被中断。

    #define   RLIMIT_RSS   5     /* resident set size */

    RMIMIT_RSS限制了进程的常驻集大小(resident set size)可占据的最大字节数.这个进程的常驻集和进程所使用的物理内存数有关。

       #define   RLIMIT_MEMLOCK 6     /* locked-in-memory address space */

    RLIMIT_MEMLOCK限制指定了进程可以使用系统调用到mlock进行锁定的最大字节数。

       #define   RLIMIT_NPROC 7     /* number of processes */

    RLIMIT_NPROC 限制指定了一个指定用户可以开启的最多并发进程数。这里的用户是通过进程来确定的有效用户ID.

    #define   RLIMIT_NOFILE   8     /* number of open files */

    RLIMIT_NOFILE 限制指定了进程可以打开的最多文件数。

       #define   RLIMIT_SBSIZE   9     /* maximum size of all socket buffers */

    RLIMIT_SBSIZE限制指定用户在任何时刻可使用的mbufs数。可以查看socket man页来获得mbufs的定义。

    #define   RLIMIT_VMEM 10 /* virtual process size (inclusive of mmap) */

    RLIMIT_VMEM限制说明一个进程的映射地址空间可以占据的字节数。如果超出限制,分配动态内存和到mmap的调用会失败。

    #define   RLIM_INFINITY

    RLIM_INFINITY宏用来去除对一个资源的限制。换句话说,将一个资源的硬限制设置为RLIM_INFINITY将会导致此种资源的使用没有任何系统限制。 将软限制设置为RLIM_INFINITY将会阻止进程收到任何软限制警告。如果你的进程不想为那些会导致进程在超过软限制时发送信号的资源设置一个信号处理器,这个参数将变得非常有用。

    如果使用了getrlimit参数,那么第二个参数需要设置为一个到rlimit结构的有效指针。然后getrlimit会将适当的限制值放入此结构。另外,在改变限制时,setrlimit会使用在第二个参数中设置值。将值设置为0将会阻止使用此资源。将值设置为RLIM_INFINITY会除去对该资源的所有限制。这些函数都在执行成功后都返回0,反之为-1.有任何错误产生,这些函数会相应的设置errno。

    getpagesize函数

    #include <unistd.h>

    int   getpagesize(void);

      在介绍getrusage函数前,我们需要讨论一下getpagesize函数。一些进程状态 是根据使用的分页(pages)来显示的。分页的内存仅仅是一段内存,通常为4096字节左右。但是分页大小却千差万别,并且它不会固定编入(hard-coded)你的系统。取而代之的是,要确定本地系统的分页大小(pagesize)需要使用getpagesezi函数。getpagesize的返回值就是每个分页使用的字节数。

    7.3 确定进程资源使用量

    getrusage函数  

    现在我们知道如何查看系统限制,我们还需要知道如何确定当前进程资源的使用量。getrusage函数就是用于此目的。此函数很容易被理解。一个进程可以确定它的内存使用量、CPU执行时间、甚至可获得其子进程的相关信息。因此,getrusage函数的一个用途就是帮助系统避免逃逸进程(runaway)的出现。逃逸进程指的是那些不受系统控制的进程,它们或者使用了过多的CPU(比如循环调用)、或者是超过了内存使用量的限制(导致内存泄漏)。

       #include <sys/types.h>

       #include <sys/time.h>

       #include <sys/resource.h>

      

       #define RUSAGE_SELF     0

       #define RUSAGE_CHILDREN     -1

      

    int getrusage(int who, struct rusage *rusage);

    getrusage函数有两个参数。第一个参数可以设置为RUSAGE_SELF或者RUSAGE_CHILDREN。如果设置成RUSAGE_SELF,那么将会以当前进程的相关信息来填充rusage(数据)结构。反之,如果设置成RUSAGE_CHILDREN,那么rusage结构中的数据都将是当前进程的子进程的信息。

    rusage(数据)结构定义在/usr/include/sys/resource.h中。它含有以下成员变量:

    struct rusage {

       struct timeval ru_utime; /* user time used */

       struct timeval ru_stime; /* system time used */

       long   ru_maxrss;           /* max resident set size */

       long   ru_ixrss;          /* integral shared memory size */

       long   ru_idrss;          /* integral unshared data */

       long   ru_isrss;          /* integral unshared stack */

       long   ru_minflt;           /* page reclaims */

       long   ru_majflt;           /* page faults */

       long   ru_nswap;          /* swaps */

       long   ru_inblock;       /* block input operations */

       long   ru_oublock;       /* block output operations */

       long   ru_msgsnd;           /* messages sent */

       long   ru_msgrcv;           /* messages received */

       long   ru_nsignals;       /* signals received */

       long   ru_nvcsw;          /* voluntary context switches */

       long   ru_nivcsw;           /* involuntary " */

       };

    我们来详细分析每一个成员变量。

    ru_utime,ru_stime

    ru_utime和ru_stime成员变量包含了在用户模式和系统模式中执行时间的总和。它们都使用timeval结构(请查看前一章来了解此结构。)

    ru_maxrss

    ru_maxrss保存了常驻集的内存使用数。其值根据内存分页的使用来确定。

    ru_ixrss

    ru_ixrss值指文本段(text segment)使用的内存数乘以执行滴答数。

    ru_idrss

    ru_idrss 值指进程所使用的私有内存数(KB)乘以执行滴答数来。

    ru_isrss

    ru_isrss 指栈使用的内存数(KB为单位)乘以执行滴答数。

    ru_minflt

    ru_minflt值指不需要I/O的页缺失数。页缺失发生在内核需要得到一个内存页以供进程访问时。

    ru_majflt

    ru_majflt值指需要I/O的页缺失数。页缺失发生在内核需要得到一个内存页以供进程访问时。

    ru_nswap

    有时,一个进程会被调出内存,以提供空间给其他进程使用。ru_nswap指的就是一个进程将要调出内存的次数。

    ru_inblock

    ru_inblock 指文件系统需要为一个读请求执行输入操作的次数。

    ru_oublock

    ru_oublock指文件系统需要为一个写入请求执行输出操作的次数。

    ru_msgsnd

    ru_msgsnd指发送的IPC信息总数

    ru_msgrcv

    ru_msgrcv指收到的IPC信息总数。

    ru_nsignals

    ru_nsignals指进程收到的信号总数。

    ru_nvcsw

    一个进程主动上下文且混总数。主动上下文切换发生在一个进程放弃它的CPU分时时。通常发生在一个进程等待某可用资源时。

    ru_nivcsw

    ru_nivcsw包含了因高优先级进程运行导致的上下文切换总数。

    7.4 小结

    本章主要阐述了程序如何得到系统限制。这些限制不应该固定写入你的代码。取而代之的是,你的程序应该使用这些接口。这是因为这些限制是与平台相关的,甚至不同系统间都会有差别。比如,系统管理员可以调整允许打开的最大文件数,因此如果你需要增加或者减小这个值,就不能将值4096固定写入程序中。另一个例子是分页大小。一些64位系统使用8096作为默认的分页大小,但是大多数的32位系统将此默认值设置位4096。再次强调,这是个可调整的参数,不应该使用一个固定的值。

    我们还学习了一个程序如何得到其资源的当前使用量,或者查看其子进程的资源使用状况。使用这些接口可以帮助我们检测甚至避免出现失控的进程。它们用在程序试图超越其软/硬限制时调试错误也非常不错。

    http://baike.baidu.com/view/5048599.htm

    http://blog.163.com/lijiji_1515/blog/static/1268774462009115114327206/

    展开全文
  • getrlimit setrlimit

    千次阅读 2015-11-22 11:15:01
    getrlimit, setrlimit函数

    介绍资源限制函数getrlimit, setrlimit,当然仍是man大法好。

    1. 首先看下ulimit -a

    wln@iZ232ngsvp8Z:~/2015/11> ulimit -a
    core file size          (blocks, -c) unlimited
    data seg size           (kbytes, -d) unlimited
    scheduling priority             (-e) 0
    file size               (blocks, -f) unlimited
    pending signals                 (-i) 3827
    max locked memory       (kbytes, -l) 64
    max memory size         (kbytes, -m) 422620
    open files                      (-n) 65535
    pipe size            (512 bytes, -p) 8
    POSIX message queues     (bytes, -q) 819200
    real-time priority              (-r) 0
    stack size              (kbytes, -s) 8192
    cpu time               (seconds, -t) unlimited
    max user processes              (-u) 3827
    virtual memory          (kbytes, -v) 397760
    file locks                      (-x) unlimited


    2. 测试程序

    #include <sys/time.h>
    #include <sys/resource.h>
    #include <stdio.h>
    
    int limitArray[]={
      RLIMIT_AS,
      RLIMIT_CORE,
      RLIMIT_CPU,
      RLIMIT_DATA,
      RLIMIT_FSIZE,
      RLIMIT_MEMLOCK,
      RLIMIT_MSGQUEUE,
      RLIMIT_NICE,
      RLIMIT_NOFILE,
      RLIMIT_NPROC,
      RLIMIT_RSS,
      RLIMIT_RTPRIO,
      RLIMIT_LOCKS,
      //RLIMIT_RTTIME,
      RLIMIT_SIGPENDING,
      RLIMIT_STACK
    };
    
    
    int main()
    {
      struct rlimit *limit;
      int len=sizeof(limitArray)/sizeof(int);
      int i=0;
      while(i<len)
      {
          if(getrlimit(limitArray[i],limit) != 0)
          {
            printf("%d %m\n",i);
          }
          else
          {
             printf("%2d: cur:%d  %d\n",i,limit->rlim_cur,limit->rlim_max);
          }
          i=i+1;
      }
    
      //set core limit
      limit->rlim_cur = RLIM_INFINITY;
      limit->rlim_max = RLIM_INFINITY;
      if(setrlimit(RLIMIT_CORE, limit) !=0)
      {
        printf("set limit failed %m\n");
      }
      else
      {
        printf("set limit ok\n");
      }
     
    }

    执行结果:

    wln@iZ232ngsvp8Z:~/2015/11> ./getrlimit1
     0: cur:407306240  -1
     1: cur:-1  -1
     2: cur:-1  -1
     3: cur:-1  -1
     4: cur:-1  -1
     5: cur:65536  262144
     6: cur:819200  819200
     7: cur:0  0
     8: cur:65535  65535
     9: cur:3827  3827
    10: cur:432762880  -1
    11: cur:0  0
    12: cur:-1  -1
    13: cur:3827  3827
    14: cur:8388608  -1
    set limit ok


    3. man

    NAME
           getrlimit, setrlimit - get/set resource limits
    
    SYNOPSIS
           #include <sys/time.h>
           #include <sys/resource.h>
    
           int getrlimit(int resource, struct rlimit *rlim);
           int setrlimit(int resource, const struct rlimit *rlim);
    
    DESCRIPTION
           getrlimit() and setrlimit() get and set resource limits respectively.  Each resource has an associated soft and hard limit,
           as defined by the rlimit structure (the rlim argument to both getrlimit() and 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:
    
           RLIMIT_AS
                  The maximum size of the process's 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 generate a SIGSEGV that kills the process if no alternate stack has been made available  via  sigalt-
                  stack(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 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  through
                  2.6  behavior.   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 to brk(2) and sbrk(2), 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 deliv-
                  ery of a SIGXFSZ signal.  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(2), truncate(2)) fails with the error EFBIG.
    
           RLIMIT_LOCKS (Early Linux 2.4 only)
                  A limit on the combined number of flock(2) locks and fcntl(2) 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) and mlockall(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(3).
    
                  The first addend in the formula, which includes sizeof(struct msg_msg *) (4 bytes on Linux/i386), 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 Linux 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 typi-
                  cally 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(2), pipe(2), dup(2), etc.)  to exceed this limit yield the error EMFILE.  (Historically, this limit was  named
                  RLIMIT_OFILE on BSD.)
    
           RLIMIT_NPROC
                  The  maximum  number of processes (or, more precisely on Linux, threads) that can be created for the real user ID of
                  the calling process.  Upon encountering this limit, fork(2) 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 to madvise(2) 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) and
                  sched_setparam(2).
    
           RLIMIT_RTTIME (Since Linux 2.6.25)
                  Specifies a limit on the amount of CPU time that a process scheduled under a real-time scheduling policy may consume
                  without  making  a blocking system call.  For the purpose of this limit, each time a process makes a blocking system
                  call, the count of its consumed CPU time is reset to zero.  The CPU time count is not reset if the process continues
                  trying to use the CPU but is preempted, its time slice expires, or it calls sched_yield(2).
    
                  Upon  reaching  the soft limit, the process is sent a SIGXCPU signal.  If the process catches or ignores this signal
                  and continues consuming CPU time, then SIGXCPU will be generated once each second until the hard limit  is  reached,
                  at which point the process is sent a SIGKILL signal.
    
                  The intended use of this limit is to stop a runaway real-time process from locking up the system.
    
           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  han-
                  dle this signal, a process must employ an alternate signal stack (sigaltstack(2)).
    
                  Since Linux 2.6.23, this limit also determines the amount of space used for the process's command-line arguments and
                  environment variables; for details, see execve(2).
    
    RETURN VALUE
           On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.
    
    ERRORS
           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;  the
                  CAP_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).
    
    CONFORMING TO
           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_MSGQUEUE,   RLIMIT_NICE,   RLIMIT_RTPRIO,
           RLIMIT_RTTIME, and RLIMIT_SIGPENDING are Linux-specific.
    
    NOTES
           A child process created via fork(2) inherits its parent's resource limits.  Resource limits are preserved across execve(2).
    
           One can set the resource limits of the shell using the built-in ulimit command (limit in  csh(1)).   The  shell's  resource
           limits are inherited by the processes that it creates to execute commands.
    
    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 Linux
           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.
    
    SEE ALSO
           dup(2), fcntl(2), fork(2), getrusage(2), mlock(2), mmap(2), open(2), quotactl(2),  sbrk(2),  shmctl(2),  sigqueue(2),  mal-
           loc(3), ulimit(3), core(5), capabilities(7), signal(7)
    
    COLOPHON
           This  page  is  part  of  release 3.15 of the Linux man-pages project.  A description of the project, and information about
           reporting bugs, can be found at http://www.kernel.org/doc/man-pages/.



    4.推荐阅读

    1.getrlimit和setrlimit函数

    http://bbs.chinaunix.net/thread-1940558-1-1.html


    展开全文
  • getrlimit,setrlimit

    2021-05-14 16:31:33
    const rlim_t max_stack_size = stack_size;... getrlimit(RLIMIT_STACK, &stack_limit); if (stack_limit.rlim_cur < stack_size) { stack_limit.rlim_cur = stack_size; } setrlimit(RLIM...
        const rlim_t max_stack_size = stack_size;
        struct rlimit stack_limit;
        getrlimit(RLIMIT_STACK, &stack_limit);
        if (stack_limit.rlim_cur < stack_size)
        {
          stack_limit.rlim_cur = stack_size;
        }
        setrlimit(RLIMIT_STACK, &stack_limit);

    进程资源查询,修改

    struct rlimit {
        rlim_t    rlim_cur;    /* soft limit: current limit */
        rlim_t    rlim_max;    /* hard limit: maximum value for rlim_cur */
    };

     

    展开全文
  • getrlimit、setrlimit

    2016-02-26 15:11:17
    每个进程都有一组资源的限制,通过getrlimit、setrlimit能查寻和改变资源限制。 本文讲述这两个函数的使用

    每个进程都有一组资源的限制,通过getrlimit、setrlimit能查寻和改变资源限制。

    getrlimit, setrlimit, prlimit - get/set resource limits

    #include <sys/time.h>
    #include <sys/resource.h>
    
    int getrlimit(int resource, struct rlimit *rlim);
    int setrlimit(int resource, const struct rlimit *rlim);
    
    //Return: 0 if OK, nonzero on error

    系统资源限制是系统启动时由进程0设置的,且由后续的进程继承。
    其中特殊的结构rlimit如下:

    struct rlimit {
                   rlim_t rlim_cur;  /* Soft limit */
                   rlim_t rlim_max;  /* Hard limit (ceiling for rlim_cur) */
    };

    改变资源限制的三个准则

    • 进程的软限制(soft limit)需要小于等于硬限制(hard limit)
    • 进程可以降低其硬限制,以大于等于soft limit。注意:降低硬限制对一般用户不可逆!
    • 仅仅超级用户进程(superuser process)可以提高硬限制

    常量RLIM_INFINITY表示无限的限制
    resource使用如下图的参数(Linux 3.2.0):

    Resource
    RLIMIT_AS The maximum size in bytes of a process’s total available memory. This affects the sbrk function (Section 1.11) and the mmap function (Section 14.8).
    RLIMIT_CORE The maximum size in bytes of a core file. A limit of 0 prevents the creation of a core file.
    RLIMIT_CPU The maximum amount of CPU time in seconds. When the soft limit is exceeded, the SIGXCPU signal is sent to the process.
    RLIMIT_DATA The maximum size in bytes of the data segment: the sum of the initialized data, uninitialized data, and heap from Figure 7.6.
    RLIMIT_FSIZE The maximum size in bytes of a file that may be created. When the soft limit is exceeded, the process is sent the SIGXFSZ signal.
    RLIMIT_MEMLOCK The maximum amount of memory in bytes that a process can lock into memory using mlock(2).
    RLIMIT_MSGQUEUE The maximum amount of memory in bytes that a process can allocate for POSIX message queues.
    RLIMIT_NICE The limit to which a process’s nice value (Section 8.16) can be raised to affect its scheduling priority.
    RLIMIT_NOFILE The maximum number of open files per process. Changing this limit affects the value returned by the sysconf function for its _SC_OPEN_MAX argument (Section 2.5.4). See Figure 2.17 also.
    RLIMIT_NPROC The maximum number of child processes per real user ID. Changing this limit affects the value returned for _SC_CHILD_MAX by the sysconf function (Section 2.5.4).
    RLIMIT_RSS Maximum resident set size (RSS) in bytes. If available physical memory is low, the kernel takes memory from processes that exceed their RSS.
    RLIMIT_SIGPENDING The maximum number of signals that can be queued for a process. This limit is enforced by the sigqueue function (Section 10.20).
    RLIMIT_STACK The maximum size in bytes of the stack. See Figure 7.6.
    • shelllimit command:set or report file size limit

    Example:

    打印出系统当前的soft limithard limit

    #include <sys/resource.h>
    #define doit(name) pr_limit(#name, name)
    static void pr_time(char *name, int resource)
    {
        struct rlimit limit;
        if(getrlimit(resource, &limit) < 0)//获取资源
            出错处理;
        printf("%s ", name);
        if(limit.rlim_cur == RLIM_INFINITY)//查看当前的soft limit
            printf("(infinity)");
        else
            printf("%d\n", limit.rlim_cur);
        if(limit.rlim_max == RLIM_INFINITY)//查看硬限制hard limit
            printf("(infinity)");
        else
            printf("%d\n", limit.rlim_max);
    }

    使用doit(RLIMIT_CORE);
    等效于pr_limit("RLIMIT_CORE", RLIMIT_CORE);

    展开全文
  • getrlimit与setrlimit

    2018-02-23 17:11:53
    功能描述: 获取或设定资源使用限制。每种资源都有相关的软硬限制,软限制是内核强加给相应资源的限制值,硬限制是软限制的最大值。非授权调用进程只可以将其软限制指定为0~硬限制...int getrlimit(int resource, struc
  • linux getrlimit sysconf

    2015-06-22 10:40:49
    linux中getrlimit和sysconf/*************************************************************** > File Name: rlimit_sysconf.c > Author: lxg > Mail: liuxingen@nsfocus.com > Created Time: 2015年06
  • getrlimit的帮助手册

    2016-10-25 18:44:47
    GETRLIMIT(2) BSD System Calls Manual GETRLIMIT(2) NAME getrlimit, setrlimit -- control maximum system resource consumption SYNOPSIS #include int ge
  • getrlimit 和 setrlimit

    2017-08-17 10:58:07
    功能描述: 获取或设定资源使用限制。每种资源都有相关的软硬限制,软限制是内核强加给相应资源的限制值,硬限制是软限制的最大值。非授权调用进程只可以将其软限制指定为0~...#include int getrlimit(int resource, s
  • 本篇文章是对在Linux系统中调用getrlimit()与setrlimit()函数的方法进行了详细的分析介绍,需要的朋友参考下
  • getrlimit 与setrlimit

    2012-11-22 21:31:28
    getrlimit和setrlimit函数   每个进程都有一组资源限制,其中某一些可以用getrlimit和setrlimit函数查询和更改。 #include  #include  int getrlimit(int resource,struct rlimit *rlptr); int setrlimit...
  • getrlimit/setrlimit

    2013-12-23 14:59:06
    int getrlimit(int resource, struct rlimit *rlp); int setrlimit(int resource, const struct rlimit *rlp); 描述 在操作系统中,我们能够通过函数getrlimit()、setrlimit()分别获得、设置每个进程能够...
  • linux下getrlimit与sysconf函数

    千次阅读 2014-10-22 13:14:46
    getrlimit与sysconf简单调用
  • 每个进程都有一组资源限制,其中一些可以用getrlimit和setrlimit函数查询和更改。 #include <sys/resource.h> int getrlimit( int resource, struct rlimit *rlptr ); int setrlimit( int resource, const ...
  • 功能描述:获取或设定资源使用限制。...用法: 代码如下:#include <sys>int getrlimit(int resource, struct rlimit *rlim);int setrlimit(int resource, const struct rlimit *rlim);参数:resource:可能的
  • getrlimit和setrlimit函数 设置资源权限

    千次阅读 2015-09-04 10:36:23
    getrlimit和setrlimit函数 每个进程都有一组资源限制,其中某一些可以用getrlimit和setrlimit函数查询和更改。 #include #include int getrlimit(int resource,struct rlimit *rlptr); int setrli
  • getrlimit和setrlimit函数

    千次阅读 2012-05-16 15:26:44
    getrlimit和setrlimit函数  每个进程都有一组资源限制,其中某一些可以用getrlimit和setrlimit函数查询和更改。 #include #include int getrlimit(int resource,struct rlimit *rlptr); int setrlimit(int ...
  • python中的getrlimit和setrlimit

    千次阅读 2016-08-03 15:51:18
    python中的getrlimit和setrlimit和c中一样。需要导入python的类库。(# import resource ) resource:可能的选择有 RLIMIT_AS //进程的最大虚内存空间,字节为单位。 RLIMIT_CORE //内核转存文件的...
  • 功能描述: 获取或设定资源使用限制。每种资源都有相关的软硬限制,软限制是内核强加给相应资源的限制值,硬限制是软限制的最大值。...int getrlimit(int resource, struct rlimit *rlim); int setrlimit(in
  • getrlimit和setrlimit

    千次阅读 2008-05-15 22:19:00
    getrlimit和setrlimit函数 每个进程都有一组资源限制,其中某一些可以用getrlimit和setrlimit函数查询和更改。#include #include int getrlimit(int resource,struct rlimit *rlptr);int setrlimit(int resource,...
  • getrlimit,setrlimit函数   getrlimit允许一个进程查询所受的的系统限制.这些系统限制通过一对硬/软限制对来指定。当一个软限制被超过时,进程还可以继续,当然这取决于限 制的类型,同时一个信号会发送给进程...
  • getrlimit和setrlimit 头文件 #include 函数原型 int getrlimit(int resource, struct rlimit *rlim); int setrlimit(int resource, const struct rlimit *rlim); 功能 每个进程都有一组资源...
  • getrlimit,setrlimit - 控制系统资源的最大使用量 #include <unistd.h> #include <stdio.h> #include <sys/resource.h> /*struct rlimit{ rlim_t rlim_cur; //soft limit: current limit.....
  • getrlimit && setrlimit

    2011-11-02 21:58:08
    C语言系统资源控制(getrlimit && setrlimit) 2007-08-17 00:40 每一个进程都有自己的一组资源限制,在(*)inux系统中我们可以通过 #include int getrlimit(int resource, struct rlimit *rlim); int...
  • getrlimit Unix系统函数

    2011-02-15 17:00:00
    Unix系统函数名称 getrlimit,setrlimit - 控制系统资源的最大使用量。[1]‍‍[2]大纲 #include int getrlimit(int resource, struct rlimit *rlp); int setrlimit(int resource, const struct rlimit *rlp); 描述...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 5,403
精华内容 2,161
关键字:

getrlimit