.\" Hey Emacs! This file is -*- nroff -*- source. .\" .\" This manpage is copyright (C) 1992 Drew Eckhardt, .\" copyright (C) 1995 Michael Shields. .\" .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" .\" Modified 1993-07-24 by Rik Faith .\" Modified 1995-05-18 by Jim Van Zandt .\" Sun Feb 11 14:07:00 MET 1996 Martin Schulze .\" * layout slightly modified .\" .\" Modified Mon Oct 21 23:05:29 EDT 1996 by Eric S. Raymond .\" Modified Thu Feb 24 01:41:09 CET 2000 by aeb .\" Modified Thu Feb 9 22:32:09 CET 2001 by bert hubert , aeb .\" Modified Mon Nov 11 14:35:00 PST 2002 by Ben Woodard .\" 2005-03-11, mtk, modified pselect() text (it is now a system .\" call in 2.6.16. .\" .\" Japanese Version Copyright (c) 1997 HANATAKA Shinya all rights reserved. .\" Translated 1997-02-23, HANATAKA Shinya .\" Modified 2000-03-12, HANATAKA Shinya .\" Updated & Modified 2001-06-04, Yuichi SATO .\" Updated & Modified 2001-07-01, Yuichi SATO .\" Updated & Modifedd 2002-01-14, Yuichi SATO .\" Updated 2002-09-24, Akihiro MOTOKI .\" Updated 2003-01-19, Akihiro MOTOKI .\" Updated 2005-03-17, Akihiro MOTOKI .\" Updated 2006-04-16, Akihiro MOTOKI, LDP v2.28 .\" Updated 2006-07-23, Akihiro MOTOKI, LDP v2.36 .\" Updated 2007-09-08, Akihiro MOTOKI, LDP v2.64 .\" Updated 2008-08-06, Akihiro MOTOKI, LDP v3.05 .\" .\"WORD: synchronous 同期させる .\"WORD: multiplexing 多重化 .\"WORD: file descriptor ファイルディスクリプタ .\"WORD: descriptor 記述子 .\"WORD: macro マクロ .\"WORD: block 停止(block) .\"WORD: block 禁止(block) .\"WORD: signal シグナル .\"WORD: timeout 時間切れ(timeout) .\"WORD: portable 移植性のある .\" .TH SELECT 2 2010-08-31 "Linux" "Linux Programmer's Manual" .\"O .SH NAME .SH 名前 .\"O select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO \- .\"O synchronous I/O multiplexing select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO \- 多重化された I/O の同期をとる .\"O .SH SYNOPSIS .SH 書式 .nf .\"O /* According to POSIX.1-2001 */ /* POSIX.1-2001 に従う場合 */ .br .B #include .sp .\"O /* According to earlier standards */ /* 以前の規格に従う場合 */ .br .B #include .br .B #include .br .B #include .sp .BI "int select(int " nfds ", fd_set *" readfds ", fd_set *" writefds , .BI " fd_set *" exceptfds ", struct timeval *" timeout ); .sp .BI "void FD_CLR(int " fd ", fd_set *" set ); .br .BI "int FD_ISSET(int " fd ", fd_set *" set ); .br .BI "void FD_SET(int " fd ", fd_set *" set ); .br .BI "void FD_ZERO(fd_set *" set ); .sp .B #include .sp .BI "int pselect(int " nfds ", fd_set *" readfds ", fd_set *" writefds , .BI " fd_set *" exceptfds ", const struct timespec *" timeout , .BI " const sigset_t *" sigmask ); .fi .sp .in -4n .\"O Feature Test Macro Requirements for glibc (see .\"O .BR feature_test_macros (7)): glibc 向けの機能検査マクロの要件 .RB ( feature_test_macros (7) 参照): .in .sp .BR pselect (): _POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600 .\"O .SH DESCRIPTION .SH 説明 .\"O .BR select () .\"O and .\"O .BR pselect () .\"O allow a program to monitor multiple file descriptors, .\"O waiting until one or more of the file descriptors become "ready" .\"O for some class of I/O operation (e.g., input possible). .\"O A file descriptor is considered ready if it is possible to .\"O perform the corresponding I/O operation (e.g., .\"O .BR read (2)) .\"O without blocking. .BR select () や .BR pselect () を使うと、プログラムで複数のファイルディスクリプタを監視し、 一つ以上のファイルディスクリプタがある種の I/O 操作の 「ready (準備ができた)」状態 (例えば、読み込み可能になった状態) になるまで待つことができる。 ファイルディスクリプタが ready (準備ができた) とは、 対応する I/O 操作 (例えば .BR read (2) など) が停止 (block) なしに実行可能な状態にあることを意味する。 .PP .\"O The operation of .\"O .BR select () .\"O and .\"O .BR pselect () .\"O is identical, with three differences: .BR select () と .BR pselect () の動作は同じであるが、以下の 3 点が異なる: .TP (i) .\"O .BR select () .\"O uses a timeout that is a .\"O .I struct timeval .\"O (with seconds and microseconds), while .\"O .BR pselect () .\"O uses a .\"O .I struct timespec .\"O (with seconds and nanoseconds). .BR select () では、タイムアウト時間の指定に構造体 .I struct timeval (秒・マイクロ秒単位) を用いる。 一方、 .BR pselect () 関数では、構造体 .I struct timespec (秒・ナノ秒単位) を用いる。 .TP (ii) .\"O .BR select () .\"O may update the .\"O .I timeout .\"O argument to indicate how much time was left. .\"O .BR pselect () .\"O does not change this argument. .BR select () は残り時間を示す .I timeout 引き数を更新することがある。 .BR pselect () はこの引き数を変更しない。 .TP (iii) .\"O The .\"O .BR select () .\"O has no .\"O .I sigmask .\"O argument, and behaves as .\"O .BR pselect () .\"O called with NULL .\"O .IR sigmask . .BR select () は .I sigmask 引き数を持たない。その動作は .I sigmask に NULL を指定した場合の .BR pselect () と同じである。 .PP .\"O Three independent sets of file descriptors are watched. .\"O Those listed in .\"O .I readfds .\"O will be watched to see if characters become .\"O available for reading (more precisely, to see if a read will not .\"O block; in particular, a file descriptor is also ready on end-of-file), .\"O those in .\"O .I writefds .\"O will be watched to see if a write will not block, and .\"O those in .\"O .I exceptfds .\"O will be watched for exceptions. .\"O On exit, the sets are modified in place .\"O to indicate which file descriptors actually changed status. .\"O Each of the three file descriptor sets may be specified as NULL .\"O if no file descriptors are to be watched for the corresponding class .\"O of events. 3 つの独立したファイルディスクリプタ集合の監視を行う。 .I readfds に入れられたディスクリプタについては、読み込みが可能かどうかを 監視する (より正確にいうと、停止 (block) なしで読むことができるかを 調べる。ファイルの終端 (end-of-file) の場合も、 ファイルディスクリプタは読み込み可能として扱われる)。 .I writefds に入れられたディスクリプタについては、停止せずに書き込みが 可能かどうかを監視する。 .I exceptfds にあるものについては、例外の監視を行なう。システムコール終了時に、 どのファイルディスクリプタの状態が実際に変化したか示すために、 集合の内容が変更される。 ある種別のイベントを監視したいファイルディスクリプタが一つもない場合には、 対応するファイルディスクリプタ集合に NULL を指定することができる。 .PP .\"O Four macros are provided to manipulate the sets. 集合を操作するために 4 つのマクロが提供されている。 .\"O .BR FD_ZERO () .\"O clears a set. .BR FD_ZERO () は集合を消去する。 .\"O .BR FD_SET () .\"O and .\"O .BR FD_CLR () .\"O respectively add and remove a given file descriptor from a set. .BR FD_SET () と .BR FD_CLR () はそれぞれ指定したファイルディスクリプタの集合への追加、削除を行う。 .\"O .BR FD_ISSET () .\"O tests to see if a file descriptor is part of the set; .\"O this is useful after .\"O .BR select () .\"O returns. .BR FD_ISSET () は集合にファイルディスクリプタがあるかどうか調べる; このマクロは .BR select () が終了した後に使うと便利である。 .PP .\"O .I nfds .\"O is the highest-numbered file descriptor in any of the three sets, plus 1. .I nfds は 3 つの集合に含まれるファイルディスクリプタの最大値に 1 を足したものである。 .PP .\"O .I timeout .\"O is an upper bound on the amount of time elapsed before .\"O .BR select () .\"O returns. .\"O If both fields of the .\"O .I timeval .\"O structure are zero, then .\"O .BR select () .\"O returns immediately. .\"O (This is useful for polling.) .\"O If .\"O .I timeout .\"O is NULL (no timeout), .\"O .BR select () .\"O can block indefinitely. .I timeout は .BR select () が復帰するまでの経過時間の上限である。 .I timeval 構造体の両方のフィールドが 0 の場合、 .BR select () はすぐに復帰する (この機能はポーリング (polling) を行うのに便利である)。 .I timeout に NULL (タイムアウトなし) が指定されると、 .BR select () は無期限に停止 (block) する。 .PP .\"O .I sigmask .\"O is a pointer to a signal mask (see .\"O .BR sigprocmask (2)); .\"O if it is not NULL, then .\"O .BR pselect () .\"O first replaces the current signal mask by the one pointed to by .\"O .IR sigmask , .\"O then does the "select" function, and then restores the original .\"O signal mask. .I sigmask は、シグナルマスク .RB ( sigprocmask (2) を参照) へのポインタである。 .I sigmask が NULL でない場合、 .BR pselect () は .I sigmask が指しているシグナルマスクで現在のシグナルマスクを置き換えてから、 "select" 関数を実行し、 終了後にシグナルマスクを元のシグナルマスクに戻す。 .PP .\"O Other than the difference in the precision of the .\"O .I timeout .\"O argument, the following .\"O .BR pselect () .\"O call: .I timeout 引き数の精度の違いを除くと、以下の .BR pselect () の呼び出しは、 .nf ready = pselect(nfds, &readfds, &writefds, &exceptfds, timeout, &sigmask); .fi .\"O is equivalent to .\"O .I atomically .\"O executing the following calls: 次のコールを .I atomic に実行するのと等価である。 .nf sigset_t origmask; sigprocmask(SIG_SETMASK, &sigmask, &origmask); ready = select(nfds, &readfds, &writefds, &exceptfds, timeout); sigprocmask(SIG_SETMASK, &origmask, NULL); .fi .PP .\"O The reason that .\"O .BR pselect () .\"O is needed is that if one wants to wait for either a signal .\"O or for a file descriptor to become ready, then .\"O an atomic test is needed to prevent race conditions. .\"O (Suppose the signal handler sets a global flag and .\"O returns. .\"O Then a test of this global flag followed by a call of .\"O .BR select () .\"O could hang indefinitely if the signal arrived just after the test .\"O but just before the call. .BR pselect () が必要になる理由は、シグナルやファイルディスクリプタの状態変化を 待ちたいときには、競合状態を避けるために atomic なテストが必要になる からである。 (シグナルハンドラが大域フラグを設定して戻る場合を考えてみよう。 この大域フラグのテストに続けて .BR select () を呼び出すと、 シグナルがテストの直後かつ呼び出しの直前に届いた時には .BR select () は永久にハングしてしまうかもしれない。 .\"O By contrast, .\"O .BR pselect () .\"O allows one to first block signals, handle the signals that have come in, .\"O then call .\"O .BR pselect () .\"O with the desired .\"O .IR sigmask , .\"O avoiding the race.) 一方、 .BR pselect () を使うと、まずシグナルを禁止 (block) して、入ってくるシグナルを操作し、 望みの .I sigmask で .BR pselect () を呼び出すことで、前記の競合を避けることができる。) .\"O .SS "The timeout" .SS タイムアウト .\"O The time structures involved are defined in .\"O .I .\"O and look like これらの関数で使用される時間関連の構造体は、 .I で以下のように定義されている。 .\"O .in +4n .\"O .nf .\"O struct timeval { .\"O long tv_sec; /* seconds */ .\"O long tv_usec; /* microseconds */ .\"O }; .\"O .fi .\"O .in .in +4n .nf struct timeval { long tv_sec; /* 秒 */ long tv_usec; /* マイクロ秒 */ }; .fi .in .\"O and .\"O .\"O .in +4n .\"O .nf .\"O struct timespec { .\"O long tv_sec; /* seconds */ .\"O long tv_nsec; /* nanoseconds */ .\"O }; .\"O .fi .\"O .in .in +4n .nf struct timespec { long tv_sec; /* 秒 */ long tv_nsec; /* ナノ秒 */ }; .fi .in .\"O (However, see below on the POSIX.1-2001 versions.) (POSIX.1-2001 での定義については下記の「注意」を参照) .PP .\"O Some code calls .\"O .BR select () .\"O with all three sets empty, .\"O .I nfds .\"O zero, and a non-NULL .\"O .I timeout .\"O as a fairly portable way to sleep with subsecond precision. 秒単位以下の精度でスリープを実現する 移植性の高い方法として、 3 つの集合全てを空、 .I nfds を 0 、 .I timeout を NULL でない値に設定して .BR select () を呼び出すという方法を使っているコードもある。 .PP .\"O On Linux, .\"O .BR select () .\"O modifies .\"O .I timeout .\"O to reflect the amount of time not slept; most other implementations .\"O do not do this. .\"O (POSIX.1-2001 permits either behavior.) .\"O This causes problems both when Linux code which reads .\"O .I timeout .\"O is ported to other operating systems, and when code is ported to Linux .\"O that reuses a \fIstruct timeval\fP for multiple .\"O .BR select ()s .\"O in a loop without reinitializing it. .\"O Consider .\"O .I timeout .\"O to be undefined after .\"O .BR select () .\"O returns. Linux では、 .BR select () は .I timeout を変更し、残りの停止時間を反映するようになっているが、 他のほとんどの実装ではこのようになっていない (POSIX.1-2001 はどちらの動作も認めている)。 このため、 .I timeout を参照している Linux のコードを他のオペレーティング・システムへ 移植する場合、問題が起こる。 また、ループの中で \fItimeval\fP 構造体を初期化せずにそのまま再利用して .BR select () を複数回行なっているコードを Linux へ移植する場合にも、問題が起こる。 .BR select () から復帰した後は .I timeout は未定義であると考えるべきである。 .\"O .\" .PP - it is rumored that: .\"O .\" On BSD, when a timeout occurs, the file descriptor bits are not changed. .\"O .\" - it is certainly true that: .\"O .\" Linux follows SUSv2 and sets the bit masks to zero upon a timeout. .\" .PP .\" 「BSD ではタイムアウトが起こっても、 .\" ファイルディスクリプタビットは変わらない」と聞いたことがある。 .\" 「Linux は SUSv2 に従っており、 .\" タイムアウトの際にはビットマスクを 0 にする」というのは確かに事実である。 .\"O .SH "RETURN VALUE" .SH 返り値 .\"O On success, .\"O .BR select () .\"O and .\"O .BR pselect () .\"O return the number of file descriptors contained in the three returned .\"O descriptor sets (that is, the total number of bits that are set in .\"O .IR readfds , .\"O .IR writefds , .\"O .IR exceptfds ) .\"O which may be zero if the timeout expires before anything interesting happens. 成功した場合、 .BR select () と .BR pselect () は更新された 3 つのディスクリプタ集合に含まれている ファイルディスクリプタの数 (つまり、 .IR readfds , .IR writefds , .I exceptfds 中の 1 になっているビットの総数) を返す。 何も起こらずに時間切れになった場合、 ディスクリプタの数は 0 になることもある。 .\"O On error, \-1 is returned, and .\"O .I errno .\"O is set appropriately; the sets and .\"O .I timeout .\"O become undefined, so do not .\"O rely on their contents after an error. エラーならば \-1 を返し、 .I errno に適切な値が設定される; 集合と .I timeout は未定義となるので、エラーが起こった後はそれらの内容を信頼してはならない。 .\"O .SH ERRORS .SH エラー .TP .B EBADF .\"O An invalid file descriptor was given in one of the sets. .\"O (Perhaps a file descriptor that was already closed, .\"O or one on which an error has occurred.) いずれかの集合に無効なファイルディスクリプタが指定された (おそらくは、すでにクローズされたファイルディスクリプタか、 エラーが発生したファイルディスクリプタが指定された)。 .TP .B EINTR .\"O A signal was caught; see .\"O .BR signal (7). シグナルを受信した。 .TP .B EINVAL .\"O .I nfds .\"O is negative or the value contained within .\"O .I timeout .\"O is invalid. .I n が負、または .I timeout に入っている値が不正である。 .TP .B ENOMEM .\"O unable to allocate memory for internal tables. 内部テーブルにメモリを割り当てることができなかった。 .\"O .SH VERSIONS .SH バージョン .\"O .BR pselect () .\"O was added to Linux in kernel 2.6.16. .BR pselect () はカーネル 2.6.16 で Linux に追加された。 .\"O Prior to this, .\"O .BR pselect () .\"O was emulated in glibc (but see BUGS). それ以前は、 .BR pselect () は glibc でエミュレートされていた (「バグ」の章を参照)。 .\"O .SH "CONFORMING TO" .SH 準拠 .\"O .BR select () .\"O conforms to POSIX.1-2001 and .\"O 4.4BSD .\"O .RB ( select () .\"O first appeared in 4.2BSD). .\"O Generally portable to/from .\"O non-BSD systems supporting clones of the BSD socket layer (including .\"O System V variants). .\"O However, note that the System V variant typically .\"O sets the timeout variable before exit, but the BSD variant does not. .BR select () は POSIX.1-2001 と 4.4BSD .RB ( select () は 4.2BSD で最初に登場した) に準拠する。 BSD ソケット層のクローンをサポートしている非 BSD システム (System V 系も含む) との間でだいたい移植性がある。しかし System V 系では たいがい timeout 変数を exit の前にセットするが、 BSD 系ではそうでないので注意すること。 .PP .\"O .BR pselect () .\"O is defined in POSIX.1g, and in .\"O POSIX.1-2001. .BR pselect () は POSIX.1g と POSIX.1-2001 で定義されている。 .\"O .SH NOTES .SH 注意 .\"O An .\"O .I fd_set .\"O is a fixed size buffer. .\"O Executing .\"O .BR FD_CLR () .\"O or .\"O .BR FD_SET () .\"O with a value of .\"O .I fd .\"O that is negative or is equal to or larger than .\"O .B FD_SETSIZE .\"O will result .\"O in undefined behavior. .\"O Moreover, POSIX requires .\"O .I fd .\"O to be a valid file descriptor. .I fd_set は固定サイズのバッファである。 負や .B FD_SETSIZE 以上の値を持つ .I fd に対して .BR FD_CLR () や .BR FD_SET () を実行した場合、 どのような動作をするかは定義されていない。 また、 POSIX では .I fd は有効なファイルディスクリプタでなければならないと規定されている。 .\"O Concerning the types involved, the classical situation is that .\"O the two fields of a .\"O .I timeval .\"O structure are typed as .\"O .I long .\"O (as shown above), and the structure is defined in .\"O .IR . 型宣言に関しては、昔ながらの状況では .I timeval 構造体の 2 つのフィールドは (上記のように) 両方とも .I long 型であり、構造体は .I で定義されている。 .\"O The POSIX.1-2001 situation is .\"O .\"O .in +4n .\"O .nf .\"O struct timeval { .\"O time_t tv_sec; /* seconds */ .\"O suseconds_t tv_usec; /* microseconds */ .\"O }; .\"O .fi .\"O .in .\"O .\"O where the structure is defined in .\"O .I .\"O and the data types .\"O .I time_t .\"O and .\"O .I suseconds_t .\"O are defined in .\"O .IR . POSIX.1-2001 の下では、以下のようになっている。 .in +4n .nf struct timeval { time_t tv_sec; /* 秒 */ suseconds_t tv_usec; /* マイクロ秒 */ }; .fi .in この構造体は .I で定義されており、データ型 .I time_t と .I suseconds_t は .I で定義されている。 .LP .\"O Concerning prototypes, the classical situation is that one should .\"O include .\"O .I .\"O for .\"O .BR select (). プロトタイプに関しては、昔ながらの状況で .BR select () を使いたい場合は、 .I をインクルードすればよい。 .\"O The POSIX.1-2001 situation is that one should include .\"O .I .\"O for .\"O .BR select () .\"O and .\"O .BR pselect (). POSIX.1-2001 の環境で .BR select () と .BR pselect () を使いたい場合は、 .I をインクルードすればよい。 .\"O Libc4 and libc5 do not have a .\"O .I .\"O header; under glibc 2.0 and later this header exists. ヘッダファイル .I は libc4 と libc5 にはなく、glibc 2.0 以降に存在する。 .\"O Under glibc 2.0 it unconditionally gives the wrong prototype for .\"O .BR pselect (). .\"O Under glibc 2.1 to 2.2.1 it gives .\"O .BR pselect () .\"O when .\"O .B _GNU_SOURCE .\"O is defined. .\"O Since glibc 2.2.2 the requirements are as shown in the SYNOPSIS. 悪いことに glibc 2.0 以前では .BR pselect () のプロトタイプが間違っている。 glibc 2.1 から 2.2.1 では .B _GNU_SOURCE が定義されている場合に、 .BR pselect () が提供される。 glibc 2.2.2 以降では、 .BR pselect () を使用するには、「書式」に記載された要件を満たす必要がある。 .\"O .SS "LINUX NOTES" .SS "Linux での注意" .\"O The Linux .\"O .BR pselect () .\"O system call modifies its .\"O .I timeout .\"O argument. Linux の .BR pselect () システムコールは .I timeout 引き数を変更する。 .\"O However, the glibc wrapper function hides this behavior .\"O by using a local variable for the timeout argument that .\"O is passed to the system call. .\"O Thus, the glibc .\"O .BR pselect () .\"O function does not modify its timeout argument; .\"O this is the behavior required by POSIX.1-2001. しかし、 glibc のラッパー関数は、システムコールに渡す timeout 引き数 としてローカル変数を使うことでこの動作を隠蔽している。 このため、glibc の .BR pselect () 関数は timeout 引き数を変更しない。 これが POSIX.1-2001 が要求している動作である。 .\"O .SH BUGS .SH バグ .\"O Glibc 2.0 provided a version of .\"O .BR pselect () .\"O that did not take a .\"O .I sigmask .\"O argument. glibc 2.0 では、 .I sigmask 引き数を取らないバージョンの .BR pselect () が提供されていた。 .\"O Starting with version 2.1, glibc provided an emulation of .\"O .BR pselect () .\"O that was implemented using .\"O .BR sigprocmask (2) .\"O and .\"O .BR select (). .\"O This implementation remained vulnerable to the very race condition that .\"O .BR pselect () .\"O was designed to prevent. バージョン 2.1 以降の glibc では、 .BR pselect () は .BR sigprocmask (2) と .BR select () を使ってエミュレートされていた。 この実装にはきわどい競合条件において脆弱性が残っていた。 この競合条件における問題を防止するために .BR pselect () は設計されたのである。 .\"O Modern versions of glibc use the (race-free) .\"O .BR pselect () .\"O system call on kernels where it is provided. 最近のバージョンの glibc では、カーネルがサポートしている場合には、 (競合が起こらない) .BR pselect () システムコールが使用される。 .\"O On systems that lack .\"O .BR pselect (), .\"O reliable (and more portable) signal trapping can be achieved .\"O using the self-pipe trick .\"O (where a signal handler writes a byte to a pipe whose other end .\"O is monitored by .\"O .BR select () .\"O in the main program.) .BR pselect () がないシステムにおいて、 シグナルの捕捉を信頼性があり (移植性も高い) 方法で行うには、 自己パイプ (self-pipe) という技を使うとよい (シグナルハンドラはパイプへ 1 バイトのデータを書き込み、同じパイプのもう一端をメインプログラムの .BR select () で監視するという方法である)。 .\"O Under Linux, .\"O .BR select () .\"O may report a socket file descriptor as "ready for reading", while .\"O nevertheless a subsequent read blocks. .\"O This could for example .\"O happen when data has arrived but upon examination has wrong .\"O checksum and is discarded. .\"O There may be other circumstances .\"O in which a file descriptor is spuriously reported as ready. .\"O .\" Stevens discusses a case where accept can block after select .\"O .\" returns successfully because of an intervening RST from the client. .\"O Thus it may be safer to use .\"O .B O_NONBLOCK .\"O on sockets that should not block. Linux では、 .BR select () がソケットファイルディスクリプタで "読み込みの準備ができた" と報告した場合でも、 この後で read を行うと停止 (block) することがある。このような状況は、 例えば、データが到着したが、検査でチェックサム異常が見つかり廃棄された時 などに起こりえる。他にもファイルディスクリプタが準備できたと間違って 報告される状況が起こるかもしれない。 .\" Stevens が挙げたのは、select が成功して復帰してから accept が .\" 呼ばれるまでの間に、クライアントから RST が送られると、 .\" accept が停止する場合である。 したがって、停止すべきではないソケットに対しては .B O_NONBLOCK を使うとより安全であろう。 .\"O .\" Maybe the kernel should have returned EIO in such a situation? .\" 多分、このような状況ではカーネルは EIO を返してしまう? .\"O On Linux, .\"O .BR select () .\"O also modifies .\"O .I timeout .\"O if the call is interrupted by a signal handler (i.e., the .\"O .B EINTR .\"O error return). .\"O This is not permitted by POSIX.1-2001. .\"O The Linux .\"O .BR pselect () .\"O system call has the same behavior, .\"O but the glibc wrapper hides this behavior by internally copying the .\"O .I timeout .\"O to a local variable and passing that variable to the system call. Linux では、 .BR select () がシグナルハンドラにより割り込まれた場合 (つまり .B EINTR エラーが返る場合)、 .I timeout も変更する。 これは POSIX.1-2001 では認められていない挙動である。 Linux の .BR pselect () システムコールも同じ挙動をするが、 glibc のラッパー関数がこの挙動を隠蔽している。 具体的には、glibc のラッパー関数の内部で、 .I timeout をローカル変数にコピーし、 このローカル変数をシステムコールに渡している。 .\"O .SH EXAMPLE .SH 例 .nf #include #include #include #include #include .\"O int .\"O main(void) .\"O { .\"O fd_set rfds; .\"O struct timeval tv; .\"O int retval; .\"O .\"O /* Watch stdin (fd 0) to see when it has input. */ .\"O FD_ZERO(&rfds); .\"O FD_SET(0, &rfds); .\"O .\"O /* Wait up to five seconds. */ .\"O tv.tv_sec = 5; .\"O tv.tv_usec = 0; .\"O .\"O retval = select(1, &rfds, NULL, NULL, &tv); .\"O /* Don't rely on the value of tv now! */ .\"O .\"O if (retval == \-1) .\"O perror("select()"); .\"O else if (retval) .\"O printf("Data is available now.\\n"); .\"O /* FD_ISSET(0, &rfds) will be true. */ .\"O else .\"O printf("No data within five seconds.\\n"); .\"O .\"O exit(EXIT_SUCCESS); .\"O } int main(void) { fd_set rfds; struct timeval tv; int retval; /* stdin (fd 0) を監視し、入力があった場合に表示する。*/ FD_ZERO(&rfds); FD_SET(0, &rfds); /* 5 秒間監視する。*/ tv.tv_sec = 5; tv.tv_usec = 0; retval = select(1, &rfds, NULL, NULL, &tv); /* この時点での tv の値を信頼してはならない。*/ if (retval == \-1) perror("select()"); else if (retval) printf("今、データが取得できました。\\n"); /* FD_ISSET(0, &rfds) が true になる。*/ else printf("5 秒以内にデータが入力されませんでした。\\n"); exit(EXIT_SUCCESS); } .fi .\"O .SH "SEE ALSO" .SH 関連項目 .\"O For a tutorial with discussion and examples, see .\"O .BR select_tut (2). 考察と使用例の書かれたチュートリアルとして、 .BR select_tut (2) がある。 .LP .\"O For vaguely related stuff, see 関係がありそうなものを挙げておく: .BR accept (2), .BR connect (2), .BR poll (2), .BR read (2), .BR recv (2), .BR send (2), .BR sigprocmask (2), .BR write (2), .BR epoll (7), .BR time (7)