Thread: Structures and shared memory

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    7

    Structures and shared memory

    I'm having a little bit of a problem in an exercise of mine. It's server/client communication and i want the data to be transfered into shared memory so that the client could read them, and data to be written into shared memory as well.
    The problem is that my data are into structures. As i know, the shared memory gives you only a pointer *data
    Code:
    data = shmat(shmid, NULL, 0);
    that points at the beggining of the shared memory itself.
    how can i point the structure at the beggining of the shared memory and how can i assign a structure to write at the shared memory?
    thanks and any help would be appreciated
    Stelios

    Code:
    struct topics {
     int topicID;
     char *baseTopicName;
     struct messages *thema;
    } staticTopics[6];
    struct messages {
      char *topicName;
      char *sender;
      char *message;
    };

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I would recommend not using pointers in your shared memory data, first of all. If you need to create dynamic data structures within your shared memory, use offsets. Otherwise your application will struggle in such a case where the shared memory is mapped to different locations for different processes - this is not the common case, but it's possible.

    Other than that, you need to create a "top level" structure that contains all your data, and then have a pointer to that structure, which you assign to the value returned by shmat().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    7
    Quote Originally Posted by matsp View Post
    Other than that, you need to create a "top level" structure that contains all your data, and then have a pointer to that structure, which you assign to the value returned by shmat().

    --
    Mats
    you are referring to a structure pointer? can a common pointer point to a structure?
    if i get this right, yes i have a "top level" structure, the one that i have posted here. I initialized it and i want to put it into shared memory. I'm having trouble doing that because staticTopics=data cannot be done.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, a pointer can point to a structure. In fact, you are doing that with your "thema" pointer.

    You would however have to make a struct like this:
    Code:
    struct toplevel
    {
       struct topics staticTopics[6];
    };
    
    struct toplevel *shMemPointerToTopics;
    
    ...
    shMemPointerToTopics = data;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    7
    ok i am going to check this out, thanks matsp,you are great!

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    7
    hello. i come again with another question - problem.
    as the pointer shMemPointerToTopics points to the beggining of the struct,data points there as well. thats in the server.c. What if i want to have a struct, with the same names,with the shared memory so it can be easily accessed in the client.c. I thought, creating the exact name struct on the client.c with the same variables and create another pointer to point to data as well would work. but id doesn't seem to work.
    example: on both clients:
    Code:
    struct toplevel
    {
       struct topics staticTopics[6];
    };
    
    struct toplevel *shMemPointerToTopics;
    
    ...
    shMemPointerToTopics = data;
    on server mode:
    Code:
    staticTopics[1].topicID=10;
    on client mode
    Code:
    printf("Server topics %d",staticTopics[1].topicID);
    the printf prints 0 which means that the data doesnt change..
    any further help would be appreciated.
    Stelios

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The fragment of code saying:
    Code:
    staticTopics[1].topicID=10;
    seems incorrect - it probably should use shMemPointerToTopics in some way. Can you post a bit more of the code, particularly the chain of passing variables that lead to that point.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    7
    the shared memory:
    Code:
    key_t key; 
    int shmid,semid; 
    char *data;
    key = 123;	
    if ((shmid = shmget(key, 1024, IPC_CREAT | 0600)) == -1)
         printf("error");
    
    data = shmat(shmid, NULL, 0);
     shMemPointerToTopics = data;
    a function that is called:
    Code:
    void StaticTopics(int topNumber){
     staticTopics[topNumber].topicID=321;
     staticTopics[topNumber].baseTopicName="anorthosis";
     staticTopics[topNumber].thema[topNumber].sender="Timour Ketspagia";
     staticTopics[topNumber].thema[topNumber].message="Vradies champions league";
     }
    the part which initializes that. i am using the write for testing, if the variables were initialized right.

    Code:
    childpid = fork(); 
    if ( childpid == 0 ) {
    StaticTopics(1);
    write(connfd,staticTopics[1].thema[1].sender,15);
    }

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    staticTopics
    I presume this is a global variable - local to your program, and not the one inside the sharedmemory pointer you have assigne.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault!?!?!?
    By Viper187 in forum Windows Programming
    Replies: 57
    Last Post: 10-02-2008, 09:40 PM
  2. Shared memory in Linux: B-TREE of structures
    By zahid in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 11:15 PM