LoginSignup
1
0

More than 5 years have passed since last update.

ファイルディスクリプタが読み書き可能かどうかを判定する

Posted at

ファイルディスクリプタに対して、読み書き可能かどうかを即時チェックする関数です。

int isReadable(int fd) { 
   fd_set fdset;
   struct timeval t = {0, 0};

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

   return select(fd + 1, &fdset, NULL, NULL, &t) > 0;
}
int isWritable(int fd) { 
   fd_set fdset;
   struct timeval t = {0, 0};

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

   return select(fd + 1, NULL, &fdset, NULL, &t) > 0;
}

戻り値は読み書き可能なら 1 が、そうでなければ 0 が返ります。

もともとソケット用に作った関数ですが、引数はファイルディスクリプタなので UNIX ならデバイスなども対象に含まれます。

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0