与维基百科相比,什么样的文件描述符描述更简单?为什么需要它们?比如说,以壳进程为例,它是如何应用的呢? 进程表是否包含多个文件描述符?如果是,为什么?
当前回答
任何操作系统都有正在运行的进程(p),比如p1、p2、p3等等。每个进程通常会持续使用文件。
每个过程都由一个过程树(或者另一种说法是进程表)组成。
通常,操作系统用数字表示每个进程中的每个文件(也就是说,在每个进程树/表中)。
进程中使用的第一个文件是file0,第二个是file1,第三个是file2,依此类推。
任何这样的数字都是文件描述符。
文件描述符通常是整数(0、1、2而不是0.5、1.5、2.5)。
假设我们经常将过程描述为“进程表”,并且假设表格有行(条目),我们可以说每个条目中的文件描述符单元格用于表示整个条目。
以类似的方式,当您打开一个网络套接字时,它有一个套接字描述符。
在某些操作系统中,您可能会耗尽文件描述符,但这种情况极为罕见,普通计算机用户不必为此担心。
文件描述符可能是全局的(进程A开始于0,结束于1;进程B开始于2,结束于3)等等,但据我所知,通常在现代操作系统中,文件描述符不是全局的,实际上是特定于进程的(进程A开始于0,结束于5,而进程B开始于0,结束于10)。
其他回答
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.
类似地,当您打开一个网络套接字时,它也由一个整数表示,称为套接字描述符。 我希望你能理解。
文件描述符(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 ]
文件描述符
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
这里提供的所有答案都很棒,这是我的版本
文件描述符是非负整数,充当“文件”或I/O资源(如管道、套接字或数据流)的抽象句柄。这些描述符帮助我们与这些I/O资源进行交互,并使使用它们变得非常容易。I/O系统对于用户进程来说是一个字节流(I/O流)。Unix进程使用描述符(小的无符号整数)来引用I/O流。与I/O操作相关的系统调用以一个描述符作为参数。
有效的文件描述符范围从0到可配置的最大描述符数(ulimit, /proc/sys/fs/file-max)。内核为FD表的std输入(0),std输出(1)和std错误(2)分配desc.。如果文件打开失败,fd返回-1。
当进程成功请求打开一个文件时,内核返回一个文件描述符,该描述符指向内核全局文件表中的一个条目。文件表项包含文件的inode、字节偏移量和该数据流的访问限制(只读、只写等)等信息。