Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1254631
  • 博文数量: 168
  • 博客积分: 3483
  • 博客等级: 中校
  • 技术积分: 1696
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-06 13:17
文章分类

全部博文(168)

文章存档

2015年(6)

2014年(9)

2013年(47)

2012年(11)

2011年(13)

2010年(18)

2009年(11)

2008年(42)

2007年(11)

分类: C/C++

2013-08-31 14:02:42


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <getopt.h>
  8. #include <sys/time.h>
  9. #include <sys/resource.h>
  10. #include <string.h>
  11. #include <errno.h>

  12. #define DefaultMaxFD    (40960*4)
  13. #define BUFLEN        1024
  14. #define errExit(msg) do { \
  15.     perror(msg); exit(EXIT_FAILURE); \
  16. } while (0)

  17. /*
  18. static struct option longopts[] = {
  19.   { "help", 0, 0, 'h' },
  20.   { "fd-to-expend", 0, 0, 'n' },
  21.   { NULL, 0, 0, 0 }
  22. };
  23. */

  24. void usage(void);
  25. /*----------------------------------------------------------------------------*/

  26. int main(int argc, char **argv)
  27. {
  28.     int fd = 0, c, i;
  29.     int nfd = 0;
  30.     char *cmd = NULL;
  31.     char cmdline[2048] = { 0 };
  32.     int res;
  33.     struct rlimit rlim;
  34.     unsigned long max_open = DefaultMaxFD;

  35.     //set the max open files
  36.     memset(&rlim, 0, sizeof(rlim));
  37.     if (getenv("maxOpen"))
  38.         max_open=strtoul(getenv("maxOpen"),NULL,0);
  39.     rlim.rlim_cur = max_open;
  40.     rlim.rlim_max = max_open;
  41.      res = setrlimit(RLIMIT_NOFILE, &rlim);
  42.     if (res)
  43.         perror("change open file limit failed.\n");

  44.     //debug: argv before getopt
  45.     if (getenv("xdebug")) {
  46.         for (i=0; i < argc; i++) printf("B-argv[%d] %s\n", i, argv[i]);
  47.         printf("\n");
  48.     }

  49.     while ((c = getopt_long(argc, argv, "n:ht",
  50.                 NULL, NULL)) != -1) {
  51.         switch (c) {
  52.         case 'n':
  53.             nfd=atoi(optarg);
  54.             break;
  55.         case 't':
  56.             break;
  57.         case 'h':
  58.         default:
  59.             goto out_usage;
  60.         }
  61.     }

  62.     //debug: argv after getopt
  63.     if (getenv("xdebug")) {
  64.         for (i=0; i < argc; i++) printf("A-argv[%d] %s\n", i, argv[i]);
  65.         printf("\n");
  66.         for (c=optind; c < argc; c++) printf("P=argv[%d] %s\n", c, argv[c]);
  67.         printf("\n");
  68.     }

  69.     if (optind > argc - 1)
  70.         goto out_usage;

  71.     //get open file limit
  72.      res = getrlimit(RLIMIT_NOFILE, &rlim);
  73.     if (res) {
  74.         perror("get open file limit failed.\n");
  75.     } else {
  76.         printf("==> now the open file limit: (%lu).(%lu)\n",
  77.             rlim.rlim_cur, rlim.rlim_max);
  78.     }

  79.     //trace arg value
  80.     cmd = argv[optind];
  81.     printf("==> fd to expend = [%d]\n", nfd);
  82.     printf("==> cmd = [%s]\n", cmd);

  83.     //expend the fd
  84.     fd = open("/", O_RDONLY);
  85.     printf("==> first open fd = [%d]\n", fd);
  86.     for (i = 1; i < nfd; i++) {
  87.         c = open("/", O_RDONLY);
  88.         if (c < 0) {
  89.             fprintf(stderr, "prefd = [%d] ", fd);
  90.             errExit("open fail.");
  91.         } else
  92.             fd = c;
  93.         /* yes the fd leak is intentional */
  94.     }

  95.     printf("==> now max fd = [%d]\n", fd);
  96.     for (c=optind; c < argc; c++)
  97.         sprintf(cmdline+strlen(cmdline), "%s ", argv[c]);
  98.     printf("==> cmdline = [%s]\n", cmdline);

  99.     if (getenv("useSystem")) {
  100.         res = system(cmdline);
  101.     } else {
  102.         res = execvp(argv[optind], &argv[optind]);
  103.     }

  104.     exit(res);

  105. out_usage:
  106.     usage();
  107.     exit(EXIT_FAILURE);
  108. }

  109. void usage()
  110. {
  111.     fprintf(stderr, "usage: $prog [-h] [-n fsNum] \n");
  112. }


阅读(1129) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~