蓝森林首页 | 返回主页 | 本站地图 | 站内搜索 | 联系信箱 |
 您目前的位置:首页 > 自由软件 > 技术交流 > 应用编程


    

蓝森林 http://www.lslnet.com 2006年6月6日 10:18


#define doit(name) pr_limits(#name, name) #name这个也可以?

#include        <sys/types.h>
#include        <sys/time.h>
#include        <sys/resource.h>
#include        "ourhdr.h"

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

static void        pr_limits(char *, int);

int
main(void)
{
        doit(RLIMIT_CORE);
        doit(RLIMIT_CPU);
        doit(RLIMIT_DATA);
        doit(RLIMIT_FSIZE);
#ifdef        RLIMIT_MEMLOCK
        doit(RLIMIT_MEMLOCK);
#endif
#ifdef        RLIMIT_NOFILE        /* SVR4 name */
        doit(RLIMIT_NOFILE);
#endif
#ifdef        RLIMIT_OFILE        /* 44BSD name */
        doit(RLIMIT_OFILE);
#endif
#ifdef        RLIMIT_NPROC
        doit(RLIMIT_NPROC);
#endif
#ifdef        RLIMIT_RSS
        doit(RLIMIT_RSS);
#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)
                err_sys("getrlimit error for %s", name);
        printf("%-14s  ", name);
        if (limit.rlim_cur == RLIM_INFINITY)
                printf("(infinite)  ");
        else
                printf("%10ld  ", limit.rlim_cur);
        if (limit.rlim_max == RLIM_INFINITY)
                printf("(infinite)\n");
        else
                printf("%10ld\n", limit.rlim_max);
}

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

#name这个东西,看不懂,这也可以?
有人介绍下吗?

有点意思,去查了下资料

[code]
# include <sys/time.h>
# include <sys/resource.h>
int getrlimit(int resource,struct rlimit *rlim);
int setrlimit(int resource,conststruct rlimit *rlim);
进程的资源限制通常在系统初始化时由0进程建立,然后由后续进程继承。
# define doit(name)   pr_limits(#name,name)其中,后一个“#”是“字符串创建算符”,
例如:name=abc;#name=”abc”;
实验证实:该“字符串创建算符”必须在宏定义中,作为函数的参数来使用。
[/code]


楼主是否在看APUE,里面有这样的文字
[code]
在d o i t宏中使用了新的ANSI C字符串创建算符( # ),以便为每个资源名产生字符串值。
例如:
d o i t ( R L I M I T _ C O R E ) ;
这将由C预处理程序扩展为:
pr_limits("RLIMIT_CORE", RLIMIT_CORE);
[/code]

1 可以使用cpp观察预处理结果
2 参考C标准有:
[code]
The # operator:
Constraints
1 Each # preprocessing token in the replacement list for a function-like macro shall be
followed by a parameter as the next preprocessing token in the replacement list.
Semantics
2 If, in the replacement list, a parameter is immediately preceded by a # preprocessing
token, both are replaced by a single character string literal preprocessing token that
contains the spelling of the preprocessing token sequence for the corresponding
argument. Each occurrence of white space between the argument’s preprocessing tokens
becomes a single space character in the character string literal. White space before the
first preprocessing token and after the last preprocessing token composing the argument
is deleted. Otherwise, the original spelling of each preprocessing token in the argument
is retained in the character string literal, except for special handling for producing the
spelling of string literals and character constants: a \ character is inserted before each "
and \ character of a character constant or string literal (including the delimiting "
characters), except that it is implementation-defined whether a \ character is inserted
before the \ character beginning a universal character name. If the replacement that
results is not a valid character string literal, the behavior is undefined. The character
string literal corresponding to an empty argument is "". The order of evaluation of # and
## operators is unspecified.
[/code]
最重要的一句:
[b]If, in the replacement list, a parameter is immediately preceded by a # preprocessing
token, both are replaced by a single character string literal preprocessing token that
contains the spelling of the preprocessing token sequence for the corresponding
argument. [/b]
标准中关于character string literal的解释:包含在双引号之内的0个或多个字节字符串序列.
[code]
A character string literal is a sequence of zero or more multibyte characters enclosed in
double-quotes, as in "xyz".
[/code]
相关的名词太多,不好翻译,大概意思就是:将#和之后紧跟的预处理记号替换为包含在双引号之内的与该处理记号相应的拼写序列.
为什么说是拼写序列呢,是因为对预处理记号中的空白有特殊的要求:在预处理记号中的每个空白替换为一个单独的空格,第一个预处理记号之前和最后一个预处理记号之后的空白被删除。另外对于\还有特殊的要求.
举一些例子:
首先是来自C标准中的例子,说明了#的用法:
[code]
EXAMPLE In the following fragment:
#define hash_hash # ## #
#define mkstr(a) # a
#define in_between(a) mkstr(a)
#define join(c, d) in_between(c hash_hash d)
char p[] = join(x, y); // equivalent to
// char p[] = "x ## y";
The expansion produces, at various stages:
join(x, y)
in_between(x hash_hash y)
in_between(x ## y)
mkstr(x ## y)
"x ## y"
[/code]
这个例子显示了对空白的处理
[code]
mkstr( a   b       c   )
被替换为
"a b c"
[/code]

-->
是的,谢谢大家了!



Copyright © 1999-2000 LSLNET.COM. All rights reserved. 蓝森林网站 版权所有。 E-mail : webmaster@lslnet.com