Does use of Linux pread avoid "unavailability of data for reading written by a different thread"?

Viewed 59

Please assume below scenario (OS = Redhat Linux),

Option A :

Writer Thread : Writes to a file using FD=1. Sets last written position and size in a std::atomic<int64_t> variable.

Edit for more clarity : write done using write C function call. https://www.man7.org/linux/man-pages/man2/write.2.html

Reader Thread : Reads above file using a different FD=2 at value saved in above std::atomic<int64_t> variable.

Then I presume it is possible that, above read thread NOT being able to read all data written by the writer thread (i.e. a read call with FD=2 could return a lesser no of bytes). Since there could be buffering at FD level.

======================================================================================

Option B:

Writer Thread : Writes to a file using FD=1. Sets last written position and size in a std::atomic<int64_t> variable.

Edit for more clarity : Only appends done (no overwrite takes place).write done using write C function call. https://www.man7.org/linux/man-pages/man2/write.2.html

Reader Thread : Reads(using pread) above file using the same FD=1 at value saved in above std::atomic<int64_t> variable.

https://man7.org/linux/man-pages/man2/pwrite.2.html

Now, is it guaranteed that ALL data written by Writer thread is read by Reader Thread ?

1 Answers

Buffering is at libc level, keeping the data around before handing it over to the kernel. pread is a syscall, it will only give you the data that has already been shown to the kernel.

So no. pread saves you the extra calls for seek+read, it does not solve any buffering issues.

How can you ensure that the kernel gets to see your data? You haven't shown your writer code, but usually calling fflush should do it.

Related