linux核心中的檔案描述符(六)–fd的分配–expand_files

來源:互聯網
上載者:User

linux核心中的檔案描述符(六)--fd的分配--expand_files

Kernel version:2.6.14

CPU architecture:ARM920T

Author:ce123(http://blog.csdn.net/ce123)

我們先貼出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 = 32if (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;spin_unlock(&files->file_lock);nfdt = alloc_fdtable(nr);//根據nr重新建立一個新的fdtableif (!nfdt) {error = -ENOMEM;spin_lock(&files->file_lock);goto out;}spin_lock(&files->file_lock);fdt = files_fdtable(files);/* * Check again since another task may have expanded the * fd table while we dropped the lock */if (nr >= fdt->max_fds || nr >= fdt->max_fdset) { //nr值必須大於max_fds和max_fdset值,這裡再次進行檢查是防止另一個進程進行了expandcopy_fdtable(nfdt, fdt); //將舊的fdtable中的內容拷貝至新的fdtable} else {/* Somebody expanded while we dropped file_lock */spin_unlock(&files->file_lock);__free_fdtable(nfdt);spin_lock(&files->file_lock);goto out;}rcu_assign_pointer(files->fdt, nfdt);//用新的fdtable替換舊的fdtable  free_fdtable(fdt);//釋放舊的fdtable out:return error;}

我們再來看一下擴充檔案描述符表的關鍵函數alloc_fdtable,其定義如下:

static struct fdtable *alloc_fdtable(int nr){struct fdtable *fdt = NULL;int nfds = 0;  fd_set *new_openset = NULL, *new_execset = NULL;struct file **new_fds;fdt = kmalloc(sizeof(*fdt), GFP_KERNEL);if (!fdt)  goto out;memset(fdt, 0, sizeof(*fdt));nfds = __FD_SETSIZE;  //#define __FD_SETSIZE1024          //  #define PAGE_SHIFT12          //  #define PAGE_SIZE(1UL << PAGE_SHIFT)  /* Expand to the max in easy steps */  do {if (nfds < (PAGE_SIZE * 8))//dfds = 1024nfds = PAGE_SIZE * 8;else {nfds = nfds * 2;if (nfds > NR_OPEN)nfds = NR_OPEN;}} while (nfds <= nr);//第一次expand時,nr應該等於32  new_openset = alloc_fdset(nfds);//分配開啟檔案位元影像  new_execset = alloc_fdset(nfds);  if (!new_openset || !new_execset)  goto out;fdt->open_fds = new_openset;fdt->close_on_exec = new_execset;fdt->max_fdset = nfds;//更新max_fdset值,此時這個值為32knfds = NR_OPEN_DEFAULT;//nfds = 32/* * Expand to the max in easy steps, and keep expanding it until * we have enough for the requested fd array size. */do {#if NR_OPEN_DEFAULT < 256if (nfds < 256)nfds = 256;//nfds = 256(32->256->1024)//無法超過1024,因為在最開始的就進行了檢查,一定要小於current->signal->rlim[RLIMIT_NOFILE].rlim_cur)else#endifif (nfds < (PAGE_SIZE / sizeof(struct file *)))nfds = PAGE_SIZE / sizeof(struct file *);else {nfds = nfds * 2;if (nfds > NR_OPEN)nfds = NR_OPEN;  }} while (nfds <= nr);new_fds = alloc_fd_array(nfds);//分配檔案描述符數組if (!new_fds)goto out;fdt->fd = new_fds;fdt->max_fds = nfds;//更新max_fdsfdt->free_files = NULL;return fdt;out:  if (new_openset)  free_fdset(new_openset, nfds);  if (new_execset)  free_fdset(new_execset, nfds);kfree(fdt);return NULL;}

alloc_fd_array和alloc_fdset採用kmalloc或者vmalloc進行記憶體配置。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.