首页 / 操作系统 / Linux / Linux内核中的文件描述符(五)--fd的分配--locate_fd
Kernel version:2.6.14CPU architecture:ARM920T继续上一篇文章的内容,分析另一个文件描述符fd的分配函数locate_fd。dup系统调用用于复制一个文件描述符对应的文件,返回值是个文件描述符。在前面的文章中,我们已经分析过了dup的源码(http://www.linuxidc.com/Linux/2012-12/77128.htm),在这里我们深入分析locate_fd函数,其定义如下:static int locate_fd(struct files_struct *files, struct file *file, unsigned int orig_start)//从orig_start位开始分配fd { unsigned int newfd; unsigned int start; int error; struct fdtable *fdt; error = -EINVAL; if (orig_start >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)//检查orig_start是大于进程最大可以打开文件的数量 goto out;repeat: fdt = files_fdtable(files);//文件描述符位图 /* * Someone might have closed fd"s in the range * orig_start..fdt->next_fd */ start = orig_start; if (start < fdt->next_fd) start = fdt->next_fd;//如果orig_start小于next_fd,那就从next_fd开始分配 newfd = start; if (start < fdt->max_fdset) {//max_fdset是描述符问题的位数,下面会具体讲解 newfd = find_next_zero_bit(fdt->open_fds->fds_bits, fdt->max_fdset, start);//分配fd }
error = -EMFILE; if (newfd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)//进行判断,分配的fd不能大于进程最大可以打开的文件数量 goto out; error = expand_files(files, newfd);//文件描述符表的扩展,这个我们留在下一篇文章中详细讲解 if (error < 0) goto out; /* * If we needed to expand the fs array we * might have blocked - try again. */ if (error) goto repeat; /* * We reacquired files_lock, so we are safe as long as * we reacquire the fdtable pointer and use it while holding * the lock, no one can free it during that time. */ fdt = files_fdtable(files); if (start <= fdt->next_fd) fdt->next_fd = newfd + 1;//更新next_fd值 error = newfd;