linux內(nèi)核中的文件描述符(五)--fd的分配--locate_fd
CPU architecture:ARM920T
本文引用地址:http://m.butianyuan.cn/article/201611/320001.htmAuthor:ce123(http://blog.csdn.net/ce123)
繼續(xù)上一篇博客的內(nèi)容,分析另一個文件描述符fd的分配函數(shù)locate_fd。dup系統(tǒng)調(diào)用用于復(fù)制一個文件描述符對應(yīng)的文件,返回值是個文件描述符。在前面的文章中,我們已經(jīng)分析過了dup的源碼(http://blog.csdn.net/ce123/article/details/8444482),在這里我們深入分析locate_fd函數(shù),其定義如下:
[plain]view plaincopyprint?
- staticintlocate_fd(structfiles_struct*files,
- structfile*file,unsignedintorig_start)//從orig_start位開始分配fd
- {
- unsignedintnewfd;
- unsignedintstart;
- interror;
- structfdtable*fdt;
- error=-EINVAL;
- if(orig_start>=current->signal->rlim[RLIMIT_NOFILE].rlim_cur)//檢查orig_start是大于進(jìn)程最大可以打開文件的數(shù)量
- gotoout;
- repeat:
- fdt=files_fdtable(files);//文件描述符位圖
- /*
- *Someonemighthaveclosedfdsintherange
- *orig_start..fdt->next_fd
- */
- start=orig_start;
- if(start
next_fd) - start=fdt->next_fd;//如果orig_start小于next_fd,那就從next_fd開始分配
- newfd=start;
- if(start
max_fdset){//max_fdset是描述符問題的位數(shù),下面會具體講解 - 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)//進(jìn)行判斷,分配的fd不能大于進(jìn)程最大可以打開的文件數(shù)量
- gotoout;
- error=expand_files(files,newfd);//文件描述符表的擴(kuò)展,這個我們留在下一篇文章中詳細(xì)講解
- if(error<0)
- gotoout;
- /*
- *Ifweneededtoexpandthefsarraywe
- *mighthaveblocked-tryagain.
- */
- if(error)
- gotorepeat;
- /*
- *Wereacquiredfiles_lock,sowearesafeaslongas
- *wereacquirethefdtablepointeranduseitwhileholding
- *thelock,noonecanfreeitduringthattime.
- */
- fdt=files_fdtable(files);
- if(start<=fdt->next_fd)
- fdt->next_fd=newfd+1;//更新next_fd值
- error=newfd;
- out:
- returnerror;
- }
[plain]view plaincopyprint?
- linux/arch/arm/kernel/init_task.c
- structtask_structinit_task=INIT_TASK(init_task);
- #defineINIT_TASK(tsk)
- {
- ...
- .files=&init_files,
- ...
- }
[plain]view plaincopyprint?
- staticstructfiles_structinit_files=INIT_FILES;
- linux/init_task.h
- #defineINIT_FDTABLE
- {
- .max_fds=NR_OPEN_DEFAULT,
- .max_fdset=__FD_SETSIZE,
- .next_fd=0,
- .fd=&init_files.fd_array[0],
- .close_on_exec=&init_files.close_on_exec_init,
- .open_fds=&init_files.open_fds_init,
- .rcu=RCU_HEAD_INIT,
- .free_files=NULL,
- .next=NULL,
- }
- #defineNR_OPEN_DEFAULTBITS_PER_LONG
- #define__FD_SETSIZE1024
- #defineINIT_FILES
- {
- .count=ATOMIC_INIT(1),
- .file_lock=SPIN_LOCK_UNLOCKED,
- .fdt=&init_files.fdtab,
- .fdtab=INIT_FDTABLE,
- .close_on_exec_init={{0,}},
- .open_fds_init={{0,}},
- .fd_array={NULL,}
- }
[plain]view plaincopyprint?
- typedef__kernel_fd_setfd_set;
- #undef__NFDBITS
- #define__NFDBITS(8*sizeof(unsignedlong))
- #undef__FD_SETSIZE
- #define__FD_SETSIZE1024
- #undef__FDSET_LONGS
- #define__FDSET_LONGS(__FD_SETSIZE/__NFDBITS)
- #undef__FDELT
- #define__FDELT(d)((d)/__NFDBITS)
- #undef__FDMASK
- #define__FDMASK(d)(1UL<<((d)%__NFDBITS))
- typedefstruct{
- unsignedlongfds_bits[__FDSET_LONGS];
- }__kernel_fd_set;
評論