#include<stdio.h>
#include<stdlib.h>
#if defined(BSD) || defined(MACOS)
#include<sys/time.h>
#define FMT "%10lld "
#else
#define FMT "%10ld "
#endif
#include<sys/resource.h>
#define doit(name) pr_limits(#name,name)
static void pr_limits(char * ,int);
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
exit(0);
}
static void pr_limits(char *name,int resource)
{
struct rlimit limit;
if(getrlimit(resource,&limit)<0)
printf("getrlimit error for %s",name);
printf("%-14s ",name);
if(limit.rlim_cur==RLIM_INFINITY)
printf("(infinite) ");
else
printf(FMT,limit.rlim_cur);
if(limit.rlim_max==RLIM_INFINITY)
printf("(infinite)");
else
printf(FMT,limit.rlim_max);
putchar((int)'\n');
}
-------------------------------------------------------------------------------------
在shell下面可以用:ulimit-------------------------------------------------------------------------------------
附录:
1.字符串创建运算符(#)它后面的变量将用双引号括起来成为字符串
例如 :# define doit(name) pr_limits(#name, name) ;doit(RLIMIT_CORE)将由c预处理程序扩展为pr_limits("RLIMIT_CORE",RLIMIT_CORE);
2.合并符 (##)
" It permits separate tokens to be joined into a single token and therefore cannot be the first or last token in the macro definition."http://msdn.microsoft.com/en-us/library/09dwwt6y(v=vs.80).aspx#define paster( n ) printf_s( "token" #n " = %d", token##n )int token9 = 9;
If a macro is called with a numeric argument likeCopypaster( 9 );
the macro yieldsCopyprintf_s( "token" "9" " = %d", token9 );
which becomesCopyprintf_s( "token9 = %d", token9 );
本文介绍了一个使用C语言编写的程序,该程序能够查询并显示操作系统对进程的各种资源限制,包括最大文件大小、最大堆栈大小等。通过调用getrlimit函数获取限制信息,并根据不同的操作系统定义了相应的格式字符串。
612

被折叠的 条评论
为什么被折叠?



