INTER PROCESS COMMUNICATION-FILE MAPPING/MEMORY MAPPED FILES
This article explains about how to create and use memory
mapped files called "File Mapping" in windows environment. File mapping can be
used to share a file or memory between different processors in windows
environment. Windows provides 32 bit API for accomplishing this task. The
advantage of File mapping is inter process communication and speeding file
access. APIs used for File mapping are CreateFile, ReadFile,
WriteFile, CreateFileMapping, MapViewOfFile,
UnmapViewOfFile, FlushViewOfFile. Example 1 explains how to write
data to File mapping and Example 2 explains how to read data from File
Mapping.
Example 1 starts with checking for command line
arguments. The first argument is File Mapping write executable name and second
argument is the File mapping file name and calls CreateFile for creating
a file. The permissions given are read and write with create always option. It
then calls CreateFileMapping to create the mapping. In this we are
passing the size of memory mapped file and name of the memory mapped file. Using
this memory mapped file object, other process can use for read/write operations.
MapViewOfFile loads the file data into the memory space of the process
and returns the new address of the data. Here we are casting the data to
MEMORYDATA structure(mdata). The mdata will be filled integer values from 0 to
MAXDATA.
Example 2 starts with checking for command line
arguments. The first argument is File Mapping read executable name and second
argument is the File mapping file name and calls CreateFile for opening
an existing file. The permissions given are read option. It then calls
CreateFileMapping to for mapping. In this we are passing the size of memory
mapped file as 0 and name of the memory mapped file. MapViewOfFile loads
the file data into the memory space of the process and returns the new address
of the data. Here we are casting the data to MEMORYDATA structure(mdata). The
values in the mdata will be printed on console.
FlushViewOfFile can be used to flush the changes to
the disk. The system will write to disk only those memory pages that actually
contain modified data.
Sample:
The following sample explains how to communicate between two
process in windows environment. One application writes data into file mapping
and another application reads data from file.
Copyright (C), All rights reserved-2008
No warranty or guarantee not limited to these are provided with above free
software. Send your comments or feedback
to
feedback@prasannatech.com. For
advertising contact
advertise@prasannatech.com