与维基百科相比,什么样的文件描述符描述更简单?为什么需要它们?比如说,以壳进程为例,它是如何应用的呢? 进程表是否包含多个文件描述符?如果是,为什么?
当前回答
In simple words, when you open a file, the operating system creates an entry to represent that file and store the information about that opened file. So if there are 100 files opened in your OS then there will be 100 entries in OS (somewhere in kernel). These entries are represented by integers like (...100, 101, 102....). This entry number is the file descriptor. So it is just an integer number that uniquely represents an opened file for the process. If your process opens 10 files then your Process table will have 10 entries for file descriptors.
类似地,当您打开一个网络套接字时,它也由一个整数表示,称为套接字描述符。 我希望你能理解。
其他回答
文件描述符
To Kernel all open files are referred to by file descriptors. A file descriptor is a non - negative integer. When we open an existing or create a new file, the kernel returns a file descriptor to a process. When we want to read or write on a file, we identify the file with file descriptor that was retuned by open or create, as an argument to either read or write. Each UNIX process has 20 file descriptors and it disposal, numbered 0 through 19 but it was extended to 63 by many systems. The first three are already opened when the process begins 0: The standard input 1: The standard output 2: The standard error output When the parent process forks a process, the child process inherits the file descriptors of the parent
As an addition to other answers, unix considers everything as a file system. Your keyboard is a file that is read only from the perspective of the kernel. The screen is a write only file. Similarly, folders, input-output devices etc are also considered to be files. Whenever a file is opened, say when the device drivers[for device files] requests an open(), or a process opens an user file the kernel allocates a file descriptor, an integer that specifies the access to that file such it being read only, write only etc. [for reference : https://en.wikipedia.org/wiki/Everything_is_a_file ]
除了以上所有简化的回答。
如果使用bash脚本处理文件,最好使用文件描述符。
例如: 如果你想读写文件"test.txt",使用文件描述符如下所示:
FILE=$1 # give the name of file in the command line
exec 5<>$FILE # '5' here act as the file descriptor
# Reading from the file line by line using file descriptor
while read LINE; do
echo "$LINE"
done <&5
# Writing to the file using descriptor
echo "Adding the date: `date`" >&5
exec 5<&- # Closing a file descriptor
文件描述符(FD):
In Linux/Unix, everything is a file. Regular file, Directories, and even Devices are files. Every File has an associated number called File Descriptor (FD). Your screen also has a File Descriptor. When a program is executed the output is sent to File Descriptor of the screen, and you see program output on your monitor. If the output is sent to File Descriptor of the printer, the program output would have been printed. Error Redirection : Whenever you execute a program/command at the terminal, 3 files are always open standard input standard output standard error. These files are always present whenever a program is run. As explained before a file descriptor, is associated with each of these files. File File Descriptor Standard Input STDIN 0 Standard Output STDOUT 1 Standard Error STDERR 2 For instance, while searching for files, one typically gets permission denied errors or some other kind of errors. These errors can be saved to a particular file. Example 1
$ ls mydir 2>错误文件.txt
标准错误的文件描述符是2。 如果没有任何名为mydir的目录,则命令的输出将保存到文件errorfile.txt 使用"2>"将错误输出重定向到名为"errorfile.txt"的文件 因此,程序输出不会充满错误。 我希望你得到了答案。
我不知道内核代码,但我将在这里补充我的意见,因为我已经思考了一段时间,我认为这将是有用的。
当您打开一个文件时,内核返回一个文件描述符来与该文件进行交互。
文件描述符是您正在打开的文件的API的实现。内核创建这个文件描述符,将其存储在一个数组中,并将其提供给您。
例如,该API需要一个允许您读取和写入文件的实现。
现在,再想想我说过的话,记住所有东西都是文件——打印机、监视器、HTTP连接等等。
这是我阅读https://www.bottomupcs.com/file_descriptors.xhtml后的总结。
推荐文章
- 如何在父进程退出后使子进程死亡?
- 为什么Linux被称为单片内核?
- Unix列表命令'ls'可以输出数值chmod权限吗?
- 在Unix中,我可以在一个目录中运行'make'而不首先cd'到该目录吗?
- 快速unix命令显示文件中间的特定行?
- 如何生成一个核心转储在Linux上的分段错误?
- 在Python中如何在Linux和Windows中使用“/”(目录分隔符)?
- 使用sh shell比较字符串
- 只列出UNIX中的目录
- 如何限制从grep返回的结果的数量?
- 如何管道列表的文件返回的找到命令到猫查看所有文件
- 以相对于当前目录的路径递归地在Linux CLI中列出文件
- 如何使用xargs复制名称中有空格和引号的文件?
- 在makefile中抑制命令调用的回声?
- Shell脚本for循环语法