请注意:这个问题不是( One element array in struct)的重复
以下代码摘自Linux内核源码(版本:3.14)
struct files_struct { atomic_t count; struct fdtable __rcu *fdt; struct fdtable fdtab; spinlock_t file_lock ____cacheline_aligned_in_smp; int next_fd; unsigned long close_on_exec_init[1]; unsigned long open_fds_init[1]; struct file __rcu * fd_array[NR_OPEN_DEFAULT]; };
我只是想知道为什么close_on_exec_init和open_fds_init被定义为包含一个元素的数组,而不是仅仅被定义为unsigned long close_on_exec_init;和unsigned long open_fds_init ;.
解决方法
当分配了file_struct时,close_on_exec_init字段为fdt-> close_on_exec提供初始存储. (请参阅fs / file.c中的dup_fd.)
如果相应的文件描述符设置了“close-on-exec”标志,则设置fdt-> close_on_exec的每个位.因此,Linux只需要为fdt-> close_on_exec分配额外的空间,如果进程具有比无符号长度的位数更多的打开文件描述符.
open_fds_init字段为fdt-> open_fds字段提供相同的功能. fd_array字段为fdt-> fd字段提供相同的功能. (请注意,fd_array的大小为BITS_PER_LONG.)
close_on_exec_init和open_fds_init字段以前的类型为struct embedded_fd_set,但是在this commit中更改为裸阵列.提交消息不能解释为什么作者选择使用单元素数组而不是使用裸标量.也许作者(David Howells)只是想避免使用&操作符.
原文地址:https://www.jb51.cc/c/114019.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。