Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

shared memory.

Posted on 1997-12-18
8
Medium Priority
?
373 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 400 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
Technology Partners: 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

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.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.
Suggested Courses

705 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