pollのヘルプ・マニュアル
日本語 英語
poll --help
man poll
POLL(2) Linux Programmer’s Manual POLL(2)
名前
poll, ppoll - ファイルディスクリプタにおけるイベントを待つ
書式
#include
int poll(struct pollfd *fds, nfds_t nfds, int timeout);
#define _GNU_SOURCE
#include
int ppoll(struct pollfd *fds, nfds_t nfds,
const struct timespec *timeout, const sigset_t *sigmask);
説明
poll() は select(2) と同様の仕事を行う、つまり、ファイルディスクリプタ
集合のいずれか一つが I/O を実行可能な状態になるのを待つ。
監視するファイルディスクリプタ集合は、 fds 引き数で指定する。 fds は 、
以下の型の構造体の nfds 個の配列である。
struct pollfd {
int fd; /* file descriptor */
short events; /* requested events */
short revents; /* returned events */
};
構造体の fd にはオープンしたファイルのファイルディスクリプタを入れる。
構 造体の events 要素は入力パラメータで、アプリケーションが興味を持って
いるイベントのビットマスクを指定する。
revents 要素は出力パラメータで、実際に起こったイベントがカーネルによ り
設定される。 revents で返されるビット列には、 events で指定したもののど
れか、もしくは POLLERR, POLLHUP, POLLNVAL のうちの 一 つ が 含 ま れ る
(POLLERR, POLLHUP, POLLNVAL の 3つのビットは events に指定しても意味が
なく、対応した状態が真の場合に revents に設定される)。
どのファイルディスクリプタにも要求したイベントが発生しておらず、エラ ー
も 起こらない場合、 poll() はイベントのうちいずれか一つが発生するまで停
止 (block) する。
timeout 引き数は poll() が停止する時間の上限を設定するもので、ミリ秒 単
位で指定する。 timeout に負の値を指定すると、タイムアウト時間が無限とな
る。
events に指定したり、 revents で返されるビットは で定義され て
いる:
POLLIN 読み出し可能なデータがある。
POLLPRI
読 み 出 し 可能な緊急データ (urgent data) がある (例えば
、TCP ソケットの帯域外 (out-of-band data) データを受信 し
た 場合や、パケットモードの擬似端末のマスタがスレーブ側の
変化を見つけたとき)。
POLLOUT
書き込みが停止 (block) しない状態である。
POLLRDHUP (Linux 2.6.17 以降)
ストリームソケットの他端が、コネクションを close したか、
コ ネクションの書き込み側を shutdown した。この定義を有効
にするには、 _GNU_SOURCE 機能検査マクロを定義しなければな
らない。
POLLERR
エラー状態 (出力の場合のみ)。
POLLHUP
ハングアップした (出力の場合のみ)。
POLLNVAL
不正な要求: fd がオープンされていない (出力の場合のみ)。
_XOPEN_SOURCE を定義してコンパイルした場合には、以下の定義も行われる。
ただし、上記のリストにあるビット以上の情報が得られる訳ではない。
POLLRDNORM
POLLIN と同じ。
POLLRDBAND
優先帯域データ (priority band data) が読み出し可能であ る
(普通は Linux では使用されない)。
POLLWRNORM
POLLOUT と同じ。
POLLWRBAND
優先帯域データ (priority data) が書き込み可能である。
Linux では POLLMSG も定義されているが、使用されていない。
ppoll()
poll() と ppoll() の関係は select(2) と pselect(2) の関係と同じようなも
のである: pselect(2) と同様に、 ppoll() を使うと、アプリケーションは フ
ァ イルディスクリプタの状態変化もしくはシグナルの捕捉を安全に待つことが
できる。
timeout 引き数の違いを除くと、以下の ppoll() の呼び出しは、
ready = ppoll(&fds, nfds, timeout, &sigmask);
次のコールを atomic に実行するのと等価である。
sigset_t origmask;
sigprocmask(SIG_SETMASK, &sigmask, &origmask);
ready = poll(&fds, nfds, timeout);
sigprocmask(SIG_SETMASK, &origmask, NULL);
なぜ ppoll() が必要なのかについての説明は pselect(2) の説明を参照のこと
。
sigmask 引き数に NULL が指定された場合、シグナルマスクの操作は行われな
い (したがって、 ppoll() の poll() との違いは timeout 引き数の精度だ け
となる)。
timeout 引き数は ppoll() が停止する時間の上限を指定するものである。この
引き数には以下の型の構造体へのポインタを指定する。
struct timespec {
long tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
timeout に NULL が指定された場合、 ppoll は無限に停止することがあり得る
。
返り値
成 功した場合は正の数を返す。この数は 0 以外の revents 要素を持つ構造体
の数である (別の言い方をすると、これらのディスクリプタにはイベントか エ
ラ ー報告がある)。値 0 は、タイムアウトとなり、どのファイルディスクリプ
タでもイベントが発生しなかったことを示す。エラーの場合は -1 が返され 、
errno が適切に設定される。
エラー
EBADF 集合のどれかに不正なファイルディスクリプタが含まれている。
EFAULT 引き数として指定した配列が、呼び出したプロセスのアドレス空間に含
まれていない。
EINTR 要求されたイベントのどれかが起こる前にシグナルが発生した。 sig-
nal(7) 参照。
EINVAL nfds の値が RLIMIT_NOFILE を超えた。
ENOMEM ファイルディスクリプタ・テーブルを確保するためのメモリがない。
バージョン
poll() システムコールは Linux 2.1.23 で導入された。 poll() ライブラリ・
コールは libc 5.4.28 から導入された (これはカーネルが poll() システムコ
ー ルをサポートしていない場合に select(2) を使用してエミュレートを行う)
。
ppoll() システムコールは カーネル 2.6.16 で Linux に 追 加 さ れ た 。
ppoll() ライブラリコールは glibc 2.4 に追加された。
準拠
poll() は POSIX.1-2001 に準拠している。 ppoll() は Linux 固有である。
注意
い くつかの実装では、値 -1 を持った非標準の定数 INFTIM が定義されており
、 timeout の指定に使用できる。この定数は glibc では定義されていない。
Linux での注意
Linux の ppoll() システムコールは timeout 引き数を変更す る 。 し か し
、glibc のラッパー関数は、システムコールに渡す timeout 引き数としてロー
カル変数を使うことでこの動作を隠蔽している。このため、glibc の ppoll()
関数は timeout 引き数を変更しない。
バグ
select(2) の 「バグ」の節に書かれている、誤った準備完了通知 (spurious
readiness notifications) についての議論を参照のこと。
関連項目
select(2), select_tut(2), feature_test_macros(7), time(7)
Linux 2008-04-23 POLL(2)
POLL(2) Linux Programmer’s Manual POLL(2)
NAME
poll, ppoll - wait for some event on a file descriptor
SYNOPSIS
#include
int poll(struct pollfd *fds, nfds_t nfds, int timeout);
#define _GNU_SOURCE
#include
int ppoll(struct pollfd *fds, nfds_t nfds,
const struct timespec *timeout, const sigset_t *sigmask);
DESCRIPTION
poll() performs a similar task to select(2): it waits for one of a set
of file descriptors to become ready to perform I/O.
The set of file descriptors to be monitored is specified in the fds
argument, which is an array of nfds structures of the following form:
struct pollfd {
int fd; /* file descriptor */
short events; /* requested events */
short revents; /* returned events */
};
The field fd contains a file descriptor for an open file.
The field events is an input parameter, a bit mask specifying the
events the application is interested in.
The field revents is an output parameter, filled by the kernel with the
events that actually occurred. The bits returned in revents can
include any of those specified in events, or one of the values POLLERR,
POLLHUP, or POLLNVAL. (These three bits are meaningless in the events
field, and will be set in the revents field whenever the corresponding
condition is true.)
If none of the events requested (and no error) has occurred for any of
the file descriptors, then poll() blocks until one of the events
occurs.
The timeout argument specifies an upper limit on the time for which
poll() will block, in milliseconds. Specifying a negative value in
timeout means an infinite timeout.
The bits that may be set/returned in events and revents are defined in
:
POLLIN There is data to read.
POLLPRI
There is urgent data to read (e.g., out-of-band data on
TCP socket; pseudo-terminal master in packet mode has
seen state change in slave).
POLLOUT
Writing now will not block.
POLLRDHUP (since Linux 2.6.17)
Stream socket peer closed connection, or shut down writ-
ing half of connection. The _GNU_SOURCE feature test
macro must be defined in order to obtain this definition.
POLLERR
Error condition (output only).
POLLHUP
Hang up (output only).
POLLNVAL
Invalid request: fd not open (output only).
When compiling with _XOPEN_SOURCE defined, one also has the following,
which convey no further information beyond the bits listed above:
POLLRDNORM
Equivalent to POLLIN.
POLLRDBAND
Priority band data can be read (generally unused on
Linux).
POLLWRNORM
Equivalent to POLLOUT.
POLLWRBAND
Priority data may be written.
Linux also knows about, but does not use POLLMSG.
ppoll()
The relationship between poll() and ppoll() is analogous to the rela-
tionship between select(2) and pselect(2): like pselect(2), ppoll()
allows an application to safely wait until either a file descriptor
becomes ready or until a signal is caught.
Other than the difference in the timeout argument, the following
ppoll() call:
ready = ppoll(&fds, nfds, timeout, &sigmask);
is equivalent to atomically executing the following calls:
sigset_t origmask;
sigprocmask(SIG_SETMASK, &sigmask, &origmask);
ready = poll(&fds, nfds, timeout);
sigprocmask(SIG_SETMASK, &origmask, NULL);
See the description of pselect(2) for an explanation of why ppoll() is
necessary.
If the sigmask argument is specified as NULL, then no signal mask
manipulation is performed (and thus ppoll() differs from poll() only in
the precision of the timeout argument).
The timeout argument specifies an upper limit on the amount of time
that ppoll() will block. This argument is a pointer to a structure of
the following form:
struct timespec {
long tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
If timeout is specified as NULL, then ppoll() can block indefinitely.
RETURN VALUE
On success, a positive number is returned; this is the number of struc-
tures which have non-zero revents fields (in other words, those
descriptors with events or errors reported). A value of 0 indicates
that the call timed out and no file descriptors were ready. On error,
-1 is returned, and errno is set appropriately.
ERRORS
EFAULT The array given as argument was not contained in the calling
program’s address space.
EINTR A signal occurred before any requested event; see signal(7).
EINVAL The nfds value exceeds the RLIMIT_NOFILE value.
ENOMEM There was no space to allocate file descriptor tables.
VERSIONS
The poll() system call was introduced in Linux 2.1.23. The poll()
library call was introduced in libc 5.4.28 (and provides emulation
using select(2) if your kernel does not have a poll() system call).
The ppoll() system call was added to Linux in kernel 2.6.16. The
ppoll() library call was added in glibc 2.4.
CONFORMING TO
poll() conforms to POSIX.1-2001. ppoll() is Linux-specific.
NOTES
Some implementations define the non-standard constant INFTIM with the
value -1 for use as a timeout. This constant is not provided in glibc.
Linux Notes
The Linux ppoll() system call modifies its timeout argument. However,
the glibc wrapper function hides this behavior by using a local vari-
able for the timeout argument that is passed to the system call. Thus,
the glibc ppoll() function does not modify its timeout argument.
BUGS
See the discussion of spurious readiness notifications under the BUGS
section of select(2).
SEE ALSO
select(2), select_tut(2), feature_test_macros(7), time(7)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2009-06-02 POLL(2)