Tech Support > Microsoft Windows > Development Resources > shared memory creation on Windows vs Unix
shared memory creation on Windows vs Unix
Posted by umuta@us.ibm.com on May 10th, 2005


Hi -
On Unix if a process creates a shared memory segment with shmget()
and exits , the created segment is still valid unless removed by
shmctl().
Is is true that on Windows if the process which created the shared
memory segments with CreateFileMapping() exits, the segments will be
deleted as well?

thanks,
Umut

Posted by Alex Blekhman on May 10th, 2005


umuta@us.ibm.com wrote:
It will be deleted if no other process is referencing file
mapping. CreateFileMapping function creates kernel object.
Kernel objects are reference counted. When reference count
drops to zero, then kernel releases the object.



Posted by umuta@us.ibm.com on May 18th, 2005


Thanks Alex for your reply.
I am wondering if there is any way to make it behave like unix. I am
trying to port an application from Unix to Windows and this is what is
currently being done: process A creates the shared memory segments and
exits; then process B attaches the created segments, does what it needs
to do and removes the segments and exits.

Any suggestions?

thanks,
Umut

Posted by Scott McPhillips [MVP] on May 18th, 2005


umuta@us.ibm.com wrote:

Only with a file. Process B can map a view of the file.

--
Scott McPhillips [VC++ MVP]


Posted by umuta@us.ibm.com on May 19th, 2005


But once the process A exits and the segments are removed, how will
process B find the segments to attach?

Posted by Jason S on May 19th, 2005


umuta@us.ibm.com wrote:
you need to name the file mapping to something with a unique id that is
known to processes A and B.


Posted by umuta@us.ibm.com on May 20th, 2005


Ok , here is a simple program:
a.c:
-----
#include <windows.h>
main()
{
int size 4096 ;
int hmap;
void * lvpmap;

hmap =
CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_R EADWRITE,0,size,"MM1");
printf("hmap = %d\n",hmap);
lvpmap = MapViewOfFile(hmap,FILE_MAP_WRITE,0,0,0);
}

b.c
----
#include <windows.h>
main()
{
int hmap;

hmap = OpenFileMapping(FILE_MAP_READ,TRUE,"MM1");
printf("hmap = %d\n",hmap);
}

compile a.c and b.c
run a ----> prints the value of hmap
run b ---> prints 0

If I put the program "a" in a loop then program "b" prints the same
hmap value as "a". I wondering if there is a way to get the same
behaviour without putting "a" in a loop.

Posted by umuta@us.ibm.com on May 20th, 2005


Oh now I got it. You mean a regular file instead of a kernel object,
right?

Posted by Scott McPhillips [MVP] on May 20th, 2005


umuta@us.ibm.com wrote:
You are mapping to the page file. If program "a" exits before "b"
starts then this won't work. The mapping is destroyed when "a" exits
unless "b" also has it open. I think that was your original problem,
and you haven't really changed your approach yet.

To make the data survive "a" exiting before "b" starts you have to put
the data in your own named file. Both "a" and "b" must know the file
name. You can use ordinary file read/write functions or you can use
CreateFileMapping/MapViewOfFile in both programs. But either way, you
have to have a file if you want the data to persist between programs.

--
Scott McPhillips [VC++ MVP]


Posted by Sten Westerback on May 20th, 2005



<umuta@us.ibm.com> wrote in message
news:1116546594.686870.60410@f14g2000cwb.googlegro ups.com...
All files are kernel objects (even if for instance CRT wraps it's own
routines
around CreateFile(), WriteFile() etc) ... so i can't see what you mean

Persistent data just can't exist in memory without a process. In teory there
are
some always available processes that you could use. The first that comes
into
my mind is the Clipboard but it's use should be controlled directly by a
user.

Anyway, you have to store the data somewhere -- either a file or maybe in
Registry....

- Sten
MVP SDK





Posted by umuta@us.ibm.com on May 23rd, 2005


I have changed the application to use
h1= CreateFile();
h2= CreateFileMapping(h1,.....);


No my question is this:If I have access to only the handle from
CreateFileMapping() ,h2, is there a way to get h1 so I can call
CloseHandle(h1); ?

thanks,
Umut

Posted by Olof Lagerkvist on May 24th, 2005


umuta@us.ibm.com wrote:

It is generally better to close the handle returned from CreateFile()
immediately after the call to CreateFileMapping().

--
Olof Lagerkvist
ICQ: 724451
Web: http://here.is/olof



Similar Posts