在 Linux 內核中,主要有兩個函式涉及檔案描述符的分配:get_unused_fd 與 locate_fd。本文先講 get_unused_fd。(環境:2.6.14、ARM920T)
int get_unused_fd(void)
{
struct files_struct * files = current->files;//獲得當前進程的打開文件列表 files
int fd, error;
struct fdtable *fdt;
error = -EMFILE;
spin_lock(&files->file_lock);
repeat:
fdt = files_fdtable(files);//獲得文件描述符位圖結構
fd = find_next_zero_bit(fdt->open_fds->fds_bits,
fdt->max_fdset,
fdt->next_fd);
//find_next_zero_bit 在文件描述符位圖 fds_bits 中從 next_fd 位開始搜索下一個(包括 next_fd)為 0 的位,也就是分配一個文件描述符
/*
* N.B. For clone tasks sharing a files structure, this test
* will limit the total number of files that can be opened.
*/
if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)//檢查是否超過當前進程限定的最大可打開文件數
goto out;
/* Do we need to expand the fd array or fd set? */
error = expand_files(files, fd);//根據需要擴展 fd。返回值<0,錯誤;返回值>0,擴展後再次進行 fd 的分配
if (error < 0)
goto out;
if (error) {
/*
* If we needed to expand the fs array we
* might have blocked - try again.
*/
error = -EMFILE;
goto repeat;//之前進行了擴展操作,重新進行一次空閒 fd 的分配
}
FD_SET(fd, fdt->open_fds);//在 open_fds 的位圖上置位
FD_CLR(fd, fdt->close_on_exec);
fdt->next_fd = fd + 1;//next_fd 加 1
#if 1
/* Sanity check */
if (fdt->fd[fd] != NULL) {
printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
fdt->fd[fd] = NULL;
}
#endif
error = fd;
out:
spin_unlock(&files->file_lock);
return error;
}
current->signal->rlim[RLIMIT_NOFILE].rlim_cur 就是一個進程可以打開的最大檔案數。RLIMIT_NOFILE 定義如下:
# define RLIMIT_NOFILE 7 /* max number of open files */
在 signal 結構中,rlim 是 struct rlimit 型別的陣列:
struct signal_struct {
...
struct rlimit rlim[RLIM_NLIMITS];
...
};
struct rlimit {
unsigned long rlim_cur;//當前值
unsigned long rlim_max;//最大值
};
Linux 透過 fork 建立進程,而第一個進程是靜態定義的。因此若進程建立後沒有修改這些值,它們就與第一個進程相同。第一個進程的 task_struct(節錄):
linux/arch/arm/kernel/init_task.c
struct task_struct init_task = INIT_TASK(init_task);
#define INIT_TASK(tsk) \
{ \
...
.signal = &init_signals, \
...
}
#define INIT_SIGNALS(sig) {\
.count = ATOMIC_INIT(1), \
.wait_chldexit = __WAIT_QUEUE_HEAD_INITIALIZER(sig.wait_chldexit),\
.shared_pending = { \
.list = LIST_HEAD_INIT(sig.shared_pending.list),\
.signal = {{0}}},\
.posix_timers = LIST_HEAD_INIT(sig.posix_timers), \
.cpu_timers = INIT_CPU_TIMERS(sig.cpu_timers), \
.rlim = INIT_RLIMITS, \
}
include\asm-generic\resource.h
#define INIT_RLIMITS \
{ \
[RLIMIT_CPU] = { RLIM_INFINITY, RLIM_INFINITY }, \
[RLIMIT_FSIZE] = { RLIM_INFINITY, RLIM_INFINITY }, \
[RLIMIT_DATA] = { RLIM_INFINITY, RLIM_INFINITY }, \
[RLIMIT_STACK] = { _STK_LIM, _STK_LIM_MAX }, \
[RLIMIT_CORE] = { 0, RLIM_INFINITY }, \
[RLIMIT_RSS] = { RLIM_INFINITY, RLIM_INFINITY }, \
[RLIMIT_NPROC] = { 0, 0 }, \
[RLIMIT_NOFILE] = { INR_OPEN, INR_OPEN }, \
[RLIMIT_MEMLOCK] = { MLOCK_LIMIT, MLOCK_LIMIT }, \
[RLIMIT_AS] = { RLIM_INFINITY, RLIM_INFINITY }, \
[RLIMIT_LOCKS] = { RLIM_INFINITY, RLIM_INFINITY }, \
[RLIMIT_SIGPENDING] = { 0, 0 }, \
[RLIMIT_MSGQUEUE] = { MQ_BYTES_MAX, MQ_BYTES_MAX }, \
[RLIMIT_NICE] = { 0, 0 }, \
[RLIMIT_RTPRIO] = { 0, 0 }, \
}
#define NR_OPEN (1024*1024) /* Absolute upper limit on fd num */
#define INR_OPEN 1024 /* Initial setting for nfile rlimits */
由此可見 rlim_cur = 1024,也就是一個進程預設最多可以打開 1024 個檔案;而 NR_OPEN(1024×1024)則是系統層面的絕對上限。理解這一層,就能明白為何 expand_files 中要拿 NR_OPEN 來做硬性把關。