putting a structure in shared memory *Unix*

This is a discussion on putting a structure in shared memory *Unix* within the Linux Programming forums, part of the Platform Specific Boards category; How would I copy a structure into shared memory on Unix? The structure is below: Code: struct sh_weather { float ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    1

    putting a structure in shared memory *Unix*

    How would I copy a structure into shared memory on Unix? The structure is below:
    Code:
          struct sh_weather {
        
              float intemp_F; // indoor temperature in Fahrenheit
              float intemp_C; // indoor temperature in Celsius
              float outtemp_F; // Outdoor temperature in Fahrenheit
              float outtemp_C; // outdoor temp in Celsius 
              unchar w_speed; // Wind Speed in MPH
              char *wind_d; // String pointing to wind direction buffer
              float inbaro; // Inches of Mercury
              float mmbaro; // Mercury in MM
              unchar out_hum; // Outside Humidity
              unchar in_hum;  // Inside Humidity
              float t_rain;  // Total rain since weather system reset
          } share_weather;
    struct share_weather *weatherPTR ; // point to the shared weather structure



    do I use mmap()?

    also how do I typedef a pointer to the structure to update the data fields inside it

    I got a shared memory chunk with this:
    Code:
          shm_id = shmget(key, sizeof(share_data), IPC_CREAT | 0644); //  Give only Read permission to Clients
                if (shm_id < 0) {
             printf("Problem getting shared memory segment using key %d\n", key);
             exit(1);
                }
    kinda lost after that
    thanks

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Code:
    memcpy(pointer_to_shared_memory, pointer_to_struct_you_want_to_share, sizeof(struct_you_want_to_share));

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,702
    >> char *wind_d; // String pointing to wind direction buffer
    That buffer will also need to be in shared memory.

    >> kinda lost after that
    You need to call shmat() as well.
    Shared Memory Segments

    gg

  4. #4
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    I usually use

    shm_open()
    ftruncate()
    mmap()

    sequence. You propably will also need sem_open, sem_wait and sem_post to synchronize the access to shm. You propably do not want clients being reading the data at the same time you're updating it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Putting A Struct Into Shared Memory
    By Lockout in forum C Programming
    Replies: 1
    Last Post: 02-28-2011, 03:05 AM
  2. Putting a set of structs in memory...how?
    By raindog308 in forum C Programming
    Replies: 3
    Last Post: 02-23-2011, 03:44 PM
  3. Shared Memory...
    By suzan in forum Linux Programming
    Replies: 1
    Last Post: 02-16-2006, 02:29 AM
  4. Shared Memory
    By wardej2 in forum Linux Programming
    Replies: 8
    Last Post: 10-21-2005, 08:48 AM
  5. Putting a structure into a file
    By Unregistered in forum C++ Programming
    Replies: 10
    Last Post: 06-25-2002, 05:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21