473,408 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Shared memory for communicating between two programs

151 100+
Hi,

I am working on shared memory to write the map by one program and using the same map by other program. Is it feasible to do that with shared memory. Is there any source code or documentation doing the same.i will be really thankful as i am in great need.
Jul 31 '09 #1
8 7774
JosAH
11,448 Expert 8TB
@Man4ish
It depends on what you want to do exactly and which implementation of the shared memory package you're using. Most implementations have some form of locking and/or event dispatching available, but they're all different. Google is your friend here.

kind regards,

Jos
Jul 31 '09 #2
Banfa
9,065 Expert Mod 8TB
My experience using shared memory to communicate has been using dual port RAM to communicate between separate processors. Dual port RAM actually has 2 address and 2 data buses so both processors can connect simultaneously to any memory location.

As with all shared memory this has the problem of making sure you don't read while the other processor is writing. However unlike a software implementation on a single processor there is no locking mechanism between 2 processors (on the whole).

We got round that but implementing a strict state machine that ran on both sides of the RAM, a few memory locations were reserved as control bytes and using these it was possible to implement a locking mechanism that allowed effective communication through the RAM.

The whole point of this storyu is that if it turns out you have no effective locking/event dispatch available in the software solution you find you could always do the same thing.
Jul 31 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
This is usually done by using separate processes.

The process with the map is the server process and the processes that use the map are the client processes.

These processes may be separate programs or subprocesses created by a program.
Jul 31 '09 #4
Man4ish
151 100+
I searched in google in as suggested........

Expand|Select|Wrap|Line Numbers
  1.     char c;
  2.     int shmid;
  3.     key_t key;
  4.     char *shm, *s;
  5.  
  6.     /*
  7.      * We'll name our shared memory segment
  8.      * "123321".
  9.      */
  10.     key = 123321;
  11.  
  12.     /*
  13.      * Create the segment.
  14.      */
  15.     if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
  16.         perror("shmget");
  17.         exit(1);
  18.     }
  19.  
  20.     /*
  21.      * Now we attach the segment to our data space.
  22.      */
  23.     if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
  24.         perror("shmat");
  25.         exit(1);
  26.     }
  27.  
  28.     /*
  29.      * Now put some things into the memory for the
  30.      * other process to read.
  31.      */
  32.   // map <string, string>
  33.     s = shm;
  34.     strcpy(s,"Bytes  ....");
  35.  
  36. It is working fine for client process and if i print the value of string child process, I am able to print the mesage "Bytes ..... ". But now i am moving step ahead to store the values in map and print the value in child process.
  37. map<int, string> shm;
  38.  
  39.  if ((shm =  shmat(shmid, NULL, 0)) == (char *) -1) {
  40.         perror("shmat");
  41.         exit(1);
  42.     }
  43.  
  44. no match for ‘operator=’ in ‘shm = shmat(shmid, 0u, 0)’
  45. /usr/lib/gcc/ia64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_map.h:225: note: candidates are: std::map<_Key, _Tp, _Compare, _Alloc>& std::map<_Key, _Tp, _Compare, _Alloc>::operator=(const std::map<_Key, _Tp, _Compare, _Alloc>&) [with _Key = int, _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >]
  46.  
  47. map <int, string> mapregion;
  48.     mapregion = shm;
  49.     mapregion[1] = "My name is manish";
I think map is not allowed , Then how map can be implemented for such process.Thanks in advance.
Aug 3 '09 #5
newb16
687 512MB
You can't assign string ( or char* ) to map - you ned to insert it like
shm[whatever] = shmat(shmid, NULL, 0);
like you do it below with mapregion[1]
Aug 3 '09 #6
Man4ish
151 100+
@newb16
still map is not stored in memory, it will generate a map but it will not be accessible by other process(program).
Aug 3 '09 #7
newb16
687 512MB
You can't put class in the shared memory - it can create some internal structures and reference them by pointers, and you can't force it to place them in the shared memory. Even if you write custom allocator, values of pointers to the shared memory will be different for the different processes that share it.
Aug 3 '09 #8
weaknessforcats
9,208 Expert Mod 8TB
When you use shared processes, each process has its own memory space so that pointers and such ar valid only in the process that creates them.

Therefore: Do not try to directly access structures in a process from another process.

Instead, you pass a query to your process with the map. That process does the lookup and passes back the results of the query. (called marshaling).

If you were using Windows, there's a nice section onhow to do this is Jeffrey Richter's Windows via C/C++.
Aug 3 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Srijit Kumar Bhadra | last post by:
Hello, Here is some sample code with pywin32 build 203 and ctypes 0.9.6. Best regards, /Srijit File: SharedMemCreate_Mutex_win32all.py # This application should be used with...
11
by: Michael Schuler | last post by:
The use of STL in shared memory poses a real problem since (non-smart) pointers are not allowed there. Is there any solution for containers in shared memory using smart pointers? Where can I...
1
by: myren, lord | last post by:
When I first discovered shared memory (between multiple processes) I immediately started thinking of how to build my own VM subsystem + locking mechanisms for a large single block of memory. This...
16
by: Jacob | last post by:
It is common practice (I've heard) to always catch allocation exceptions from "new". How is done in practice? What would be a common procedure (path of sequence) after such an event has occured?...
2
by: santa19992000 | last post by:
Confusing th eword with "library", "shared library" and how to use these things in real C project, is there any small example I can take a look. Thanks.
6
by: Bart Simpson | last post by:
I want to create a data repository in memory so that different processes can share the same data, rather than loading the data multiple times for each process and then having to contend with...
4
by: Rick | last post by:
Hi guys!! I'm asking if is possible to create 2 different programs and at the running each one, can access to shared memory and modify and read data in it? (not at the same time but can see data...
3
by: victor.s.email | last post by:
Hi, Im a Java developer new to PHP development. Im tasked to do some development playing with Shared Memory (shm http://forums.devshed.com/php-development-5/). Im a little curious about the...
2
by: gaurav kashyap | last post by:
Dear all, I have a server program that listens to a particular port and a number of client programs that connect to the server. Now i want to put some data in form of python list in main...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.