[lkml]   [2003]   [Aug]   [27]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
SubjectLockless file reading
(Maybe a bit off topic, but I couldn't get answers elsewhere and it is
about kernel behaviour)

The question is what can happen if I read() a file that's being
simultaneously updated by a write() in another process? If I'm writing
123 over XXX, is it possible that read() returns 1X3 in some conditions?
Is the behaviour filesystem specific? Any idea about other operating

I'm thinking about implementing lockless file reads that work like:

void write_data(int fd, off_t offset, void *data, size_t size) {
pwrite(fd, data, size, offset); // or writev() or copy+single pwrite()
pwrite(fd, data, size, offset + size);

void read_data(int fd, off_t offset, void *data, size_t size) {
unsigned char buf{size*2];
for (;;) {
pread(fd, buf, size*2, offset); // or shared mmap()ed access
if (memcmp(buf, buf+size, size) == 0) break;
memcpy(data, buf, size);

The only case when I see that it would break is if we write "1212" but
pread() sees "1X1X" or "X2X2" there.

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to
More majordomo info at
Please read the FAQ at

 \ /
  Last update: 2005-03-22 13:48    [W:0.117 / U:4.936 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site