12
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

非同期I/OとノンブロッキングI/O

Last updated at Posted at 2019-12-08

背景

同期/非同期、ブロッキング/ノンブロッキングの話が出てきて個人的にまとめようと思って書いた。

非同期IOの特徴

I/O処理が完了したタイミングで通知するI/Oモデルを非同期I/Oという。
ユーザへのI/O完了通知はシグナルかコールバックで行われる。
通知があるまでプロセスは他の処理を進めることが出来る。

io_prep_pread(3), io_prep_pwrite(3), io_submit(2)を用いて実装できる。
また、POSIXでの実装にaio_writeやaio_readなどのライブラリ関数も存在する。

struct {
    pthread_cond_t cond;
    pthread_mutex_t mtx;
    int flag;
} notified = {
    .cond   = PTHREAD_COND_INITIALIZER,
    .mtx    = PTHREAD_MUTEX_INITIALIZER,
    .flag   = 0
};

void thread_func(union sigval sv)
{
    printf("%s : aio_read from fd %d completed \n",
        __func__, sv.sival_int);

    pthread_mutex_lock(&notified.mtx);
    notified.flag = 1;
    pthread_cond_signal(& notified.cond);
    pthread_mutex_unlock(& notified.mtx);
}

main ()
{
    char a[BUFSIZ];
    struct aiocb aio = {
        .aio_offset   = 0,
        .aio_buf      = a,
        .aio_nbytes   = sizeof(a),
        .aio_reqprio  = 0,
        .aio_sigevent = {
            .sigev_notify            = SIGEV_THREAD,
            .sigev_notify_function   = thread_func,
            .sigev_notify_attributes = NULL
        }
    };

    aio.aio_fildes = open(argv[1], O_RDONLY);
    aio.aio_sigevent.sigev_value.sival_int = aio.aio_fildes;
    aio_read(&aio);

    /* do other jobs */

    pthread_mutex_lock(&notified.mtx);
    while (!notified.flag)
        pthread_cond_wait(&notified.cond, &notified.mtx);
    pthread_mutex_unlock(&notified.mtx);
}

使ってる関数のまとめ。それぞれの内容は下記

io_queue_init(2) - 非同期I/Oの準備
io_prep_pwrite(3) -
io_submit(2) - 非同期 I/O ブロックを処理待ちキューに登録する
io_getevents(2) - 完了キューから非同期 I/O イベントを読み出す
io_queue_release(3) - ユーザ空間に関連したコンテキストを解放する

ノンブロッキングIOの特徴

ノンブロッキングI/OではI/O対象のファイルディスクリプタが準備完了していない場合ユーザへ
即座にエラーが返る(EAGAIN)

open(2)にO_NONBLOCKフラグを指定することで実装できる。
O_NONBLOCKを指定してopenされたファイルディスクリプタに対する操作でプロセスを待たせることはなくなります。

# include 
# include 
# include 
# include 
# include 
# include 
# include 
# include 

void set_fl(int fd, int flags);
void clr_fl(int fd, int flags);

char buf[100000];

int main(void) {
  int ntowrite, nwrite;
  char *ptr;

  ntowrite = read(STDIN_FILENO, buf, sizeof(buf));
  fprintf(stderr, "read %d byts\n", ntowrite);
  set_fl(STDOUT_FILENO, O_NONBLOCK);

  for (ptr = buf; ntowrite > 0; ) {
    errno = 0;
    nwrite = write(STDOUT_FILENO, ptr, ntowrite);
    fprintf(stderr, "nwrite = %d, errno = %d, err_message = '%s'\n", nwrite, errno, strerror(errno));
    if (nwrite > 0) {
      ptr += nwrite;
      ntowrite -= nwrite;
    }
  }

  clr_fl(STDOUT_FILENO, O_NONBLOCK);
  return 0;
}

void set_fl(int fd, int flags) {
  int val;

  if ((val = fcntl(fd, F_GETFL, 0)) < 0) {
    fprintf(stderr, "fcntl F_GETFL error");
    exit(1);
  }

  val |= flags;  /* turn on flags */

  if (fcntl(fd, F_SETFL, val) < 0) {
    fprintf(stderr, "fcntl F_SETFL error");
    exit(1);
  }
}

void clr_fl(int fd, int flags) {
  int val;

  if ((val = fcntl(fd, F_GETFL, 0)) < 0) {
    fprintf(stderr, "fcntl F_GETFL error");
    exit(1);
  }

  val &= ~flags;  /* turn flags off */

  if (fcntl(fd, F_SETFL, val) < 0) {
    fprintf(stderr, "fcntl F_SETFL error");
    exit(1);
  }
}

両者の違い

通知をバックグラウンドで待つのが非同期。
ステータスを決まった間隔で確認しに行くのがノンブロッキング。

これらを駆使してかつ、I/Oの多重化(I/O Multiplexing)を使うのがイベント駆動アーキテクチャにつながっていく話です。

C10K問題

「C10K問題」(クライアント1万台問題)とは、ハードウェアの性能上は問題がなくても、あまりにもクライアントの数が多くなるとサーバがパンクする問題のこと。
メモリをある量だけ確保したスレッドが大量生成されると起こるらしい。

参考:https://wa3.i-3-i.info/word11592.html

spinlockについて

ちょっとだけ脱線。非同期とか調べてたら何度か出てくる用語スピンロックについて。

マルチプロセッサ環境において,各CPUが同じ資源(リソース)に同時アクセスする際に
用いられる排他制御の仕組み。 1つの資源に対して1つのロック変数をメモリー上に用意します。
そして,ロック変数を取得できたCPUだけが資源にアクセスできます。

スピン・ロックは,マルチプロセッサ・システムで手軽に排他制御を実現する方法として
よく利用されています。ただし,ビジー・ウエイト中のCPUは処理が実行できずに
待たされた状態になりますから,ビジー・ウエイトが頻繁に発生すると処理効率が
悪くなります。処理効率を考えるなら,スピン・ロックで排他制御する資源をなるべく
細かく分けるなどの工夫が必要です。

スピン・ロックの一種に,「読み書き用スピン・ロック」があります。
このスピン・ロックでは,CPUからの読み出し処理が競合した場合にはロックを行わず,
どちらからも資源にアクセスできます。書き込むときのみ,通常通り排他制御を行います。
読み書き用スピン・ロックは資源に対する参照処理が多い場合に有効です。

まとめ

参考リンク

非同期 IO について
ノンブロッキングI/Oと非同期I/Oの違いを理解する

12
7
0

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
12
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?