OnTheFly
OnTheFly

Reputation: 2101

Unable to read same file in parallel

I am trying to read a logfile in two different applications at the same time. But CreateFile (http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx) with share-mode #FILE_SHARE_READ always fails on the second call with error-code 32: ERROR_SHARING_VIOLATION: - The process cannot access the file because it is being used by another process.

When the logfile is written in modes FILE_SHARE_WRITE or FILE_SHARE_READ | FILE_SHARE_WRITE the file can not be read by any of the two applications only when FILE_SHARE_READ is used for the writer-application and then it only works if the reader-applications use FILE_SHARE_WRITE or FILE_SHARE_READ | #FILE_SHARE_WRITE, not when using #FILE_SHARE_READ. Strange .... Any suggestions?

Thank you very much.

Amendment:

Writer-Application: CreateFile(file,GENERIC_WRITE,FILE_SHARE_READ,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0)
Reader-Applications: CreateFile(file,GENERIC_READ,FILE_SHARE_READ | FILE_SHARE_WRITE,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0)

Amendment 2:

When using the calls from the first Amendment the 1st Reader-Application can successfully get the filehandle, but the 2nd Reader-Application will fail with ERROR_SHARING_VIOLATION.

Amendment 3: One pointed me to the solution!

The problem was that I used logical Or instead of bitwise Or for the share-mode: FILE_SHARE_READ Or FILE_SHARE_WRITE = 1 Or 2 = 1, but it should be FILE_SHARE_READ | FILE_SHARE_WRITE = 1 | 2 = 3. So could not work as the Writer-Application required FILE_SHARE_WRITE.

Upvotes: 1

Views: 1961

Answers (4)

Marcin Skoczylas
Marcin Skoczylas

Reputation: 19

Yes, I hit the same wall with Memory Mapped Files - I wanted to create a mmap file to read/write by multiple processes... Works as a charm on Linux/MacOS.

It seems you can do this only in Unix OSes, I was not able to recreate this behaviour in Windows at all.

Upvotes: -1

Hans Passant
Hans Passant

Reputation: 941218

It isn't strange, but it does forever trip up programmers that the logic is inverted. When a program creates a log file then it only has to specify FILE_SHARE_READ to allow another process to read the file. It however acquired another capability when doing so, it asked for GENERIC_WRITE so it could write the file. No objection from the operating system, it will forever keep that capability until it closes the file.

What then goes wrong is another process trying to open the file but deny the right to write the file. In other words only specify FILE_SHARE_READ and not FILE_SHARE_WRITE. That cannot work, the first process already acquired the right to write, you cannot jerk the floor mat and say it shouldn't write to the file. That would be a nasty Denial Of Service attack. So the operating system says no and fails the CreateFile() call.

The other process must specify FILE_SHARE_WRITE.

Upvotes: 6

VRK
VRK

Reputation: 400

you can not do multiple operation i.e. read/write operation simultaneously. you might have to make a copy of it in the memory if file is not too large.

Upvotes: 0

tnchalise
tnchalise

Reputation: 1583

You cannot access a same file for two different operations like, WRITE_TO or READ_FROM. Instead, you can make a clone of it for two different operations.

Upvotes: 0

Related Questions