Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15372368
  • 博文数量: 2005
  • 博客积分: 11986
  • 博客等级: 上将
  • 技术积分: 22535
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-17 13:56
文章分类

全部博文(2005)

文章存档

2014年(2)

2013年(2)

2012年(16)

2011年(66)

2010年(368)

2009年(743)

2008年(491)

2007年(317)

分类: LINUX

2008-06-10 11:58:22

sys_open会从files->next_fd开始寻找第一个出现的空闲fd

fd = find_next_zero_bit(fdt->open_fds->fds_bits, fdt->max_fds,
                files->next_fd)

sys_close关闭文件时,如果关闭的文件fd比files->next_fd大,那么将files->next_fd指向
这个刚刚关闭的fd,所以保证,sys_open返回的最大fd值确实因为同时打开过最大fd值那么多的文件
    struct fdtable *fdt = files_fdtable(files);
    __FD_CLR(fd, fdt->open_fds);
    if (fd < files->next_fd)
        files->next_fd = fd;

fd_set集合在我的系统上默认为1024,所以程序使用selcet最多可以同时监控1024个fd文件,
如果同时打开文件超出1024个,那么select将无法管理超出去的那些被打开的文件
(注:经过select执行之后,会自动fd_set集合中没有发生事件的相应fd位置0)


#include
#include
#include
#include

int main(int argc, char *argv[])
{
    int i;
    char buf[10];
    int outfile_handle;

    for(i = 0;i < 10;i++)
    {
        sprintf(buf, "%d", i);
        outfile_handle = open(buf, O_RDWR | O_CREAT | O_TRUNC, 0666);
        fprintf(stderr, ":%i -- handle=%i\n", i, outfile_handle);
        if(i > 5)close(outfile_handle);
    }
}
输出:
:0 -- handle=3
:1 -- handle=4
:2 -- handle=5
:3 -- handle=6
:4 -- handle=7
:5 -- handle=8
:6 -- handle=9
:7 -- handle=9
:8 -- handle=9
:9 -- handle=9

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