Linux文件系统(七)---系统调用之open操作(二) 之 get_unused_fd
Open系统调用
/* 2 */:这一步是需要找到一个没有使用的文件描述符fd
看一下sys_open函数中调用的这个函数get_unused_fd:
![](https://code.csdn.net/assets/CODE_ico.png)
- 738 /*
- 739 * Find an empty file descriptor entry, and mark it busy.
- 740 */
- 741 int get_unused_fd(void)
- 742 {
- 743 struct files_struct * files = current->files; /* (1)获得当前进程的文件打开表,这个前面已经说过了 */
- 744 int fd, error;
- 745
- 746 error = -EMFILE;
- 747 write_lock(&files->file_lock);
- 748
- 749 repeat:
- 750 fd = find_next_zero_bit(files->open_fds, /* (2)在这个进程文件打开表中寻找还没有使用的fd */
- 751 files->max_fdset,
- 752 files->next_fd);
- 753
- 754 /*
- 755 * N.B. For clone tasks sharing a files structure, this test
- 756 * will limit the total number of files that can be opened.
- 757 */
- 758 if (fd >= current->rlim[RLIMIT_NOFILE].rlim_cur) /* 不能超过限制(考虑到fork进程情况,共享fd情况) */
- 759 goto out;
- 760
- 761 /* Do we need to expand the fdset array? */
- 762 if (fd >= files->max_fdset) { /* 如果当前进程的fd集合中最大fd比当前申请的fd小,那么需要扩大fdset,需要扩容:具体见http://blog.csdn.net/shanshanpt/article/details/38943731中files_struct结构体描述 */
- 763 error = expand_fdset(files, fd); /* 扩容函数1 */
- 764 if (!error) {
- 765 error = -EMFILE;
- 766 goto repeat;
- 767 }
- 768 goto out;
- 769 }
- 770
- 771 /*
- 772 * Check whether we need to expand the fd array.
- 773 */
- 774 if (fd >= files->max_fds) { /* 类似于上面,这里是需要扩大fd-array数组,具体见上面链接 */
- 775 error = expand_fd_array(files, fd); /* 扩容函数2 */
- 776 if (!error) {
- 777 error = -EMFILE;
- 778 goto repeat;
- 779 }
- 780 goto out;
- 781 }
- 782
- 783 FD_SET(fd, files->open_fds); /* 将fd加入到打开文件描述符中 */
- 784 FD_CLR(fd, files->close_on_exec);/* 从close-on-exec中清除 */
- 785 files->next_fd = fd + 1; /* 当前描述符是最大的fd,所有next就是fd+1 */
- 786 #if 1
- 787 /* Sanity check */
- 788 if (files->fd[fd] != NULL) {
- 789 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
- 790 files->fd[fd] = NULL;
- 791 }
- 792 #endif
- 793 error = fd;
- 794
- 795 out:
- 796 write_unlock(&files->file_lock);
- 797 return error;
- 798 }
- 799
ATTENTION:注意上面的max_fdset和max_fds的区别,前者是当前可以容纳的最大的文件描述符的数量大小!后者是当前可以容纳的文件对象的数量大小!对于一个文件对象而言,可以存在多个文件描述符指向这一个文件对象!所以对于申请到的fd,要分别和这两个进行判断!
主要看上面的三个函数,第一个是寻找fd函数find_next_zero_bit:
这个函数的意义就是找到open_fds打开的文件描述符中第一个bit位=0的那一位,作为新的fd返回。(前面也说过,fd的管理是使用位管理的),反正是一堆位运算,慢慢看吧:
![](https://code.csdn.net/assets/CODE_ico.png)
- 254 static inline unsigned long find_next_zero_bit(void *addr, unsigned long size, unsigned long offset)
- 255 {
- 256 unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
- 257 unsigned long result = offset & ~31UL;
- 258 unsigned long tmp;
- 259
- 260 if (offset >= size)
- 261 return size;
- 262 size -= result;
- 263 offset &= 31UL;
- 264 if (offset) {
- 265 tmp = *(p++);
- 266 tmp |= ~0UL >> (32-offset);
- 267 if (size < 32)
- 268 goto found_first;
- 269 if (~tmp)
- 270 goto found_middle;
- 271 size -= 32;
- 272 result += 32;
- 273 }
- 274 while (size & ~31UL) {
- 275 if (~(tmp = *(p++)))
- 276 goto found_middle;
- 277 result += 32;
- 278 size -= 32;
- 279 }
- 280 if (!size)
- 281 return result;
- 282 tmp = *p;
- 283
- 284 found_first:
- 285 tmp |= ~0UL << size;
- 286 if (tmp == ~0UL) /* Are any bits zero? */
- 287 return result + size; /* Nope. */
- 288 found_middle:
- 289 return result + ffz(tmp);
- 290 }
扩大fdset集合函数:expand_fdset
![](https://code.csdn.net/assets/CODE_ico.png)
- 162 /*
- 163 * Expand the fdset in the files_struct. Called with the files spinlock
- 164 * held for write.
- 165 */
- 166 int expand_fdset(struct files_struct *files, int nr)
- 167 {
- 168 fd_set *new_openset = 0, *new_execset = 0;
- 169 int error, nfds = 0;
- 170
- 171 error = -EMFILE;
- 172 if (files->max_fdset >= NR_OPEN || nr >= NR_OPEN) /* 不能大于系统最大限制 */
- 173 goto out;
- 174
- 175 nfds = files->max_fdset; /* 当前最大文件描述符 */
- 176 write_unlock(&files->file_lock);
- 177
- 178 /* Expand to the max in easy steps,下面是一个 简单的扩展过程 */
- 179 do {
- 180 if (nfds < (PAGE_SIZE * 8))
- 181 nfds = PAGE_SIZE * 8;
- 182 else {
- 183 nfds = nfds * 2;
- 184 if (nfds > NR_OPEN)
- 185 nfds = NR_OPEN;
- 186 }
- 187 } while (nfds <= nr);
- 188
- 189 error = -ENOMEM;
- 190 new_openset = alloc_fdset(nfds); /* 根据新的大小分配新的打开set集合 */
- 191 new_execset = alloc_fdset(nfds); /* 根据新的大小分配新的执行时候需要close的set集合 */
- 192 write_lock(&files->file_lock);
- 193 if (!new_openset || !new_execset)
- 194 goto out;
- 195
- 196 error = 0;
- 197
- 198 /* Copy the existing tables and install the new pointers:将老的数据拷贝到新的内存中来 */
- 199 if (nfds > files->max_fdset) {
- 200 int i = files->max_fdset / (sizeof(unsigned long) * 8);
- 201 int count = (nfds - files->max_fdset) / 8;
- 202
- 203 /*
- 204 * Don't copy the entire array if the current fdset is
- 205 * not yet initialised.
- 206 */
- 207 if (i) {
- 208 memcpy (new_openset, files->open_fds, files->max_fdset/8);
- 209 memcpy (new_execset, files->close_on_exec, files->max_fdset/8);
- 210 memset (&new_openset->fds_bits[i], 0, count);
- 211 memset (&new_execset->fds_bits[i], 0, count);
- 212 }
- 213 /* 下面几步骤很重要,将新分配的挂载到files结构体中去 */
- 214 nfds = xchg(&files->max_fdset, nfds);
- 215 new_openset = xchg(&files->open_fds, new_openset);
- 216 new_execset = xchg(&files->close_on_exec, new_execset);
- 217 write_unlock(&files->file_lock);
- 218 free_fdset (new_openset, nfds);
- 219 free_fdset (new_execset, nfds);
- 220 write_lock(&files->file_lock);
- 221 return 0;
- 222 }
- 223 /* Somebody expanded the array while we slept ... */
- 224
- 225 out:
- 226 write_unlock(&files->file_lock);
- 227 if (new_openset)
- 228 free_fdset(new_openset, nfds);
- 229 if (new_execset)
- 230 free_fdset(new_execset, nfds);
- 231 write_lock(&files->file_lock);
- 232 return error;
- 233 }
再看一下具体的alloc_fdset函数:
![](https://code.csdn.net/assets/CODE_ico.png)
- 128 /*
- 129 * Allocate an fdset array, using kmalloc or vmalloc.
- 130 * Note: the array isn't cleared at allocation time.
- 131 */
- 132 fd_set * alloc_fdset(int num)
- 133 {
- 134 fd_set *new_fdset;
- 135 int size = num / 8;
- 136
- 137 if (size <= PAGE_SIZE)
- 138 new_fdset = (fd_set *) kmalloc(size, GFP_KERNEL);
- 139 else
- 140 new_fdset = (fd_set *) vmalloc(size);
- 141 return new_fdset;
- 142 }
回到上面,看一下扩大fd数组的函数expand_fd_array:
![](https://code.csdn.net/assets/CODE_ico.png)
- 52 /*
- 53 * Expand the fd array in the files_struct. Called with the files
- 54 * spinlock held for write.
- 55 */
- 56
- 57 int expand_fd_array(struct files_struct *files, int nr)
- 58 {
- 59 struct file **new_fds;
- 60 int error, nfds;
- 61
- 62
- 63 error = -EMFILE;
- 64 if (files->max_fds >= NR_OPEN || nr >= NR_OPEN) /* 不能超过最大系统限制 */
- 65 goto out;
- 66
- 67 nfds = files->max_fds; /* 当前进程中最大的fd */
- 68 write_unlock(&files->file_lock);
- 69
- 70 /*
- 71 * Expand to the max in easy steps, and keep expanding it until
- 72 * we have enough for the requested fd array size.
- 73 */
- 74 /* 简单的扩展策略 */
- 75 do {
- 76 #if NR_OPEN_DEFAULT < 256
- 77 if (nfds < 256)
- 78 nfds = 256;
- 79 else
- 80 #endif
- 81 if (nfds < (PAGE_SIZE / sizeof(struct file *)))
- 82 nfds = PAGE_SIZE / sizeof(struct file *);
- 83 else {
- 84 nfds = nfds * 2;
- 85 if (nfds > NR_OPEN)
- 86 nfds = NR_OPEN;
- 87 }
- 88 } while (nfds <= nr);
- 89
- 90 error = -ENOMEM;
- 91 new_fds = alloc_fd_array(nfds); /* 分配新的fd_array数组 */
- 92 write_lock(&files->file_lock);
- 93 if (!new_fds)
- 94 goto out;
- 95
- 96 /* Copy the existing array and install the new pointer */
- 97
- 98 if (nfds > files->max_fds) {
- 99 struct file **old_fds;
- 100 int i;
- 101 /* 将当前进行的文件数组指针指向新申请的fd数组! */
- 102 old_fds = xchg(&files->fd, new_fds);
- 103 i = xchg(&files->max_fds, nfds);
- 104
- 105 /* Don't copy/clear the array if we are creating a new
- 106 fd array for fork() */
- 107 if (i) { /* 下面将老的数据拷贝过去 */
- 108 memcpy(new_fds, old_fds, i * sizeof(struct file *));
- 109 /* clear the remainder of the array */
- 110 memset(&new_fds[i], 0,
- 111 (nfds-i) * sizeof(struct file *));
- 112
- 113 write_unlock(&files->file_lock);
- 114 free_fd_array(old_fds, i);
- 115 write_lock(&files->file_lock);
- 116 }
- 117 } else {
- 118 /* Somebody expanded the array while we slept ... */
- 119 write_unlock(&files->file_lock);
- 120 free_fd_array(new_fds, nfds);
- 121 write_lock(&files->file_lock);
- 122 }
- 123 error = 0;
- 124 out:
- 125 return error;
- 126 }
Linux文件系统(七)---系统调用之open操作(二) 之 get_unused_fd相关推荐
- Linux文件系统及磁盘分区与格式化(二次排版)
Linux 的文件系统 磁盘物理组成 磁盘的组成与分区: 由图中可以看到磁盘中存储数据的部件为圆形的盘片(磁盘主要记录数据的部分). 盘片的结构 而磁盘的物理 ...
- linux 如何赋值目录,Linux文件系统之目录的建立
一:前言 在用户空间中,建立目录所用的API为mkdir().它在内核中的系统调用入口是sys_mkdir().今天跟踪一下 函数来分析linux文件系统中目录的建立过程. 二:sys_mkdir() ...
- linux创建根目录代码,Linux文件系统之目录的建立
一:前言 在用户空间中,建立目录所用的API为mkdir().它在内核中的系统调用入口是sys_mkdir().今天跟踪一下 函数来分析linux文件系统中目录的建立过程. 二:sys_mkdir() ...
- Spark修炼之道(基础篇)——Linux大数据开发基础:第二节:Linux文件系统、文件夹(一)...
本节主要内容 怎样获取帮助文档 Linux文件系统简单介绍 文件夹操作 訪问权限 1. 怎样获取帮助文档 在实际工作过程其中,常常会忘记命令的使用方式.比如ls命令后面能够跟哪些參数,此时能够使用ma ...
- Linux里命令卸载mysql,linux中mysql完整卸载命令操作
yum方式安装的mysql 1.yum remove mysql mysql-server mysql-libs compat-mysql51 2.rm -rf /var/lib/mysql 3.rm ...
- Linux 文件系统与设备文件系统 (二)—— sysfs 文件系统与Linux设备模型
提到 sysfs 文件系统 ,必须先需要了解的是Linux设备模型,什么是Linux设备模型呢? 一.Linux 设备模型 1.设备模型概述 从2.6版本开始,Linux开发团队便为内核建立起一个统一 ...
- Linux文件系统二(虚拟文件系统VFS实现原理)
创作人QQ:851301776,邮箱:lfr890207@163.com 欢迎大家一起技术交流,本博客主要是自己学习的心得体会,只为每天进步一点点! 个人座右铭: 1 ...
- 嵌入式 Linux 入门(二、Linux 文件系统、文件类型及权限管理)
嵌入式 Linux入 门第二课, linux 文件系统.文件类型及权限管理. ...... 矜辰所致 目录 前言 一.Linux 文件属性 1.1 Linux 文件类型 1.2 Linux 文件权限及 ...
- 文件系统调用和Linux文件系统基础
文件系统调用和Linux文件系统基础 keywords fdisk.LBA.CHS.MBR.super struct.directory.file.inode.inode table.block.fi ...
- 第十一、十二、十三、十四章 网络配置管理、归档和远程复制同步文件、软件包管理、创建访问linux文件系统
第十一章 网络配置管理 网络地址获取方式: 1)DHCP自动获取 2)手动配置 1.网卡配置文件: /etc/sysconfig/network-scripts/ [root@server0 Desk ...
最新文章
- mysql安装文件瘦身_MySQL瘦身
- Python sort
- iOS11和机器学习CoreML库
- 130.CDMA全称是什么?
- Python 基础教程:切片、迭代和列表生成式
- 最舒适的路线 第六届
- 蓝桥杯2017初赛-9数算式-dfs
- 【Git】GitHub设置README.md引用本repo目录下的图片
- Grunt 之通配符
- URLencode 转义符
- C语言 完数C语言 完数C语言 完数
- pdf打开错误之读取本文档时出现问题(14)
- 国内用户最多的linux系统,统信UOS将可能超越麒麟系统夺得中国Linux市场份额第一名...
- 性能优化: 资源合并与压缩 -- 压缩(前端开发过程中 JavaScript、HTML、CSS 文件的压缩)
- 天津学习平面设计培训需要学习多久?
- linux上ssh免密登录原理及实现
- C语言到底有多强大?
- 在K8s上轻松部署Tungsten Fabric的两种方式
- 刺激汽车消费政策频出的背后揭露了什么样的车市真相?
- 搭建gitblit服务器