KDOC 283: パイプはファイルディスクリプタがクローズされると終了する
この文書のステータス
- 作成
- 2024-11-09 貴島
- レビュー
- 2024-11-14 貴島
概要
パイプの読み込み側を参照しているファイルディスクリプタがクローズされている場合、 パイプバッファへのwrite(2) は SIGPIPEシグナルを生成する。
man "pipe(7)" | grep -A 6 "If all file"
(read(2) will return 0). If all file descriptors referring to the read
end of a pipe have been closed, then a write(2) will cause a SIGPIPE
signal to be generated for the calling process. If the calling process
is ignoring this signal, then write(2) fails with the error EPIPE. An
application that uses pipe(2) and fork(2) should use suitable close(2)
calls to close unnecessary duplicate file descriptors; this ensures
that end-of-file and SIGPIPE/EPIPE are delivered when appropriate.
関連
抽象。
- 受け手がいなくなると、送り手に知らせて止める
関連。
- 対比: KDOC 279: パイプのバッファはいっぱいになるとwriteをブロックする。同じwrite(2)でも、読み手がいなくなるとSIGPIPEで止まり、読み手が遅いとブロックして待つ
- 動機: KDOC 282: 単体だと終了しないプロセスでパイプを使うと終了するのはなぜか?