Solved

shared memory.

Posted on 1997-12-18
8
370 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 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
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IdTCPClient1->Disconnect(); not working 3 93
How can i compile this github project?? 2 105
Can Live bindings change TGrid Cell Colour ? 1 64
cmake and message 1 27
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

710 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