【问题标题】:select() with const timeout parameter?select() 与 const timeout 参数?
【发布时间】:2015-04-10 00:26:10
【问题描述】:

select(2) 系统调用在 Stevens、Fenner 和 Rudoff 的 Unix Network Programming (2003) 第 6.3 章中定义如下:

#include <sys/select.h>
#include <sys/time.h>
int select(int maxfdp1, fd_set *readset, fd_set *writeset,
        fd_set *exceptset, const struct timeval *timeout);

但是现代 Unix 系统(例如 FreeBSD、OpenBSD、NetBSD、Linux,甚至 POSIX[1] 标准)都没有这样定义系统调用。但是,书中指出“POSIX 指定了 const 限定符”。难道是书上的错误?还是因为历史原因?不过,所有系统都将 pselect(2) 定义为具有恒定的超时参数。

http://pubs.opengroup.org/onlinepubs/009695399/functions/pselect.html

图书勘误页并未将此列为错误:

http://www.unixnetworkprogramming.com/errata.html

【问题讨论】:

  • 这是书中的错误:Upon successful completion, the select() function may modify the object pointed to by the timeout argument. SUSv2 说了类似的话,是从 1997 年开始的。
  • 恕我直言,这是否是一个错误值得商榷。 POSIX 标准是模棱两可的,因为它说超时值“可以”被修改。因此,任何实现都可以选择不修改它并将其定义为 const。甚至 Linux select 手册页也暗示了这种含糊之处:“在 Linux 上,select() 修改超时以反映未睡眠的时间量;大多数其他实现都没有这样做。(POSIX.1-2001 允许任何一种行为。).. . 考虑在 select() 返回后未定义超时。"
  • 感谢您的帮助。我已将此错误报告给作者。

标签: c unix standards portability multiplexing


【解决方案1】:

POSIX 定义了select() 的接口 如:

int select(int nfds, fd_set *restrict readfds,
       fd_set *restrict writefds, fd_set *restrict errorfds,
       struct timeval *restrict timeout);

select() 函数应等效于 pselect() 函数,但以下情况除外:

  • ...
  • 成功完成后,select() 函数可能会修改 timeout 参数指向的对象。

pselect 函数采用 const struct timespec * restrict timeout 参数(POSIX 定义中的同一页)。

书中引用的const限定词是错误的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-06
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-14
    相关资源
    最近更新 更多