Thread: Shared memory program

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    136

    Exclamation Shared memory program

    Hi,

    I have one requirement to store an array of structure at shared memory. Also the shared memory should have one counter to store number of elements in the array.

    I tried to look at some placed but didn't find anything relevant.

    So my first question, is it possible that we can store two things on same shared memory. And second if not then how to achieve the same?

    Thanks
    S_ccess is waiting for u. Go Ahead, put u there.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Hello,

    > I tried to look at some placed but didn't find anything relevant.

    Well, if you try "<your platform> shared memory" I'm sure you can find something relevant.

    As for your constraint, yes, you can store whatever you want, but if it's easier for you, define a structure holding both a counter and an array of those sub-structures you talk about.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by maven View Post
    I have one requirement to store an array of structure at shared memory. Also the shared memory should have one counter to store number of elements in the array.
    You can write a struct which meets these requirements. Then you can share a pointer to the struct with any function that needs access to it.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    ok.

    I tried it like this. Used below structure and struct "forShm" to insert in shared memory. I am able to copy data and do all. But while reading seems it is getting some garbage. I believe i messed up something.


    Code:
    typedef struct userList
    {
      char userName[64];
      int  socketFd;
      int  gpReady;
    }listUsers;
    
    struct forShm
    {
      listUsers userInfo[10];
      int  numUsers;
      int  numGpUsers;
    };
    
                 shmid = shmget((key_t)1234, sizeof(struct forShm)*10, 0666 | IPC_CREAT);
                 sh_mem = shmat(shmid, (void *)0, 0);
                 fromShm = (struct forShm*)sh_mem;
    
                 strcpy(fromShm->userInfo[count].userName,tokName);
                 fromShm->userInfo[count].socketFd = sockfd;
                 count=count+1;
                 fromShm->numUsers = count;
    S_ccess is waiting for u. Go Ahead, put u there.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Post the part of your code that is reading and that you believe is wrong.
    Also I may misunderstand something but why sizeof(struct forShm)*10 ? you already have your 10 users in 1 forShm structure.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    @maven:
    8 years on this board, 134 posts, you should have enough experience under your belt to do a little research on your own, and read some documentation. Nevertheless, here's some help:
    Let me google that for you
    shm_overview(7) - Linux man page
    shmat(2): shared memory operations - Linux man page
    shmget(2): allocates shared memory segment - Linux man page

    Notice that all the shared memory functions return some sort of error information and set errno. Try actually checking for errors, and printing useful error messages (e.g. using perror()) if they fail. And obviously, if your shared memory functions fail, exit your program with an error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shared memory IPC Help!!!
    By liudaisuda in forum Linux Programming
    Replies: 3
    Last Post: 09-21-2011, 04:14 PM
  2. simple shared memory program on c programming
    By kattythebest in forum C Programming
    Replies: 1
    Last Post: 09-09-2009, 02:39 AM
  3. ICP shared memory
    By cavemandave in forum C Programming
    Replies: 1
    Last Post: 11-20-2007, 06:08 AM
  4. Shared Memory...
    By suzan in forum Linux Programming
    Replies: 1
    Last Post: 02-16-2006, 02:29 AM
  5. using shared memory in c
    By flawildcat in forum C Programming
    Replies: 1
    Last Post: 04-09-2002, 12:25 PM