I am writing a Service which will create a file and write Records in it, other processes(there are four concurrent processes) will read a record and modify some of its values. I am currently using LockFileEx()
and UnLockFileEx()
functions for sequencing.
I am creating File through my service using createFile function as follows
FILEHANDLE = ::CreateFile ( TEXT("C:\\abc.BIN"),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_ALWAYS ,
FILE_ATTRIBUTE_NORMAL,
NULL );
and other processes get Handle of file as given below:
FILEHANDLE = ::CreateFile(TEXT("C:\\abc.BIN"),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
The problem is that two or more processes can not get handle of the file at the same time. When one process is writing the file other process can not even get filehandle just for reading, even though I am using LockFileEx()
function which locks a specified region of the file not the complete file. I get system error code 32 every time when I try to open file by setting FILE_SHARED_READ
flag or when I try to open it exclusively. When I set FILE_SHARE_READ | FILE_SHARE_WRITE
, a deadlock occurs.
Pls tell me any solution so that I can open file by multiple processes.
Thank you!!