7.1.1 Sharing a File

If two (or more) processes need to share a file, one process should open the file for exclusive access by using the CreateFile function. The CreateFileMapping function uses the open file handle to create a file mapping object. This object allows the file's contents to be shared among processes that know the object's name or handle. The file handle should remain open until the processes are no longer sharing the file; this prevents non-sharing processes from reading or writing the file.

CreateFileMapping enables you to specify an object name, the number of bytes to be mapped from the file, and the read and write access to the mapped memory. The access flags must not conflict with the flags specified when the file was opened with CreateFile.

CreateFileMapping returns a handle to the file mapping object. This handle is needed to create a file view in the process' virtual address space. A file view is a copy of the entire file or portion of the file, depending on the number of bytes requested.

The process that created the file mapping object uses the MapViewOfFile function with the mapping object handle to create a file view in the process' virtual address space. The access flags must not conflict with the flags specified by CreateFileMapping.

Other processes must obtain their own handles to the same file mapping object by using the OpenFileMapping function and specifying the object's name. These processes must then create their own file views by using MapViewOfFile. If a name for the file mapping object is not specified when the object is created, the corresponding memory can be shared with other processes by inheriting or duplicating the object's handle, although this can be difficult.

MapViewOfFile returns a pointer to the file view. To ensure that multiple processes are sharing the same physical memory through their own pointers, all of the views of a file must be derived from the same file mapping object. If this is the case, the file views are said to be coherent, or identical. Coherent file views are obtained by opening a named mapping object in another process, duplicating the mapping object handle into another process, or inheriting the mapping object handle. Typically, file views will not be coherent if the file is being accessed by ReadFile or WriteFile or if the file views are derived from different mapping objects, even if the mapping objects refer to the same file.

If two or more processes have write access to the shared memory, the processes must synchronize their access to the memory by using a semaphore.

Some applications require that a file be mapped at the same address in multiple processes. To place the file view at a specific location within the process' address space, you should use the MapViewOfFileEx function and specify the desired address. The system may map the file view to an address other than the one specified, but MapViewOfFileEx returns the address which will be consistent among the sharing processes.