Go Back   The UNIX and Linux Forums > Top Forums > Programming
google site



Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Display Modes
  #1  
Old 03-12-2010
Registered User
 

Join Date: Jul 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Talking Shared Memory and String,Vector

How can i user string or vector ins shared memory ?

For example i have a structure sharedInfo like below

Code:
struct sharedInfo {
     string szName;
     int iAge;
     string szAddrees;
};

if i use this notation my program crashes. And if i use char szName[256], it work fine, what is wrong with string and vector in shared memoy.

Last edited by pludi; 03-12-2010 at 07:56 AM.. Reason: code tags, please...
Sponsored Links
  #2  
Old 03-14-2010
Registered User
 

Join Date: Aug 2009
Location: Munich (Germany)
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

Fire your favorite web browser, and give in your favorite search engine "stl container shared memory". You should be lucky.

The fastest way is probably to reuse the wheel others have already invented:
Chapter 9. Boost.Interprocess

Cheers,
Loïc
  #3  
Old 03-15-2010
Registered User
 

Join Date: Oct 2003
Posts: 115
Thanks: 0
Thanked 1 Time in 1 Post
Yes, it will crash because "string" (if this is C) would be a char * allocated on the heap, and if this is c++, is probably the same (but I suppose the C++ STL could implement it any way they wanted). Regardless, it's not going to be compatible with shared memory.

So...as Loic said, you'll have to either live with a predefined size for the entire shared memory segment, or use a separated shared memory management library. It seems even the shared memory management library he supplies, however, is unfortunately not going to be directly compatible with string. Or at least at first blush, it didn't appear to be. It just allows you to dynamically allocate from a shared memory segment (which still must have a fixed size).
  #4  
Old 03-17-2010
Registered User
 

Join Date: Jul 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Talking

Is there any other alternative to use STL in Shared memory ? What about memory mapped files ?
  #5  
Old 03-18-2010
Registered User
 

Join Date: Jul 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
extern "C" {
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
}

#include <iostream>
#include <vector>

using namespace std;


typedef struct sample{
        int iCount;
        string szName;
}sample;

int CreateSharedMem(key_t key) {
        int shmid=-1;
        if ((shmid = shmget(key,sizeof(sample),IPC_CREAT|0666)) < 0) {
                cout << "Unable to get value " << endl;
                perror("shmget");
                return -1;
        }
        return shmid;
}

int main() {
        pid_t pid;
        int shmid;
        key_t key;
        key = 1001;
        /* create shared memory */
        shmid = CreateSharedMem(key);

        sample *ostData;

        /* attach shared memory */
        if ( (ostData = (sample *) shmat(shmid,NULL,0)) == NULL) {
                cout << " Unable to attach to Shared region." << endl;
                exit(1);
        }

        sample *smp = new (ostData) sample();
        smp->szName = "abcxyz";
        /* doing above b'coz can't assign directly to szName */

        ostData->iCount = 0;

        if ( (pid = fork())< 0 ) {
                perror("pid");
                exit(1);
        }else if(pid == 0) { // child
                if((shmid = shmget(key,sizeof(sample),0666)) < 0) {
                        cout << "Faild to create shared segment." << endl;
                        exit(1);
                }
                sample *ostData =(sample *) shmat(shmid,NULL,0);
                cout << "---------------- child ------------------- " << endl;
                cout << "ostData->szName : " << ostData->szName <<endl;
                cout << "ostData->iCount : " << ostData->iCount <<endl;
                /* changing in child */
                ostData->szName = "xyzabc";
                ostData->iCount = 10;

                shmdt(ostData);
        }else { // parent
                int status;
                waitpid(pid,&status,WNOHANG);
                cout << "-------------- Parent ------------------- " << endl;
                cout << "ostData->szName : " << ostData->szName <<endl;
                cout << "ostData->iCount : " << ostData->iCount << endl;
                /* in parent the change of count is reflected but name doesn't */
        }
        ostData->~sample();
        shmdt(ostData);
        shmctl(shmid,IPC_RMID,NULL);
        return 0;
}

In above program i m able to assign name ins szName and able view it in child, but when i change szName in child it doesn't reflected in parent.

One thing might be because it is (szName) given space using new in parent process in shared memory that's why child process is not able to change value at that location. but still child is able to view that value then why can't it change the value at that location.

Last edited by vino; 03-19-2010 at 01:31 AM.. Reason: added code tags
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shared memory for shared library otheus Programming 0 09-03-2008 03:55 AM
Shared memory in shared library DreamWarrior Programming 12 05-30-2007 04:33 PM
memory sharing - not shared memory - elzalem Programming 9 05-02-2007 07:45 AM
Shared memory shortage but lots of unused memory cjcamaro UNIX for Advanced & Expert Users 1 10-13-2004 05:10 PM
vector<string> with insert cmd photon Programming 1 09-10-2004 05:51 PM



All times are GMT -4. The time now is 08:00 AM.