467,077 Members | 1,070 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,077 developers. It's quick & easy.

Reading file while another process is writing to it.

Expert 256MB
Is it possible to open a file for reading, while another process has it open for writing to it? One of the problems is that the process that is writing to it is written in C and is running on a vxworks OS. I'm reading the file using MFC on a windows XP PC. The file is stored on a Windows 2000 server. I tried using CStdioFile.Open using the CFile::modeRead flag. When did this i received a CFileException with the cause stating CFileException::sharingViolation SHARE.EXE was not loaded, or a shared region was locked. Then i opened the file with the CFile::modeRead | CFile::shareDenyNone. This allows me to open the file, but when i do a CStdioFile::ReadString() I sometimes read the file just fine, other times I am unable to read anything (no data is received from the file). Any ideas or sujestions? I thought about using a different class to access the file.
Apr 18 '08 #1
  • viewed: 14766
Share:
6 Replies
weaknessforcats
Expert Mod 8TB
You cannot have someone reading a file while someone else is writing to it. The writer changes the file structure underneath the feet of the reader. This is called a race condition and it usually precedes a crash.

What you have to do is lock the file. That is, obtain a lock. A writer checks to see iof the lock is set. If so, it waits an tries again later. Ditto for the reader.

The easiest thing to do is to use a lock file. A writer tries to open the lock file and if it does not exists, creates the lock file. A reader then opens the lock file and because it opens, it means the data file is locked so the reader goes away or waits.

The writer deletes the lock file after the write operation is complete.

The reader needs to do the same thing. If the lock file does not open, the reader creates it and that causes a writer to understand the data file is locked so the writer waits and tries to open the lock file again.
Apr 20 '08 #2
Nepomuk
Expert 2GB
You cannot have someone reading a file while someone else is writing to it...
Little correction:
You cannot have someone reading a file while someone else is writing to it with Windows! With Linux (and I would guess Unixes as well - so that would probably include OS X too) it's possible. And, in some (ok, very few) cases, actually a good idea.

Greetings,
Nepomuk
Apr 21 '08 #3
weaknessforcats
Expert Mod 8TB
Little correction:
You cannot have someone reading a file while someone else is writing to it with Windows! With Linux (and I would guess Unixes as well - so that would probably include OS X too) it's possible. And, in some (ok, very few) cases, actually a good idea.
So, while you are reading this tree another thread comes along and alters the structure of the tree or moves stuff around or deletes stuff making the reader use invalid addresses and this is OK?
Apr 22 '08 #4
100+
i used a function like this on a fedora server last year, it i could open a text file and watch it have messages append to the end, updateing live. Im not sure how this function worked exactly but it is possible. I will try and digg up the modual and let you know when i get home.
Good luck
Apr 22 '08 #5
Nepomuk
Expert 2GB
So, while you are reading this tree another thread comes along and alters the structure of the tree or moves stuff around or deletes stuff making the reader use invalid addresses and this is OK?
First part: Yes!
Second part: I don't think so.

Greetings,
Nepomuk
Apr 22 '08 #6
weaknessforcats
Expert Mod 8TB
Multiple readers are OK. However, the presence of one writer requires a lock. That is, the reader has to lock to prevent the writer from screwing up the read and the writer has to lock top prevent the reader from crashing the program.

Depending upon your file structure, you might be able to lock just a section of the file, maybe by locking a memory page, rather than the entire file.
Apr 23 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Gabe Moothart | last post: by
8 posts views Thread by Tom Wright | last post: by
3 posts views Thread by =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.