Kernel version:2.6.14
CPU architecture:ARM920T
我们先贴出expand_files函数的源码:
int expand_files(struct files_struct *files, int nr)
{
int err, expand = 0;
struct fdtable *fdt;
fdt = files_fdtable(files);
if (nr >= fdt->max_fdset || nr >= fdt->max_fds) { //我们在前面的文章中已经分析过,初始时max_fdset = 1024,max_fds = 32
if (fdt->max_fdset >= NR_OPEN || //#define NR_OPEN (1024*1024) /* Absolute upper limit on fd num */
fdt->max_fds >= NR_OPEN || nr >= NR_OPEN) {
err = -EMFILE; //max_fdset和max_fds都不能大于 NR_OPEN,否则返回 -EMFILE,即打开太多的文件
goto out;
}
expand = 1;
if ((err = expand_fdtable(files, nr)))//真正进行扩展
goto out;
}
err = expand;
out:
return err;
}
expand_files函数进行一些检查后调用expand_fdtable进行文件描述符表的扩展,下面分析expand_fdtable函数。
static int expand_fdtable(struct files_struct *files, int nr)
__releases(files->file_lock)
__acquires(files->file_lock)
{
int error = 0;
struct fdtable *fdt;
struct fdtable *nfdt = NULL;