Unix环境高级编程(7-8 打印当前资源限制)c语言宏定义中的#,##

#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 like
Copypaster( 9 );

the macro yields
Copyprintf_s( "token" "9" " = %d", token9 );

which becomes
Copyprintf_s( "token9 = %d", token9 );

你可能感兴趣的:(Unix环境高级编程(7-8 打印当前资源限制)c语言宏定义中的#,##)