title: Two processes reading/writing to the same file - Win32 using LockFileEx() Function
body: <p>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 <code>LockFileEx()</code> and <code>UnLockFileEx()</code> functions for sequencing. </p>
  
  <p>I am creating File through my service using createFile function as follows</p>
  
  <pre><code>FILEHANDLE = ::CreateFile ( TEXT("C:\\abc.BIN"),
                              GENERIC_READ | GENERIC_WRITE,
                              0,
                              NULL,
                              OPEN_ALWAYS ,
                              FILE_ATTRIBUTE_NORMAL,
                              NULL );
  </code></pre>
  
  <p>and other processes get Handle of file as given below:</p>
  
  <pre><code>FILEHANDLE = ::CreateFile(TEXT("C:\\abc.BIN"),
                            GENERIC_READ |  GENERIC_WRITE,
                            0,
                            NULL,
                            OPEN_EXISTING,
                            FILE_ATTRIBUTE_NORMAL,
                            NULL);
  </code></pre>
  
  <p>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 <code>LockFileEx()</code> 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 <code>FILE_SHARED_READ</code> flag or when I try to open it exclusively. When I set <code>FILE_SHARE_READ | FILE_SHARE_WRITE</code>, a deadlock occurs. </p>
  
  <p>Pls tell me any solution so that I can open file by multiple processes. </p>
  
  <p>Thank you!!</p>