この方法でノンブロッキング書き込みを実行できるFIFOを作成しました。
// others, searching for a non-blocking FIFO-writer may copy this ;-)
mkfifo("/tmp/myfifo", S_IRWXU);
int fifo_fd = open("/tmp/myfifo", O_RDWR);
fcntl(fifo_fd, F_SETFL, fcntl(fifo_fd, F_GETFL) | O_NONBLOCK);
// and then in a loop:
LOGI("Writing into fifo.");
if (write(fifo_fd, data, count) < 0) {
LOGE("Failed to write into fifo: %s", strerror(errno));
}
ノンブロッキング書き込みは完璧に機能しますが、私のロギングのために私は見ることができます:
(..)
Writing into fifo.
Writing into fifo.
Writing into fifo.
Failed to write into fifo: Try again
Writing into fifo.
Failed to write into fifo: Try again
Writing into fifo.
Failed to write into fifo: Try again
のようなリーダーを作成するcat /tmp/myfifo > foo.out
と、エラーはなくなります。
FIFOはリングバッファのように機能し、バッファがいっぱいになると最初に書き込まれたバイトをドロップすると思いました。しかし今、私はそれが最初のバイトが読まれるまで新しいバイトをブロック/防止することを学びました。
FIFOがリングバッファのように動作するように、他の簡単な方法や追加の操作を知っている人はいますか?