LinuxSir.cn,穿越时空的Linuxsir!

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

怎么知道一个进程能同时打开多少个描述字用C程序?

[复制链接]
发表于 2004-4-2 18:48:07 | 显示全部楼层 |阅读模式

  1. #include <fcntl.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. int
  8. main(void)
  9. {
  10.    unsigned int  max[1028];
  11.    unsigned  count;
  12.    for(count=3;count<1028;count++) {
  13.          if((max[count]=open("/etc/fstab",O_RDONLY))<0) {
  14.                 printf("                   create descriptor %d error\n",max[count]);
  15.                 return(1);
  16.          }else
  17.          printf("descriptor is %d\n",max[count]);
  18.    }
  19.    printf("done\n");
  20.    return(0);
  21. }
  22. #./a.out
  23. ..........................
  24. ..........................
  25. descriptor is 1022
  26. descriptor is 1023
  27. descriptor is -1
  28. descriptor is -1
  29. descriptor is -1
  30. descriptor is -1
  31. done

复制代码

RedHat 9.0的FD_SETSIZE的值是1024.
 楼主| 发表于 2004-4-2 19:35:43 | 显示全部楼层
我太粗心了。。。。

现在已改正。

好象还有点问题。。。
为什么这行没有起作用?

  1.                 printf("                             create descriptor %d error\n",max[count]);
复制代码

发表于 2004-4-3 08:55:31 | 显示全部楼层
呵呵,能不出错吗? 看看你这句:

  1. unsigned int  max[1028];
复制代码

max被声明成无符号数,-1对于它来说就成了2^32-1,当然大于0啦。改成:

  1. int  max[1028];
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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