Skip to the content.

File I/O: Further Details

5.4 Relationship Between File Descriptors and Open Files

To understand what is going on, we need to examine three data structures maintained by the kernel:

For each process, the kernel maintains a table of open file descriptors. Each entry in this table records information about a single file descriptor, including:

The kernel maintains a system-wide table of all open file descriptions. (This table is sometimes referred to as the open file table, and its entries are sometimes called open file handles.) An open file description stores all information relating to an open file, including:

  1. the current file offset (as updated by read() and write(), or explicitly modified using lseek());
  2. status flags specified when opening the file (i.e., the flags argument to open());
  3. the file access mode (read-only, write-only, or read-write, as specified in open());
  4. settings relating to signal-driven I/O (Section 63.3); and
  5. a reference to the i-node object for this file.

We can draw a number of implications from the preceding discussion:

5.5 Duplicating File Descriptors

介绍了几个 duplicate fd 的系统调用,以及使用的原因 : 将 stderr 和 stdout 指向同一个文件,将 stderr 关闭,然后 dup stdout,正好得到 stderr,如此,当 pipe 写入到 stderr 的实际上被指向到 stdout 中间。(也许如此吧!)

本站所有文章转发 CSDN 将按侵权追究法律责任,其它情况随意。