LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1113|回复: 4

资源限制程序,有一小部分看不明白

[复制链接]
发表于 2006-11-26 22:30:54 | 显示全部楼层 |阅读模式
红字部分,特别是 #name 前的 # 是什么意思:ask

  1. #include        <sys/types.h>
  2. #include        <sys/time.h>
  3. #include        <sys/resource.h>
  4. #include        "ourhdr.h"

  5. [color="Red"]#define doit(name)        pr_limits(#name, name)[/color]
  6. static void pr_limits(char *, int);

  7. int        main(int argc, char *argv[])
  8. {
  9.         doit(RLIMIT_CORE);
  10.         doit(RLIMIT_CPU);
  11.         doit(RLIMIT_DATA);
  12.         doit(RLIMIT_FSIZE);

  13. #ifdef        RLIMIT_MEMLOCK
  14.         doit(RLIMIT_MEMLOCK);
  15. #endif

  16. #ifdef RLIMIT_NOFILE
  17.         doit(RLIMIT_NOFILE);
  18. #endif

  19. #ifdef RLIMIT_OFILE
  20.         doit(RLIMIT_OFILE);
  21. #endif

  22. #ifdef RLIMIT_NPROC
  23.         doit(RLIMIT_NPROC);
  24. #endif

  25. #ifdef RLIMIT_RSS
  26.         doit(RLIMIT_RSS);
  27. #endif
  28.         doit(RLIMIT_STACK);

  29. #ifdef RLIMIT_VMEM
  30.         doit(RLIMIT_VMEM);
  31. #endif

  32. exit(0);
  33. }

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

  37.         if (getrlimit(resource, &limit) < 0)
  38.                 err_sys("getrlimit error for %s", name);
  39.         printf("%-14s        ", name);
  40.         if (limit.rlim_cur == RLIM_INFINITY)
  41.                 printf("(infinite)        ");
  42.         else
  43.                 printf("%10ld        ", limit.rlim_cur);
  44.         if (limit.rlim_max == RLIM_INFINITY)
  45.                 printf("(infinite)\n");
  46.         else
  47.                 printf("%10ld\n", limit.rlim_max);
  48. }
复制代码
发表于 2006-11-27 21:48:20 | 显示全部楼层
试一下就知道了


  1. /* t.c */

  2. #include <stdio.h>
  3. #define fn(x) printf(#x, x)
  4. int main()
  5. {
  6.         fn(1);
  7.         return 0;
  8. }
复制代码


用 gcc -E t.c | tail 查看替换后的结果

  1. int main()
  2. {
  3. printf("1", 1);
  4. return 0;
  5. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2006-11-27 21:53:17 | 显示全部楼层
这个是用来联结字符串的
回复 支持 反对

使用道具 举报

发表于 2006-11-28 10:45:05 | 显示全部楼层
google “The C Preprocessor" stringification
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-12-3 23:04:00 | 显示全部楼层
明白了,谢谢各位
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表