?????????????????????????fd???????????pipe????????????fd??????????syscall????fs/pipe.c?ж?????pipe??pipe2??syscall????????
1 SYSCALL_DEFINE2(pipe2?? int __user *?? fildes?? int?? flags)
2 {
3     int fd[2];
4     int error;
5
6     error = do_pipe_flags(fd?? flags);
7     if (!error) {
8         if (copy_to_user(fildes?? fd?? sizeof(fd))) {
9             sys_close(fd[0]);
10             sys_close(fd[1]);
11             error = -EFAULT;
12         }
13     }
14     return error;
15 }
16
17 SYSCALL_DEFINE1(pipe?? int __user *?? fildes)
18 {
19     return sys_pipe2(fildes?? 0);
20 }
?????????????do_pipe_flags()?????????????get_unused_fd_flags(flags)??????fd????????????
????#define get_unused_fd_flags(flags) alloc_fd(0?? (flags))??λ??include/linux/fs.h??
??????????????????????????alloc_fd()?????????????????fd????????????λ??fs/file.c???????????????
1 int alloc_fd(unsigned start?? unsigned flags)
2 {
3     struct files_struct *files = current->files;
4     unsigned int fd;
5     int error;
6     struct fdtable *fdt;
7
8     spin_lock(&files->file_lock);
9 repeat:
10     fdt = files_fdtable(files);
11     fd = start;
12     if (fd < files->next_fd)
13         fd = files->next_fd;
14
15     if (fd < fdt->max_fds)
16         fd = find_next_zero_bit(fdt->open_fds->fds_bits??
17                        fdt->max_fds?? fd);
18
19     error = expand_files(files?? fd);
20     if (error < 0)
21         goto out;
22
23     /*
24      * If we needed to expand the fs array we
25      * might have blocked - try again.
26      */
27     if (error)
28         goto repeat;
29
30     if (start <= files->next_fd)
31         files->next_fd = fd + 1;
32
33     FD_SET(fd?? fdt->open_fds);
34     if (flags & O_CLOEXEC)
35         FD_SET(fd?? fdt->close_on_exec);
36     else
37         FD_CLR(fd?? fdt->close_on_exec);
38     error = fd;
39 #if 1
40     /* Sanity check */
41     if (rcu_dereference(fdt->fd[fd]) != NULL) {
42         printk(KERN_WARNING "alloc_fd: slot %d not NULL! "?? fd);
43         rcu_assign_pointer(fdt->fd[fd]?? NULL);
44     }
45 #endif
46
47 out:
48     spin_unlock(&files->file_lock);
49     return error;
50 }
??????pipe??????????start?????0?????м???????expand_files()???????????????fd????ж????????????????????????????亯??????????
????/*
????* Expand files.
????* This function will expand the file structures?? if the requested size exceeds
????* the current capacity and there is room for expansion.
????* Return <0 error code on error; 0 when nothing done; 1 when files were
????* expanded and execution may have blocked.
????* The files->file_lock should be held on entry?? and will be held on exit.
????*/
?????????????????????????alloc_fd()???????????????????fd???????
??????????????fd?С?????fd???????????????????EMFILE????????????????????fd???
?????????????????д?????????(kernel??2.6.18)?У???δ??client????????fd???仯?????????????????????????fd??8???????????????????μ??????????fd???8?????μ??????fd???????1???
????????????????????socket???fd????????????????? alloc_fd(0?? (flags)??????????????
????socket(sys_call) -> sock_map_fd() -> sock_alloc_fd() -> get_unused_fd_flags()
????open???????????get_unused_fd_flags()???????о????
???????????????????select??????????Linux??fd???????????????????????ε?fd???????С??????IO?????????????????????fd???1024???????С???????????????????select?????????????????????????????????????????????fd?С??1024???????select??????
?????????????????????????????????????????????????????????select?????