Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

shared memory.

I have a Shared Memory class that will work under solaris, and windows via conditional compilation. solaris provide a function called shmget() which provides info on the shm block I am about to create, namely the number of users attached. I use this to decide whether to memset the memory to zero. Does anyone know a way to see if a handle already exists, or if the file I create already exists. The code I use for WIN32 is...


bool SM::open(unsigned long ident, unsigned long size)
{
      char ident_[255];
      sprintf(ident_, "SMID:%ld__%ld", ident, size);

      handle_ = CreateFileMapping((HANDLE)0xFFFFFFFF,
                        NULL, PAGE_READWRITE,
                        0, size, ident_);

// maybe check if handle_ exists or file existed here.??

      if(handle_ == NULL)
      {
         return false;
      }

      map_ = MapViewOfFile(handle_, FILE_MAP_READ |
                              FILE_MAP_WRITE, 0, 0, 0);

      open_ = map_ != NULL;
      if(open_)
      {
         init_ident = ident;
         init_size = size;
         cout << "Attached to shared memory" << endl;
         // only memset if I created memory.
         memset(map_, 0, init_size);
         return true;
      }
   return false;
}


Thanks for any help.
Nigel.
0
nigel5
Asked:
nigel5
  • 6
  • 2
1 Solution
 
nietodCommented:
From the windows SDK documentation.

 File-mapping objects can be shared either through process creation, through handle duplication, or by name
0
 
nietodCommented:
For your case, you will probably want to do it by name.  That is, look for a particular file name.

At the moment, you are not naming your sharred memory file, you will probably have to change that.

If that is not what you need.  What is the relationship between the different processes that share the memory?  i.e. Are they started independantly.  Does one start another, etc.
0
 
nigel5Author Commented:
So then, I guess that a page file is created in a directory somewhere, when you call CreateFileMapping.?? I thought I was doing that with the ident_ argument??

The relationship between process is primarily that of control. A simple form of TCP/IP port protocol that runs on the same machine.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nietodCommented:
Yes, you are specifying a name.  I was looking back at the wrong call when I made that comment.

More research seems to show that I'm not doing well today.  I thought you could specify a path in the name.  You cannot.  That means you might not be able to find the file.

However, if the mapping already existed.  GetLastError will return ERROR_ALREADY_EXISTS.  You can uuse that to determine if you need to initialize.
0
 
nietodCommented:
Note that when the mapping already exists, GetLastError() will indicate an error occured, however, the CreateFileMapping will still return a valid handle, i.e., it is not a REAL error.  
0
 
nigel5Author Commented:
Is the handle created a system wide, or process wide handle?

I will give it a go over the weekend when I have a full development workstation to play with.

0
 
nietodCommented:
No handles (of any type) are system wide.  In general handles (all types) are process specific, however, it is possible to create handles that are inherited by a child processes.  Since you are not dealing with child processes (Right?) this doesn't matter to you.  Your processes won't share the handles themselfs, but will create/obtain their own handles using CreateFileMapping and an agreed upon file name.  (The other option for this stuff, is to use no name, which means no one can access the mapped file without a handle, then you pass the handle to a child process.  However, this only works with parent/child processes.  For all other cases you must name the mapping.)
0
 
nietodCommented:
I'm going away tomorrow, the 23, and will be unavailable.  If you need more help after that you will have to reject my answer.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now