Solved

shared memory.

Posted on 1997-12-18
8
369 Views
Last Modified: 2008-02-01
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
Comment
Question by:nigel5
  • 6
  • 2
8 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 1176646
From the windows SDK documentation.

 File-mapping objects can be shared either through process creation, through handle duplication, or by name
0
 
LVL 22

Expert Comment

by:nietod
ID: 1176647
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
 

Author Comment

by:nigel5
ID: 1176648
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!

 
LVL 22

Expert Comment

by:nietod
ID: 1176649
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
 
LVL 22

Expert Comment

by:nietod
ID: 1176650
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
 

Author Comment

by:nigel5
ID: 1176651
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
 
LVL 22

Expert Comment

by:nietod
ID: 1176652
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
 
LVL 22

Expert Comment

by:nietod
ID: 1176653
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question