4 ms·
Here's one that's not super well-known: writes to a pipe are atomic as long as the write size is <= PIPE_BUF, which is at least 512 bytes (on Linux it's a full
by ridiculous_fish 10y ago
Here's one that's not super well-known: writes to a pipe are atomic as long as the write size is <= PIPE_BUF, which is at least 512 bytes (on Linux it's a full 4k). So pipes can be used as a naïve message queue: ensure each message is under the limit in size, and they will not be split.
Anyone know if there's a similar guarantee for files?
- colmmacc 10y agoThey won't be split, bit one thing to watch out for is that they can be coalesced.
- brobinson 10y agoI read a while back that this PIPE_BUF restriction also applies to multiple writers appending a file.
- deleted 10y ago[deleted]
- adrianratnapala 10y agoI think Linux goes out of its ways to make writes <= 512 bytes atomic. Helpful for writing log files. But I don't think this is a true guarantee in any standardised sense.
- bluetomcat 10y agoThe PIPE_BUF POSIX requirement is about guaranteeing that when multiple processes write less than PIPE_BUF to the same pipe, the reader will not see its input intermingled from different processes. It is often wrongly interpreted as "a single write() of less than PIPE_BUF will be retrieved via a single read() on the other side".
- the8472 10y agoOpening with O_APPEND you get atomic appends if they're <= PIPE_BUF. And the list already mentions atomic memory operations. Those also apply to memory-mapped files.
- deleted 10y ago[deleted]