News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

File sharing by mapping

Started by Danesh, June 16, 2005, 05:03:10 AM

Previous topic - Next topic

Danesh

Hi all,

I want to map a file into memory and then get handles as many as I want form mapped file which points to the address of memory which the file has been mapped and use these handles to access the mapped file from other processes to just read.
I just want that the memory which has been used for creating veiw of a mapped file just be allocted once and then only get handles to same memory without allocating new memory. I don't know whats diffrent between CreateFileMapping and OpenFileMapping since both return a handle from a mapped file. Also, I want to know does the function MapViewOfFile which creates view of a mapped file in memory, allocate memory or just returns the handle of a mapped file and memory has been allocated already by CreateFileMapping and/or OpenFileMapping ?

Thanks,

Danesh


Tedd

To map a file, you need to..

hFile = CreateFile ...GENERIC_READ,FILE_SHARE_READ,.........
hMap = CreateFileMapping, hFile,..,PAGE_READONLY,.....pName
pMem = MapViewOfFile, hMap,FILE_MAP_READ,....


pMem will then be the address of the file in memory.
You'll probably also want to GetFileSize on hFile, so you don't try reading past the end of it (which will cause an exception at some point)
Depending on what you need, you can change the READ access/share/map attributes and give write access - check win32.hlp for details :wink

And to close and clean up after yourself..

UnmapViewOfFile, pMem
CloseHandle, hMap
CloseHandle, hFile



Each process will open the file in the same way (and close afterwards!) The way you get the sharing is by giving the file-mapping a name (last param in CreateFileMapping) - which all processes use to refer to the file mapping; meaning only the first one actually maps the file into memory, and the rest get access to that.
No snowflake in an avalanche feels responsible.

Danesh

Thanks Ted,

Ok, now how another process can use the file which has been mapped before by using the name you mentioned ? Should it use again OpenFileMapping or its just enough to use the pMem address ?
Another question is, can two processes read same bytes of a mapped at the same time ? I mean does OS manage it or it should be done by prorammer by using a queue or flag or whatever ?

Thanks,

Danesh


chep

Quote from: Danesh on June 16, 2005, 01:21:43 PM
Ok, now how another process can use the file which has been mapped before by using the name you mentioned ? Should it use again OpenFileMapping or its just enough to use the pMem address ?

The other process must use OpenFileMapping.


Quote from: Danesh on June 16, 2005, 01:21:43 PM
Another question is, can two processes read same bytes of a mapped at the same time ? I mean does OS manage it or it should be done by prorammer by using a queue or flag or whatever ?

While you only read, there is no concurrency problem (nothing gets modified, so nothing can get corrupted).
You'll need inter process synchronization (eg. Mutexes) from the moment at least one process writes to the file, to ensure the writing process has exclusive access to the part of the file he is modifying.

doomsday

Quote from: DaneshOk, now how another process can use the file which has been mapped before by using the name you mentioned ?

DuplicateHandle may come in handy, if you want to go down that path.

regards,
-Brent

Tedd

Quote from: Danesh
Ok, now how another process can use the file which has been mapped before by using the name you mentioned ? Should it use again OpenFileMapping or its just enough to use the pMem address ?

As I said, you just open it in the same way, using the same name - windows will take care of the 'sharing.'


Quote from: Danesh
Another question is, can two processes read same bytes of a mapped at the same time ? I mean does OS manage it or it should be done by prorammer by using a queue or flag or whatever ?

Reading obviously isn't a problem. Writing can cause problems, so you may have to use a (named!) mutex which each process sychronizes on. Or, you can ignore it and I think the effect you get is that the last process to close the file get's the final say.
No snowflake in an avalanche feels responsible.