tech-userlevel archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: FIFO blocking behaviour
> Date: Wed, 16 Sep 2026 19:36:59 +0200
> From: Edgar Fuß <ef%math.uni-bonn.de@localhost>
>
> I couldn't find anything substantial about fifo semantics in the man pages
> or SUS.
>
> Is it defined that writing to a fifo will block until someone has read
> the data from the other end?
No, quite the opposite: it goes into a buffer of an unspecified size,
so it may or may not block depending on implementation details. From
POSIX.1-2024:
An attempt to write to a pipe or FIFO has several major
characteristics:
[...]
o Blocking/immediate: Blocking is only possible with O_NONBLOCK
clear. If there is enough space for all the data requested to be
written immediately, the implementation should do so. Otherwise,
the calling thread may block; that is, pause until enough space
is available for writing. The effective size of a pipe or FIFO
(the maximum amount that can be written in one operation without
blocking) may vary dynamically, depending on the implementation,
so it is not possible to specify a fixed value for it.
https://pubs.opengroup.org/onlinepubs/9799919799/functions/write.html#tag_17_699_03
On NetBSD, the effective size usually ranges from 16k to 64k, though
these numbers are not promises and it may vary from pipe to pipe --
all you are promised is that it is _at least_ PIPE_BUF, which POSIX
guarantees is at least 512
(https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/limits.h.html).
You can query how many bytes it is currently possible to write without
blocking by ioctl(FIONSPACE). (This is nonstandard.) Here's an
example of using FIONSPACE to ensure we have completely filled up the
pipe's buffer without blocking so that the _next_ attempt to write
_will_ block:
https://nxr.NetBSD.org/xref/src/tests/kernel/t_fdrestart.c?r=1.4#103
(Just ignore the `rump_sys_' part of the names you see there:
rump_sys_write(...) is a stand-in for write(...).)
Home |
Main Index |
Thread Index |
Old Index