lkml.org 
[lkml]   [2003]   [Aug]   [27]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
SubjectLockless file reading
From
Date
(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
systems?

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

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

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;
usleep(10);
}
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 majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

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