Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shared memory using plain C in Windows 2

Status
Not open for further replies.

Nosferatu

Programmer
Jun 9, 2000
412
0
0
RO
Is there any way to make use of shared memory in Windows, using C functions? I am looking for some equivalents of the Unixes shm_xxxx functions...

Thanks. [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
this should help

here is an excerpt

Sharing Files and Memory

File mapping can be used to share a file or memory between two or more processes. To share a file or memory, all of the processes must use the name or the handle of the same file mapping object.


-pete
 
Yep... thanks Pete.
I think this is at the base of the CSharedFile of the MFC.
Good. [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
You use mmapped files and file locks with a naming scheme
basically. Semaphores would be the logical way to approach
the locks.

Here is a library for win32:

Ubnfortunately it still uses win32 specific
functions so I don't know if it's suitable for
what you want.
 
Hey marsd, thanks for the link.
It's very helpful as I already got on the way with what Pete proposed.
Indeed, I was looking for POSIX functions for shared memory but MS seems to be stubborn enough not to make any accountance of them, so in the end I might be needed to write my own wrappers for shared memory to ensure code portability.

Cheers,
--Razvan [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Warning about windows shared memory: it is noy like unix in that you cant just write to it and everyone will see it. it has to be locked everytime you access it. If you dont unlock it no-one else has access to it. I found this extremely painful when I first went from Unix to Windows
 
Thanks Xwb. Actually, as I am working mostly on Windows, I am sure I'll find huge pain when going to port the system library on Unix.
This thread shared memory with its locks needed seems to be easy, comparing to shared memory on Unix.
But doing the full-shared memory with file maps will help in the process, as they share some similarities with Unix memory sharing, as far as I can tell without actually working on them.

I need this for a quite complex tree structure (a file tree) and worst of all, I need to make the code portable.
So having the code work with windows threads won't do any good when going to Unix.
Thus, I go for the memory file-mapping... God help me as I haven't started yet!
[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
If you have a tree structure, make sure you use indices: not addresses. It is basically back to what programming was before pointers were invented: everything was based on indices. It is a very common mistake to use pointers and then suddenly find they don't work across shared memory.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top