> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/man4/fd.4.md).

# fd.4

`fd` — 文件描述符文件

## 名称

`fd`, `stdin`, `stdout`, `stderr`

## 描述

**/dev/fd/0** 到 **/dev/fd/#** 这些文件指向可以通过文件系统访问的文件描述符。如果文件描述符已打开，并且打开文件时使用的模式是已有描述符模式的子集，那么调用：

```sh
fd = open("/dev/fd/0", mode);
```

与调用：

```sh
fd = fcntl(0, F_DUPFD, 0);
```

是等价的。

打开文件 **/dev/stdin**、**/dev/stdout** 和 **/dev/stderr** 等价于以下调用：

```sh
fd = fcntl(STDIN_FILENO,  F_DUPFD, 0);
fd = fcntl(STDOUT_FILENO, F_DUPFD, 0);
fd = fcntl(STDERR_FILENO, F_DUPFD, 0);
```

open(2) 调用中除 `O_RDONLY`、`O_WRONLY` 和 `O_RDWR` 之外的标志将被忽略。

## 实现说明

默认情况下，**/dev/fd** 由 [devfs(4)](/man/man4/devfs.4.md) 提供，它为前三个文件描述符提供节点。某些站点可能需要为额外的文件描述符提供节点；可以通过将 [fdescfs(4)](/man/man4/fdescfs.4.md) 挂载到 **/dev/fd** 来使这些节点可用。

## 文件

**/dev/fd/#** **/dev/stdin** **/dev/stdout** **/dev/stderr**

## 参见

[devfs(4)](/man/man4/devfs.4.md), [fdescfs(4)](/man/man4/fdescfs.4.md), [tty(4)](/man/man4/tty.4.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/man4/fd.4.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
