Solved

shared memory.

Posted on 1997-12-18
8
364 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now