*nix select 和 exceptfds/errorfds 语义

发布于 2024-08-03 07:47:12 字数 462 浏览 5 评论 0原文

select 系统调用需要 3 个文件描述符集来监视 fd 的可读/可写和文件描述符上的“例外”。

我选择的 手册页 没有详细说明 exceptfd 描述符集。它的用途是什么;它可以并且将会在文件描述符上通知什么类型的异常?

我假设这对于描述符类型可能有所不同......无论是 TCP 套接字、管道、 tty 等)。有谁有更多关于 select 可以报告不同类型描述符的错误类型的信息吗?

The select syscall takes 3 filedescriptor sets for watching fds for readable/writeable and "exceptions" on filedescriptor.

My select man page doesn't state much about the exceptfd descriptor set. What is it used for; what kind of exceptions can and will it notify on file descriptors?

I'm assuming this can be different for the descriptor type... whether it's a TCP socket, a pipe, a tty , etc.). Does anyone have more info on what kind of errors select can report on the different kinds of descriptors?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回首观望 2024-08-10 07:47:12

有时人们认为需要 exceptfds 来检测错误,但这是一种误解。错误将在 readfds 中标记。尽管 POSIX 需要它(甚至调用参数 errorfds),但错误是否也在 exceptfds 中标记取决于操作系统。实际上,仅当您关心异常情况时才需要此参数,但很少需要检测这些情况。

什么符合异常条件取决于文件描述符的类型,但到目前为止最常见的用途是在 TCP 套接字上,它指示可以使用 recv() 带有 MSG_OOB 标志。然而,TCP 带外数据有许多怪癖(例如,只有 1 个字节可以突出),因此很少使用。

在最近的 Linux 内核中,可以使用 exceptfds 来检测某些 sysfs 属性何时发生变化。可以通过读取 /sys 下的相应文件来读取属性的当前值,并且文件描述符上的 select() 将标记 exceptfds 当属性改变时。然而,目前这仅适用于某些属性以及挂载更改(/proc/mounts)。

此外,某些设备驱动程序将使用 exceptfds 标记某些特定于设备的条件。

It is sometimes thought that exceptfds is needed to detect errors, but that is a misconception. Errors will be flagged in readfds. Although POSIX requires it (and even calls the parameter errorfds), it depends on the OS whether errors are also flagged in exceptfds. Really, this parameter is only needed if you care about exceptional conditions, but only rarely is there any need to detect those.

What qualifies as an exceptional condition depends on the kind of file descriptor, but by far the most common use is on a TCP socket where it indicates that out-of-band data is available to be read using recv() with the MSG_OOB flag. However, TCP out-of-band data has a number of quirks (e.g. only 1 byte can be outstanding) and as a result is rarely used.

In recent Linux kernels exceptfds can be used to detect when certain sysfs attributes change. The current value of the attribute can be read by reading the appropriate file under /sys, and a select() on the file descriptor will flag exceptfds when the attribute changes. However this currently only works for some of the attributes, and for mount changes (/proc/mounts).

Also some device drivers will flag certain device-specific conditions using exceptfds.

万人眼中万个我 2024-08-10 07:47:12

你是对的,这取决于你使用文件描述符引用的设备类型。因此,对于套接字、FIFO、串行端口等来说,情况是不同的......

请查看 read() 的手册页。在底部(至少在 OS X 中)它列出了不同设备可能出现的不同错误。 write() 也是如此。

对于套接字、FIFO 和其他 IPC 机制,我会查看《Unix 网络编程》第 1 卷和第 2 卷。IIRC 它描述了不同错误条件下会出现什么样的 errnos。

我曾经使用 FIFO 走过这条路。我最终集思广益了生产者和消费者与 FIFO 每一端交互的所有方式,然后为每种情况编写了测试用例。这是发现所有不同错误情况的好方法(尽管很乏味)。不过我学到了很多东西,最终代码现在就可以运行了。

You're right, it depends on the type of device you're referencing with the file descriptors. So, it's different for sockets, FIFOs, serial ports, etc...

Look at the man page for read(). At the bottom (at least in OS X) it lists the different errors you can get for different devices. Same goes for write().

For sockets, FIFOs and other IPC mechanisms, I would check out Unix Network Programming, Volumes 1 and 2. IIRC it describes what kind of errnos to expect for different error conditions.

I walked this path once with FIFOs. I ended up brainstorming all the ways the producer and consumer could interact with each end of the FIFO and then wrote test cases for each of those situations. It was a good (although tedious) way to discover all the different error conditions. I learned a lot though and ultimately the code just works now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文