RLIMIT_*

用于取得 *nix 平台上的 RLIMITs 值,我猜应该是完整列表吧。参考了 apue2e。

 

#if defined(SOLARIS)
#define _XOPEN_SOURCE	500	/* Single UNIX Specification, Version 2  for Solaris 9 */
#define CMSG_LEN(x)	_CMSG_DATA_ALIGN(sizeof(struct cmsghdr)+(x))
#elif !defined(BSD)
#define _XOPEN_SOURCE	600	/* Single UNIX Specification, Version 3 */
#endif

#include <sys/types.h>	/* some systems still require this */
#include <sys/stat.h>
#include <sys/termios.h>	/* for winsize */
#ifndef TIOCGWINSZ
#include <sys/ioctl.h>
#endif
#include <stdio.h>		/* for convenience */
#include <stdlib.h>		/* for convenience */
#include <stddef.h>		/* for offsetof */
#include <string.h>		/* for convenience */
#include <unistd.h>		/* for convenience */
#include <errno.h>		/* for definition of errno */
#include <stdarg.h>		/* ISO C variable aruments */

#include <sys/resource.h>

#define	MAXLINE	4096			/* max line length */

#if defined(BSD) || defined(MACOS)
#include <sys/time.h>
#define FMT "%10lld	"
#else
#define FMT "%10ld	"
#endif	/* define(BSD) || defined(MACOS) */

#define doit(name) pr_limits(#name, name)

static void pr_limits(char*, int);

static void err_doit(int, int, const char *, va_list);
static void err_sys(const char *, ...);

int main(void)
{
#ifdef RLIMIT_AS
	doit(RLIMIT_AS);
#endif

	doit(RLIMIT_CORE);
	doit(RLIMIT_CPU);
	doit(RLIMIT_DATA);
	doit(RLIMIT_FSIZE);

#ifdef RLIMIT_LOCKS
	doit(RLIMIT_LOCKS);
#endif

#ifdef RLIMIT_MEMLOCK
	doit(RLIMIT_MEMLOCK);
#endif

	doit(RLIMIT_NOFILE);

#ifdef RLIMIT_NPROC
	doit(RLIMIT_NPROC);
#endif

#ifdef RLIMIT_RSS
	doit(RLIMIT_RSS);
#endif

#ifdef RLIMIT_SBSIZE
	doit(RLIMIT_SBSIZE);
#endif

	doit(RLIMIT_STACK);

#ifdef RLIMIT_VMEM
	doit(RLIMIT_VMEM);
#endif

#ifdef RLIMIT_SIGPENDING
	doit(RLIMIT_SIGPENDING);
#endif

#ifdef RLIMIT_MSGQUEUE
	doit(RLIMIT_MSGQUEUE);
#endif

#ifdef RLIMIT_NICE
	doit(RLIMIT_NICE);
#endif

#ifdef RLIMIT_RTPRIO
	doit(RLIMIT_RTPRIO);
#endif

#ifdef RLIMIT_TCACHE
	doit(RLIMIT_TCACHE);
#endif

#ifdef RLIMIT_AIO_OPS
	doit(RLIMIT_AIO_OPS);
#endif

#ifdef RLIMIT_AIO_MEM
	doit(RLIMIT_AIO_MEM);
#endif

#ifdef RLIMIT_RSESTACK
	doit(RLIMIT_RSESTACK);
#endif

	return 0;	
}

static void pr_limits(char* name, int resource)
{
	struct rlimit limit;

	if (getrlimit(resource, &limit) < 0)
		err_sys("getrlimit error for %s", name);

	printf("%-20s\t", name);

	if (limit.rlim_cur == RLIM_INFINITY)
		printf("(infinite)\t");
	else
		printf(FMT, limit.rlim_cur);

	if (limit.rlim_max == RLIM_INFINITY)
		printf("(infinite)");
	else
		printf(FMT, limit.rlim_max);

	putchar((int)'\n');
}

static void err_sys(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	err_doit(1, errno, fmt, ap);
	va_end(ap);
	exit(1);
}

static void err_doit(int errnoflag, int error, const char *fmt, va_list ap)
{
	char	buf[MAXLINE];

	vsnprintf(buf, MAXLINE, fmt, ap);
	if (errnoflag)
		snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s",
		strerror(error));
	strcat(buf, "\n");
	fflush(stdout);		/* in case stdout and stderr are the same */
	fputs(buf, stderr);
	fflush(NULL);		/* flushes all stdio output streams */
}

 

Resource must be one of:

 

  • 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.)

 

  • RLIMIT_DATA

          The maximum size of the process's 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 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

          A limit on the combined number of flock() locks and fcntl() leases that this process may establish (Linux 2.4 and later).

 

  • 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.

 

  • 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.

 

  • 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 onwatrds, and there only affects calls to madvise() specifying MADVISE_WILLNEED.

 

  • 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.

 

/*
 * AIX 5.3 resource limits
 * /usr/include/sys/resource.h
 */
#define RLIMIT_CPU      0               /* cpu time in milliseconds */
#define RLIMIT_FSIZE    1               /* maximum file size */
#define RLIMIT_DATA     2               /* data size */
#define RLIMIT_STACK    3               /* stack size */
#define RLIMIT_CORE     4               /* core file size */
#define RLIMIT_RSS      5               /* resident set size */

#define RLIMIT_AS       6               /* max size proc's total memory--not enforced */
#define RLIMIT_NOFILE   7               /* max # allocated fds--not enforced */
 
/*
 * SunOS 5.10 resource limits
 * /usr/include/sys/resource.h
 */
#define RLIMIT_CPU      0               /* cpu time in seconds */
#define RLIMIT_FSIZE    1               /* maximum file size */
#define RLIMIT_DATA     2               /* data size */
#define RLIMIT_STACK    3               /* stack size */
#define RLIMIT_CORE     4               /* core file size */
#define RLIMIT_NOFILE   5               /* file descriptors */
#define RLIMIT_VMEM     6               /* maximum mapped memory */
#define RLIMIT_AS       RLIMIT_VMEM

#define RLIM_NLIMITS    7               /* number of resource limits */

#if defined(_LP64)

typedef unsigned long   rlim_t;

#define RLIM_INFINITY   (-3l)
#define RLIM_SAVED_MAX  (-2l)
#define RLIM_SAVED_CUR  (-1l)

#else   /* _LP64 */

/*
 * The definitions of the following types and constants differ between the
 * regular and large file compilation environments.
 */
#if _FILE_OFFSET_BITS == 32

typedef unsigned long   rlim_t;

#define RLIM_INFINITY   0x7fffffff
#define RLIM_SAVED_MAX  0x7ffffffe
#define RLIM_SAVED_CUR  0x7ffffffd

#else   /* _FILE_OFFSET_BITS == 32 */

typedef u_longlong_t    rlim_t;

#define RLIM_INFINITY   ((rlim_t)-3)
#define RLIM_SAVED_MAX  ((rlim_t)-2)
#define RLIM_SAVED_CUR  ((rlim_t)-1)

#endif  /* _FILE_OFFSET_BITS == 32 */

#endif  /* _LP64 */
 
/*
 * linux 2.6 (ubuntu 8.10)
 * /usr/include/bits/resource.h
 */
/* Kinds of resource limit.  */
enum __rlimit_resource
{
  /* Per-process CPU limit, in seconds.  */
  RLIMIT_CPU = 0,
#define RLIMIT_CPU RLIMIT_CPU

  /* Largest file that can be created, in bytes.  */
  RLIMIT_FSIZE = 1,
#define RLIMIT_FSIZE RLIMIT_FSIZE

  /* Maximum size of data segment, in bytes.  */
  RLIMIT_DATA = 2,
#define RLIMIT_DATA RLIMIT_DATA

  /* Maximum size of stack segment, in bytes.  */
  RLIMIT_STACK = 3,
#define RLIMIT_STACK RLIMIT_STACK

  /* Largest core file that can be created, in bytes.  */
  RLIMIT_CORE = 4,
#define RLIMIT_CORE RLIMIT_CORE

  /* Largest resident set size, in bytes.
     This affects swapping; processes that are exceeding their
     resident set size will be more likely to have physical memory
     taken from them.  */
  __RLIMIT_RSS = 5,
#define RLIMIT_RSS __RLIMIT_RSS

  /* Number of open files.  */
  RLIMIT_NOFILE = 7,
  __RLIMIT_OFILE = RLIMIT_NOFILE, /* BSD name for same.  */
#define RLIMIT_NOFILE RLIMIT_NOFILE
#define RLIMIT_OFILE __RLIMIT_OFILE

  /* Address space limit.  */
  RLIMIT_AS = 9,
#define RLIMIT_AS RLIMIT_AS

  /* Number of processes.  */
  __RLIMIT_NPROC = 6,
#define RLIMIT_NPROC __RLIMIT_NPROC

  /* Locked-in-memory address space.  */
  __RLIMIT_MEMLOCK = 8,
#define RLIMIT_MEMLOCK __RLIMIT_MEMLOCK

  /* Maximum number of file locks.  */
  __RLIMIT_LOCKS = 10,
#define RLIMIT_LOCKS __RLIMIT_LOCKS

  /* Maximum number of pending signals.  */
  __RLIMIT_SIGPENDING = 11,
#define RLIMIT_SIGPENDING __RLIMIT_SIGPENDING

  /* Maximum bytes in POSIX message queues.  */
  __RLIMIT_MSGQUEUE = 12,
#define RLIMIT_MSGQUEUE __RLIMIT_MSGQUEUE

  /* Maximum nice priority allowed to raise to.
     Nice levels 19 .. -20 correspond to 0 .. 39
     values of this resource limit.  */
  __RLIMIT_NICE = 13,
#define RLIMIT_NICE __RLIMIT_NICE

  /* Maximum realtime priority allowed for non-priviledged
     processes.  */
  __RLIMIT_RTPRIO = 14,
#define RLIMIT_RTPRIO __RLIMIT_RTPRIO

  __RLIMIT_NLIMITS = 15,
  __RLIM_NLIMITS = __RLIMIT_NLIMITS
#define RLIMIT_NLIMITS __RLIMIT_NLIMITS
#define RLIM_NLIMITS __RLIM_NLIMITS
};

/* Value to indicate that there is no limit.  */
#ifndef __USE_FILE_OFFSET64
# define RLIM_INFINITY ((unsigned long int)(~0UL))
#else
# define RLIM_INFINITY 0xffffffffffffffffuLL
#endif

#ifdef __USE_LARGEFILE64
# define RLIM64_INFINITY 0xffffffffffffffffuLL
#endif

 

作者:lzy.je
出处:http://lzy.iteye.com
本文版权归作者所有,只允许以摘要和完整全文两种形式转载,不允许对文字进行裁剪。未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

// 2009.04.29 添加 ////

 

/*
 * HP-UX 11.23I Resource limits
 * /usr/include/sys/resource.h
 */

#if defined(_LARGEFILE64_SOURCE)
#  if defined(__L64_MODE__)
#    define  RLIM64_INFINITY  (uint64_t) 0x7fffffffffffffffL
#  else /* __L64_MODE__ */
#    if defined(__LL_MODE__)
#       define  RLIM64_INFINITY  (uint64_t) 0x7fffffffffffffffLL
#    endif /* __LL_MODE__ */
#  endif /* __L64_MODE__ */
#  define  RLIM32_INFINITY  (uint32_t) 0x7fffffff
#endif /* _LARGEFILE64_SOURCE */

#if defined(_KERNEL)
#  define  RLIM_INFINITY  RLIM64_INFINITY
#  define  K_RLIM_INFINITY  RLIM_INFINITY
#  define  ATT_BYTESPERBLOCK    512
#  define  LOG_ATT_BYTESPERBLOCK        9
/* bytes to 1/2k blocks and 1/2k  blocks to bytes conversion macro. */
#  define bt2bk(x)      (((unsigned long) (x)) >> (LOG_ATT_BYTESPERBLOCK))
#  define bk2bt(x)      (((unsigned long) (x)) << (LOG_ATT_BYTESPERBLOCK))
#  define ULIMIT_MAX    bt2bk(0x7fffffff)
#  if defined(__L64_MODE__)
#    define  ULIMIT64_MAX bt2bk(0x7fffffffffffffffL)
#  endif /* __L64_MODE__ */
#  define  ULIMIT32_MAX bt2bk(0x7fffffff)
#else
#if defined(__STDC__)
#  if defined(__L64_MODE__)
#    define  RLIM_INFINITY    0x7fffffffffffffffUL
#  else /* __L64_MODE__ */
#    if defined(__64BIT_OFF_T)
#      if defined(__LL_MODE__)
#        define  RLIM_INFINITY    0x7fffffffffffffffULL
#      endif /* __LL_MODE__ */
#    else /* __64BIT_OFF_T */
#      define  RLIM_INFINITY    0x7fffffffUL
#    endif /* __64BIT_OFF_T */
#  endif /* __L64_MODE__ */
#else
#  if defined(__L64_MODE__)
#    define  RLIM_INFINITY   (uint64_t) 0x7fffffffffffffffL
#  else /* __L64_MODE__ */
#    if defined(__64BIT_OFF_T)
#      if defined(__LL_MODE__)
#        define  RLIM_INFINITY   (uint64_t) 0x7fffffffffffffffLL
#      endif /* __LL_MODE__ */
#    else /* __64BIT_OFF_T */
#      define  RLIM_INFINITY   (uint32_t) 0x7fffffff
#    endif /* __64BIT_OFF_T */
#  endif /* __L64_MODE__ */
#endif /* __STDC__ */
#endif /* _KERNEL */

#define	RLIMIT_CPU	0		/* cpu time in milliseconds */
#define	RLIMIT_DATA	2		/* data size */
#define	RLIMIT_STACK	3		/* stack size */
#define	RLIMIT_RSS	5		/* resident set size */

#define	RLIMIT_FSIZE	1		/* maximum file size */
#define	RLIMIT_CORE	4		/* core file size */
#define RLIMIT_NOFILE   6               /* maximum number of open files */
#define RLIMIT_OPEN_MAX RLIMIT_NOFILE   /* maximum number of open files */
#define RLIMIT_AS       7               /* maximum number of open files */
#define RLIMIT_TCACHE   8		/* maximum number of cached threads */
#define RLIMIT_AIO_OPS  9		/* maximum number of POSIX AIO ops */
#define RLIMIT_AIO_MEM  10		/* maximum bytes locked for POSIX AIO */
#define RLIMIT_RSESTACK	11		/* RSE stack size */
#define	RLIM_NLIMITS	12		/* number of resource limits */

#endif /* _INCLUDE_XOPEN_SOURCE_EXTENDED */

#endif /* _SYS_RESOURCE_INCLUDED */

 

这几个 limit 不明白意思:

 

RLIMIT_TCACHE 8 /* maximum number of cached threads */
RLIMIT_AIO_OPS 9 /* maximum number of POSIX AIO ops */
RLIMIT_AIO_MEM 10 /* maximum bytes locked for POSIX AIO */
RLIMIT_RSESTACK 11 /* RSE stack size */
RLIM_NLIMITS 12 /* number of resource limits */

 

你可能感兴趣的:(linux,Solaris,ubuntu,AIX,HP)