Thread: Shared memory woes

  1. #1
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Shared memory woes

    I'm searched the Web and this board, and I've read the man pages, and I've read two books that touch on this subject. I am getting a tad fustrated.

    The man pages say it's best to pass in NULL for the second argument of shmat(), but if I do that, the pointer that's returned to me could be *anywhere* in the segment of shared memory. How could I find the beginning of the segment with only a pointer to some random spot in the middle?

    Conversely, I could pass in the memory address to shmat() that was returned to me from the first time I attached shared memory. This had been working well for me for a while until I tried attaching to two seperate segments of shared memory that were created by two seperate processes. I received the EINVAL error. I don't understand what's invalid about what I'm trying to do. It used to work. Could it have something to do with the fact that both segments seem to have the same address? They have different IDs.

    I feel like I'm missing something because every example I've looked at passes in NULL for the address.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    If shmaddr isn't NULL and SHM_RND is asserted in shmflg,
    the attach occurs at the address equal to shmaddr rounded
    down to the nearest multiple of SHMLBA. Otherwise shmaddr
    must be a page-aligned address at which the attach occurs.
    Are you meeting the bolded requirement? If you're passing a non-page-aligned address then that could be the problem. You can try using the SHM_RND flag if you don't want to bother with making sure the address is page-aligned. It's hard to say what's wrong without seeing your code.
    If you understand what you're doing, you're not learning anything.

  3. #3
    .
    Join Date
    Nov 2003
    Posts
    307
    Hmm. The VERY FIRST call to shmat() must use NULL. The first call actually creates the segment, so the address is not defined prior to the call.

    From my man page :
    The segment is attached for reading if (shmflg & SHM_RDONLY) is
    "true"; otherwise, it is attached for reading and writing. It is not
    possible to attach a segment for write only.

    If the shared memory segment has never been attached to by any process
    prior to the current shmat() call, shmaddr must be specified as zero
    and the segment is attached at a location selected by the operating
    system. That location is identical in all processes accessing that
    shared memory object. Once the operating system selects a location
    for a shared memory segment, the same location will be used across any
    subsequent shmat() and shmdt() calls on the segment until it is
    removed by the IPC_RMID operation of shmctl().

    If this is not the first shmat() call on the shared memory segment
    throughout the system, shmaddr must either be zero or contain a
    nonzero address that is identical to the one returned from previous
    shmat() calls for that segment. Even if no processes are currently
    attached to the segment, as long as the segment has been attached
    before, the same rule applies.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Beej
    any help?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I've read Beej's work and the man pages. I was sending NULL during the first attachment, and the address I was sending the second time was page-aligned.

    It turns out that it works properly when sending in NULL for the address when attaching the second time. That it works is good enough for my employer, but I don't understand how it works. How is it that two pointers with different values point to the same segment of shared memory?
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

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. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. RE: client/server shared memory problem
    By hampycalc in forum C Programming
    Replies: 0
    Last Post: 03-10-2006, 02:26 PM
  4. shared memory can not read value over 255
    By jbsloan in forum C Programming
    Replies: 4
    Last Post: 04-03-2005, 11:56 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