Thread: Shared Memory...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    1

    Shared Memory...

    Hello people,

    I am writing a program in C that is supposed to read from a shared memory segment and print the data on the screen. The shared memory segment is organized as an array of 23 objects, where an object is a complex datatype defined in a separate .h file.

    Now, a couple things am not sure of...the SIZE parameter of shmget is based on what? I know it's the size of the mem segment in bytes, but in my program, how can I figure that out?

    Here's my code:
    ****************************************
    Code:
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/shm.h>
    #include <stdio.h>
    
    int main()
    {
            int  shmid;
            int  m_key;
            char *data;
            int  size;
    
            m_key = 123456789;
            size  = 23*sizeof(int);
            shmid = shmget(m_key, size, 0444);
            data  = shmat(shmid, NULL, SHM_RDONLY);
            printf("Segment contains: \n%s\n", data);
            if (shmdt(data) == -1)
            {
                    perror("shmdt");
                    exit(1);
            }
            return 0;
    }
    ****************************************
    This code compiles, yet it only outputs a string out and exits. I initially had data[0],data[1], etc to be outputted...yet, that resulted in a segmentation fault!

    Please, I'd appreciate any comment on how to go about this. I read several documents on shared memory, and it's not supposed to be hard at all, but I guess I am having a hard time implementing that.

    Thank you!

  2. #2
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    If you're not being forced you should use mmap and shm_open instead of that sysv kludge (you'll probably find it easier as well).

    To start, if you haven't already done so, make sure you check the return of shmget, if it's an error then read errno so you know what went wrong-- make sure you check the return of shmat as well. Knowing if something is going wrong at either of those points will greatly ease your debugging.

    Also what filled data?
    What's the context of this application?
    Last edited by valis; 02-16-2006 at 02:41 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Partly shared memory
    By DrSnuggles in forum C++ Programming
    Replies: 13
    Last Post: 01-21-2009, 03:35 AM
  2. Shared Memory semaphores?
    By Ironic in forum C Programming
    Replies: 0
    Last Post: 10-31-2008, 07:13 PM
  3. Shared memory implementation using thread
    By kumars in forum C Programming
    Replies: 5
    Last Post: 06-18-2008, 04:24 AM
  4. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 04:44 AM
  5. Shared memory in Linux: B-TREE of structures
    By zahid in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 11:15 PM