I/O モデル

Unix I/O モデル説明(せつめい)

I/O モデル

  • Synchronous IO
    • Blocking I/O Model (Blocking / Synchronous) : BIO, ブロッキング IO
    • Non-Blocking I/O (Non-Blocking / Synchronous) : NIO, ノンブロッキング IO
    • I/O Multiplexing (Blocking / Asynchronous): I/O 多重化(たじゅうか)
    • Signal-driven I/O (SIGIO): シグナル駆動(くどう) 、Synchronous I/O に(ぞく) する
  • Asynchronous I/O (Non-Blocking / Asynchronous): AIO, 非同期(ひどうき) IO

Five IO Models

BIO

  sequenceDiagram
    participant App as Application
    participant K as Kernel

	Note over App: process blocks in a call to recvfrom
	App ->> K: recvfrom - system call
	K ->> K: no datagram ready
	Note over K: wait for data
	K ->> K: no datagram ready
	Note over K: wait for data
	K ->> K: datagram ready
	K ->> K: copy datagram
	K ->> K: copy complete
	Note over K: copy data from kernel to user
	K ->> App: return OK
	App ->> App: process datagram

NIO

  sequenceDiagram
    participant App as Application
    participant K as Kernel

	Note over App: process repeatedly calls recvfrom, waiting for an OK return (polling)
	App ->> K: recvfrom - system call
	K ->> K: no datagram ready
	K ->> App: EWOULDBLOCK
	App ->> K: recvfrom - system call
	K ->> K: no datagram ready
	K ->> App: EWOULDBLOCK
	App ->> K: recvfrom - system call
	K ->> K: no datagram ready
	K ->> App: EWOULDBLOCK
	Note right of K: wait for data
	App ->> K: recvfrom - system call
	K ->> K: datagram ready
	Note right of K: copy data from kernel to user
	K ->> K: copy datagram
	K ->> K: copy complete
	K ->> App: return OK
	App ->> App: process datagram

IO Multiplexing

  sequenceDiagram
    participant App as Application
    participant K as Kernel

	Note over App: process repeatedly calls select, waiting for one possibly many sockets to become readable
	App ->> K: select - system call
	K ->> K: no datagram ready
	Note over K: wait for data
	K ->> K: datagram ready
	K ->> App: return readable
	Note over App: process repeatedly calls select, waiting for one possibly many sockets to become readable
	App ->> K: recvfrom - system call
	K ->> K: copy datagram
	Note right of K: copy data from kernel to user
	K ->> K: copy complete
	K ->> App: return OK
	App ->> App: process datagram

Signal-Driven IO

  sequenceDiagram
    participant App as Application
    participant K as Kernel

	Note over K: wait for data
	App ->> App: establish SIGIO signal handler
	App ->> K: sigaction - system call
	K ->> App: return
	Note over App: process continues executing
	K ->> K: datagram ready
	Note over App: process blocks while data copied into application buffer
	Note left of App: signal handler
	K ->> App: deliver SIGIO
	App ->> K: recvfrom - system call
	K ->> K: copy datagram
	Note right of K: copy data from kernel to user
	K ->> K: copy complete
	K ->> App: return OK
	App ->> App: process datagram

AIO

  sequenceDiagram
    participant App as Application
    participant K as Kernel

	Note over K: process continues executing
	App ->> K: aio_read - system call
	Note over App: wait for data
	K ->> K: no datagram ready
	K ->> App: return
	K ->> K: datagram ready
	Note right of K: copy data from kernel to user
	K ->> K: copy complete
	K ->> App: deliver signal specified in aio_read
	Note over App: signal handler process datagram

Network IO

入出力(にゅうしゅつりょく) (IO) 操作(そうさ)核心(かくしん) 概念(がいねん) は、User Space BufferKernel Space Buffer (かん) でのデータ相互(そうご) コピーです。

通常(つうじょう)以下(いか) の 2 つのステップを(ふく) みます:

  1. ネットワークデータが NIC に到着(とうちゃく) するのを() つ (read-ready) / NIC が Writable になるのを() つ (write-ready) → Kernel Space Buffer に read / write
  2. Kernel Space Buffer から User Space Buffer にデータをコピー (read) User Space Buffer から Kernel Space Buffer にデータをコピー (write)