更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
fd_table.h
浏览该文件的文档.
1/*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifndef __INCLUDE_FS_FDTABLE_H
33#define __INCLUDE_FS_FDTABLE_H
34
35#include <limits.h>
36#include <sys/select.h>
37#include <semaphore.h>
38#include "linux/spinlock.h"
39
40/**
41 * @file fb_table.h
42 * @verbatim
43 fd:文件描述符(file descriptor)也叫文件句柄.
44 为了高效管理已被打开的文件所创建的索引,是一个非负整数,本质上一个凭证.凭证上岗.
45 fd只是个索引, 顺藤摸瓜, fd -> file -> vnode -> 块/字符驱动程序
46
47 当应用程序请求内核打开/新建一个文件时,内核会返回一个文件描述符用于对应这个打开/新建的文件,
48 读写文件也是需要使用这个文件描述符来指定待读写的文件的。
49 鸿蒙和LINUX一样,一切皆为文件,系统中,所有的文件操作,都是通过fd来定位资源和状态的
50 fd有两个好处, 1.安全,对用户透明,不需要知道具体怎么实现的,凭编号拿结果 2.方便扩展
51 -----------------------------------------------------------------------------
52
53 int select(int n, fd_set *restrict rfds, fd_set *restrict wfds, fd_set *restrict efds, struct timeval *restrict tv)
54 见于..\third_party\musl\src\select\select.c
55 https://blog.csdn.net/starflame/article/details/7860091
56
57 理解select模型的关键在于理解fd_set,为说明方便,取fd_set长度为1字节,fd_set中的每一bit可以对应一个文件描述符fd。
58 则1字节长的fd_set最大可以对应8个fd。
59
60 typedef struct fd_set
61 {
62 unsigned char fd_bits [(FD_SETSIZE+7)/8];//用一位来表示一个FD
63 } fd_set;
64
65 fd_set:
66 select()机制中提供一fd_set的数据结构,实际上是一long类型的数组,每一个数组元素都能与一打开的文件句柄
67 (不管是socket句柄,还是其他文件或命名管道或设备句柄)建立联系,建立联系的工作由程序员完成,当调用select()时,
68 由内核根据IO状态修改fd_set的内容,由此来通知执行了select()的进程哪一socket或文件发生了可读或可写事件。
69
70 process fd 和 system fd 的区别和联系
71 process fd(进程FD):每个进程都有一个属于自己的files_struct
72 system fd(系统FD):整个系统共用的fd表
73 二者的关系描述为:files_struct->ft_fds[进程FD].sysFd = 系统FD ;
74 * @endverbatim
75 * @brief
76 */
77
78/* open file table for process fd */
79/// 进程fd <--> 系统FD绑定 sysFd的默认值是-1
81 intptr_t sysFd; /* system fd associate with the tg_filelist index */
82};
83/// 进程fd表结构体
84struct fd_table_s {
85 unsigned int max_fds;///< 进程的文件描述符最多有256个
86 struct file_table_s *ft_fds; /* process fd array associate with system fd | 系统分配给进程的FD数组 ,fd 默认是 -1*/
87 fd_set *proc_fds; ///< 进程fd管理位,用bitmap管理FD使用情况,默认打开了 0,1,2 (stdin,stdout,stderr)
88 fd_set *cloexec_fds;
89 sem_t ft_sem; /* manage access to the file table | 管理对文件表的访问的信号量*/
90};
91/// 注:系统描述符的使用情况也是用bitmap管理见于 ..\third_party\third_party_NuttX\fs\inode\fs_files.c
92/// 进程文件表结构体 ,进程对文件操作在内存的表现 files_struct 为 进程 process->files 字段,包含一个进程的所有和VFS相关的内容
94 int count; ///< 持有的文件数量
95 struct fd_table_s *fdt; ///< 持有的文件表
96 unsigned int file_lock; ///< 文件互斥锁
97 unsigned int next_fd; ///< 下一个fd
98#ifdef VFS_USING_WORKDIR
99 spinlock_t workdir_lock; ///< 工作区目录自旋锁
100 char workdir[PATH_MAX]; ///< 工作区路径,最大 256个字符
101#endif
102};
103
104
105typedef struct ProcessCB LosProcessCB;
106//
107void files_refer(int fd);
108
109int files_close_internal(int fd, LosProcessCB *processCB);
110///复制FD
111struct files_struct *dup_fd(struct files_struct *oldf);
112///为进程分配文件管理器,其中包含fd总数,(0,1,2)默认给了stdin,stdout,stderr
114///删除参数进程的文件管理器
115void delete_files(struct files_struct *files);
116///创建文件管理器快照,所谓快照就是一份拷贝
118///删除文件管理器快照
120///分配一个系统fd,从全局tg_filelist中拿sysFd
121int alloc_fd(int minfd);
122
124
125void FileTableLock(struct fd_table_s *fdt);
126
127void FileTableUnLock(struct fd_table_s *fdt);
128
129struct fd_table_s *GetFdTable(void);
130#endif
struct files_struct * dup_fd(struct files_struct *oldf)
复制FD
int alloc_fd(int minfd)
分配一个系统fd,从全局tg_filelist中拿sysFd
struct fd_table_s * GetFdTable(void)
获取进程文件描述符表
Definition: vfs_procfd.c:77
int files_close_internal(int fd, LosProcessCB *processCB)
void delete_files_snapshot(struct files_struct *files)
删除文件管理器快照
void FileTableLock(struct fd_table_s *fdt)
对进程文件表操作上锁
Definition: vfs_procfd.c:40
void delete_files(struct files_struct *files)
删除参数进程的文件管理器
void alloc_std_fd(struct fd_table_s *fdt)
struct files_struct * create_files_snapshot(const struct files_struct *oldf)
创建文件管理器快照,所谓快照就是一份拷贝
void files_refer(int fd)
struct files_struct * alloc_files(void)
为进程分配文件管理器,其中包含fd总数,(0,1,2)默认给了stdin,stdout,stderr
void FileTableUnLock(struct fd_table_s *fdt)
对进程文件表操作解锁
Definition: vfs_procfd.c:52
进程fd表结构体
Definition: fd_table.h:84
struct file_table_s * ft_fds
Definition: fd_table.h:86
unsigned int max_fds
进程的文件描述符最多有256个
Definition: fd_table.h:85
fd_set * proc_fds
进程fd管理位,用bitmap管理FD使用情况,默认打开了 0,1,2 (stdin,stdout,stderr)
Definition: fd_table.h:87
fd_set * cloexec_fds
Definition: fd_table.h:88
sem_t ft_sem
Definition: fd_table.h:89
进程fd <--> 系统FD绑定 sysFd的默认值是-1
Definition: fd_table.h:80
intptr_t sysFd
Definition: fd_table.h:81
unsigned int next_fd
下一个fd
Definition: fd_table.h:97
unsigned int file_lock
文件互斥锁
Definition: fd_table.h:96
spinlock_t workdir_lock
工作区目录自旋锁
Definition: fd_table.h:99
struct fd_table_s * fdt
持有的文件表
Definition: fd_table.h:95
char workdir[PATH_MAX]
工作区路径,最大 256个字符
Definition: fd_table.h:100
int count
持有的文件数量
Definition: fd_table.h:94