質問

私は、UDPソケットがパケットを受信したかどうかを確認するためのLinux / ARMプラットフォーム上でselect()使用しています。私はそれがタイムアウトする前に返された場合(パケットを検出した)select呼び出しに残ったどのくらいの時間を知っているしたいと思います。

の線に沿って何かます:

int wait_fd(int fd, int msec)
{
    struct timeval tv;
    fd_set rws;

    tv.tv_sec = msec / 1000ul;
    tv.tv_usec = (msec % 1000ul) * 1000ul;

    FD_ZERO( & rws);
    FD_SET(fd, & rws);

    (void)select(fd + 1, & rws, NULL, NULL, & tv);

    if (FD_ISSET(fd, &rws)) { /* There is data */
        msec = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
        return(msec?msec:1);
    } else { /* There is no data */
        return(0);
    }
}
役に立ちましたか?

解決

最も安全な事がselect()と時間、それを自分のあいまいな定義を無視することです。

だけを選択する前と後の時間を取得し、あなたが望んでいた間隔からそれを引いています。

他のヒント

私が正しくリコール場合

は、select()関数は、選択戻る時間がタイムアウト変数で返されたままタイムアウトとI / Oパラメータとを処理する。

それ以外の場合は、あなたが呼び出す前に、再度、後に現在の時刻を記録し、両者の違いを取得する必要があります。

OSX上で、 "男性が選ぶ" よります:

 Timeout is not changed by select(), and may be reused on subsequent calls, however it 
 is good style to re-ini-tialize it before each invocation of select().

あなたが選択し呼び出す前にgettimeofdayを呼び出し、その後、出口でのgettimeofdayする必要があります。

[編集] Linuxが若干異なっていると思われます:

   (ii)   The select function may update the timeout parameter to indicate
          how much time was left. The pselect  function  does  not  change
          this parameter.

   On Linux, the function select modifies timeout to reflect the amount of
   time not slept; most other implementations do not do this.  This causes
   problems  both  when  Linux code which reads timeout is ported to other
   operating systems, and when code is  ported  to  Linux  that  reuses  a
   struct  timeval  for  multiple selects in a loop without reinitializing
   it.  Consider timeout to be undefined after select returns.
選択

Linuxは()過去を持っている時間を反映するために、timeout引数を更新します。

これは、他のシステム間で移植ではありません(したがって、OS Xのマニュアルの警告は、上記引用された)が、Linuxで動作しないことに注意してください。

ギラッド

を選択し、使用しないでください、あなたのコードで1024年よりFD大きなで試してみて、あなたが得るものを見ます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top