
02-11-2002, 01:42 AM
|
|
Retired Moderator
|
|
Join Date: Jan 2001
Posts: 2,603
|
|
Anyone have a copy of POSIX?
Does anyone know offhand if POSIX requires read/write coherence? ie, if one process write(2)s to a region of a file, and another process read(2)s from the same region, is the read guaranteed to get either the original version or the new version (but not some combination of the two)?
It would be an odd system where this wasn't the case, but I'd like to make sure.
__________________
Dr. Colin Percival, FreeBSD Security Officer
Online backups for the truly paranoid: http://www.tarsnap.com/
|

02-11-2002, 09:34 AM
|
|
Web Hosting Master
|
|
Join Date: Aug 2000
Posts: 1,167
|
|
I assume you mean a simultaneous read/write. . .
It's a bit early in the morning for me, but my understanding is that the POSIX specification does not call for preventing "dirty" reads and writes and does not enforce cooperation among processes that simultaneously access a file. You have to explicitly lock the file. Without locks, a process reading from a file that is being written to would retrieve the orginal version.
|

02-11-2002, 09:43 AM
|
|
Retired Moderator
|
|
Join Date: Jan 2001
Posts: 2,603
|
|
I don't mind if I get the old version when I read from the file. What I'm looking for is a guarantee that I won't get a combination of both... ie if the disk sector size is 512 bytes, and I'm writing/reading a 1024 byte record, is it possible for me to read the first half of the new record and the second half of the old record through some unfortunate timing?
__________________
Dr. Colin Percival, FreeBSD Security Officer
Online backups for the truly paranoid: http://www.tarsnap.com/
|

02-11-2002, 10:34 AM
|
|
Registered User
|
|
Join Date: Apr 2001
Location: Depok, Indonesia
Posts: 986
|
|
I believe it is not possible, but I'm not sure either. It is even possible that POSIX doesn't address this issue. For safety I suggest you lock the portion of file you are trying to read/write using fcntl.
|

02-11-2002, 10:47 AM
|
|
Web Hosting Master
|
|
Join Date: Aug 2000
Posts: 1,167
|
|
Quote:
Originally posted by cperciva
What I'm looking for is a guarantee that I won't get a combination of both... ie if the disk sector size is 512 bytes, and I'm writing/reading a 1024 byte record, is it possible for me to read the first half of the new record and the second half of the old record through some unfortunate timing?
|
Write buffers aren't flushed (i.e., file written and pointers updated) until the file is closed. So, I believe you'd safely get the complete original version even in the midst of a write. This would be especially true with higher-level functions such as fread() and fwrite().
The way to guarantee your read result is to stat the file and wait for it to go quiet, make an exclusive lock on it and then execute the read.
Last edited by alchiba; 02-11-2002 at 11:00 AM.
|

02-12-2002, 07:08 AM
|
|
Registered User
|
|
Join Date: Apr 2001
Location: Depok, Indonesia
Posts: 986
|
|
Quote:
Originally posted by alchiba
Write buffers aren't flushed (i.e., file written and pointers updated) until the file is closed.
|
I believe fsync() and fdatasync() can be used to flush unwritten buffers prior to closing the fd.
|

02-12-2002, 11:48 AM
|
|
Web Hosting Master
|
|
Join Date: Dec 2001
Location: Detroit, MI
Posts: 1,067
|
|
This is a very difficult question to answer. I would suggest going to www.xopen.org and trying various searches. Personally, I could not find a good keyword combination that came up with results related to the question.
It seems to me that the POSIX specification really has no place in placing guarantees on data integrity. This should be left to the file system implementors. In many cases a file can be relocated and the already executing read statement will be reading from a now invalid portion of the disk while a call to tell() may describe the length of the file as being longer than it really is. I think locking the file is the only safe way to go, especially for portability issues and certainty that your application will work across different file systems / platforms.
Scott, confused as usual.
__________________
<!-- boo! -->
|

02-12-2002, 11:50 AM
|
|
Retired Moderator
|
|
Join Date: Jan 2001
Posts: 2,603
|
|
File locking would be good. Anyone know of a good *portable* method for file locking?
Unfortunately it seems that locking was left off the requirements sheet.
__________________
Dr. Colin Percival, FreeBSD Security Officer
Online backups for the truly paranoid: http://www.tarsnap.com/
|

02-12-2002, 11:53 AM
|
|
Web Hosting Master
|
|
Join Date: Dec 2001
Location: Detroit, MI
Posts: 1,067
|
|
By portable, do you mean cross platform (ie Unix/Windows) or just your typical Unix base?
When in doubt I almost always use semop() to do all of my locking. Generally wrapping the IPC stuff in C++ classes and going from there makes for nice semi-automatic synchronization schemes. If you are dealing with pthreads and a single application then a simple mutex should do the trick.
Scott
__________________
<!-- boo! -->
|

02-12-2002, 12:01 PM
|
|
Retired Moderator
|
|
Join Date: Jan 2001
Posts: 2,603
|
|
For "portable", read "portable to any POSIX-compliant system with an ANSI C compiler, including Windows, BSD, Linux, Solaris, Irix, and VMS".
And I want safeness across multiple processes, not just within a single process.
__________________
Dr. Colin Percival, FreeBSD Security Officer
Online backups for the truly paranoid: http://www.tarsnap.com/
|

02-12-2002, 12:26 PM
|
|
Web Hosting Master
|
|
Join Date: Dec 2001
Location: Detroit, MI
Posts: 1,067
|
|
As someone else has mentioned, using fcntl() is probably your best bet. Research F_SETLK, F_GETLK, and F_SETLKW specifically. This appears to be available in FreeBSD and Linux, but I don't see any mention for Visual C++, though possibly with Cygnus. No access to VMS so I can't see for sure.
I don't think there is a standard mechanism available that will work across all platforms so you are likely stuck rolling your own. Then again, I could just be missing it...
Good luck.
__________________
<!-- boo! -->
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
| Postbit Selector |
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
| Login: |
|
|
| Advertisement: |
|
|
| Web Hosting News: |
|
|
|