Linux????械??????????:fd?????--get_unused_fd
???????????? ???????[ 2013/1/16 10:32:29 ] ????????
??????Linux???????????????????婕�????????????????get_unused_fd??locate_fd?????????????get_unused_fd?????????????????薪???locate_fd?????????get_unused_fd?????(fs/open.c)??
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!
"?? 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????????
struct rlimit {
unsigned long rlim_cur;//????
unsigned long rlim_max;//???
??????

???路???
??????????????????
2023/3/23 14:23:39???写?貌??????????
2023/3/22 16:17:39????????????????????些??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???路???????路
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11