与维基百科相比,什么样的文件描述符描述更简单?为什么需要它们?比如说,以壳进程为例,它是如何应用的呢? 进程表是否包含多个文件描述符?如果是,为什么?


当前回答

A file descriptor is an opaque handle that is used in the interface between user and kernel space to identify file/socket resources. Therefore, when you use open() or socket() (system calls to interface to the kernel), you are given a file descriptor, which is an integer (it is actually an index into the processes u structure - but that is not important). Therefore, if you want to interface directly with the kernel, using system calls to read(), write(), close() etc. the handle you use is a file descriptor.

系统调用上覆盖了一个抽象层,即stdio接口。这比基本的系统调用提供了更多的功能/特性。对于这个接口,您获得的不透明句柄是一个FILE*,它由fopen()调用返回。有很多很多函数使用stdio接口fprintf(), fscanf(), fclose(),它们让你的生活更简单。在C语言中,stdin、stdout和stderr分别是FILE*,在UNIX中分别映射到文件描述符0、1和2。

其他回答

来自马的嘴:APUE(理查德·史蒂文斯饰)。 对于内核,所有打开的文件都由文件描述符引用。文件描述符是非负数。

当我们打开一个现有文件或创建一个新文件时,内核会向进程返回一个文件描述符。内核维护一个包含所有正在使用的打开的文件描述符的表。文件描述符的分配通常是顺序的,它们从空闲文件描述符池中作为下一个空闲文件描述符分配给文件。关闭文件时,文件描述符将被释放,并可用于进一步分配。 请看这张图片了解更多细节:

When we want to read or write a file, we identify the file with the file descriptor that was returned by open() or create() function call, and use it as an argument to either read() or write(). It is by convention that, UNIX System shells associates the file descriptor 0 with Standard Input of a process, file descriptor 1 with Standard Output, and file descriptor 2 with Standard Error. File descriptor ranges from 0 to OPEN_MAX. File descriptor max value can be obtained with ulimit -n. For more information, go through 3rd chapter of APUE Book.

文件描述符(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"的文件 因此,程序输出不会充满错误。 我希望你得到了答案。

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 ]

A file descriptor is an opaque handle that is used in the interface between user and kernel space to identify file/socket resources. Therefore, when you use open() or socket() (system calls to interface to the kernel), you are given a file descriptor, which is an integer (it is actually an index into the processes u structure - but that is not important). Therefore, if you want to interface directly with the kernel, using system calls to read(), write(), close() etc. the handle you use is a file descriptor.

系统调用上覆盖了一个抽象层,即stdio接口。这比基本的系统调用提供了更多的功能/特性。对于这个接口,您获得的不透明句柄是一个FILE*,它由fopen()调用返回。有很多很多函数使用stdio接口fprintf(), fscanf(), fclose(),它们让你的生活更简单。在C语言中,stdin、stdout和stderr分别是FILE*,在UNIX中分别映射到文件描述符0、1和2。

我不知道内核代码,但我将在这里补充我的意见,因为我已经思考了一段时间,我认为这将是有用的。

当您打开一个文件时,内核返回一个文件描述符来与该文件进行交互。

文件描述符是您正在打开的文件的API的实现。内核创建这个文件描述符,将其存储在一个数组中,并将其提供给您。

例如,该API需要一个允许您读取和写入文件的实现。

现在,再想想我说过的话,记住所有东西都是文件——打印机、监视器、HTTP连接等等。

这是我阅读https://www.bottomupcs.com/file_descriptors.xhtml后的总结。