Question

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!!

No correct solution

OTHER TIPS

All of your processes are requesting exclusive access, which is why they fail to get a handle to the file at the same time. You must specify both FILE_SHARE_READ and FILE_SHARE_WRITE in all of the processes, since they are all requesting reading and writing access to the file. That in of itself will not cause deadlocks. So you must be misusing LockFileEx() instead, but you did not show that code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top