mmap and shared memory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matias Surdi

    mmap and shared memory

    Suppose I've a process P1, which generates itself a lot of data , for
    example 2Mb.
    Then, I've a process P2 which must access P1 shared memory and,
    probably, modify this data.
    To accomplish this, I've been digging around python's mmap module, but I
    can't figure how to use it without files.

    Could anybody explain me how could this be accomplished? An example will
    be very appreciated. Thanks a lot for your help.

  • greg

    #2
    Re: mmap and shared memory

    Carl Banks wrote:
    In C you can use the mmap call to request a specific physical location
    in memory (whence I presume two different processes can mmap anonymous
    memory block in the same location)
    Um, no, it lets you specify the *virtual* address in the process's
    address space at which the object you specify is to be mapped.

    As far as I know, the only way two unrelated processes can share
    memory via mmap is by mapping a file. An anonymous block is known
    only to the process that creates it -- being anonymous, there's
    no way for another process to refer to it.

    However, if one process is forked from the other, the parent
    can mmap an anonymous block and the child will inherit that
    mapping.

    (I suppose if both processes had sufficient privileges they
    could map physical memory out of /dev/mem, but that would be
    *really* dangerous!)

    --
    Greg

    Comment

    • Jeff Schwab

      #3
      Re: mmap and shared memory

      greg wrote:
      Carl Banks wrote:
      >In C you can use the mmap call to request a specific physical location
      >in memory (whence I presume two different processes can mmap anonymous
      >memory block in the same location)
      >
      Um, no, it lets you specify the *virtual* address in the process's
      address space at which the object you specify is to be mapped.
      >
      As far as I know, the only way two unrelated processes can share
      memory via mmap is by mapping a file. An anonymous block is known
      only to the process that creates it -- being anonymous, there's
      no way for another process to refer to it.
      On POSIX systems, you can create a shared memory object without a file
      using shm_open. The function returns a file descriptor.
      However, if one process is forked from the other, the parent
      can mmap an anonymous block and the child will inherit that
      mapping.
      >
      (I suppose if both processes had sufficient privileges they
      could map physical memory out of /dev/mem, but that would be
      *really* dangerous!)
      >
      --
      Greg

      Comment

      • Nikita the Spider

        #4
        Re: mmap and shared memory

        In article <Ko2dncp9M48X4S _anZ2dnUVZ_ryqn Z2d@comcast.com >,
        Jeff Schwab <jeff@schwabcen ter.comwrote:
        greg wrote:
        Carl Banks wrote:
        In C you can use the mmap call to request a specific physical location
        in memory (whence I presume two different processes can mmap anonymous
        memory block in the same location)
        Um, no, it lets you specify the *virtual* address in the process's
        address space at which the object you specify is to be mapped.

        As far as I know, the only way two unrelated processes can share
        memory via mmap is by mapping a file. An anonymous block is known
        only to the process that creates it -- being anonymous, there's
        no way for another process to refer to it.
        >
        On POSIX systems, you can create a shared memory object without a file
        using shm_open. The function returns a file descriptor.
        Sorry I missed the OP, but you might be interested in this shared memory
        module for Python:


        --
        Philip

        Whole-site HTML validation, link checking and more

        Comment

        • Jeff Schwab

          #5
          Re: mmap and shared memory

          Nikita the Spider wrote:
          In article <Ko2dncp9M48X4S _anZ2dnUVZ_ryqn Z2d@comcast.com >,
          Jeff Schwab <jeff@schwabcen ter.comwrote:
          >
          >greg wrote:
          >>Carl Banks wrote:
          >>>In C you can use the mmap call to request a specific physical location
          >>>in memory (whence I presume two different processes can mmap anonymous
          >>>memory block in the same location)
          >>Um, no, it lets you specify the *virtual* address in the process's
          >>address space at which the object you specify is to be mapped.
          >>>
          >>As far as I know, the only way two unrelated processes can share
          >>memory via mmap is by mapping a file. An anonymous block is known
          >>only to the process that creates it -- being anonymous, there's
          >>no way for another process to refer to it.
          >On POSIX systems, you can create a shared memory object without a file
          >using shm_open. The function returns a file descriptor.
          >
          Sorry I missed the OP, but you might be interested in this shared memory
          module for Python:
          http://NikitaTheSpider.com/python/shm/

          Thanks; I just downloaded it. It seems to be missing the INSTALL file;
          any idea where I could find that, or should I write to the author?

          Comment

          • Nikita the Spider

            #6
            Re: mmap and shared memory

            In article <C5ydnQgAo_Q8Ii 7anZ2dnUVZ_uCin Z2d@comcast.com >,
            Jeff Schwab <jeff@schwabcen ter.comwrote:
            Nikita the Spider wrote:
            In article <Ko2dncp9M48X4S _anZ2dnUVZ_ryqn Z2d@comcast.com >,
            Jeff Schwab <jeff@schwabcen ter.comwrote:
            greg wrote:
            >Carl Banks wrote:
            >>In C you can use the mmap call to request a specific physical location
            >>in memory (whence I presume two different processes can mmap anonymous
            >>memory block in the same location)
            >Um, no, it lets you specify the *virtual* address in the process's
            >address space at which the object you specify is to be mapped.
            >>
            >As far as I know, the only way two unrelated processes can share
            >memory via mmap is by mapping a file. An anonymous block is known
            >only to the process that creates it -- being anonymous, there's
            >no way for another process to refer to it.
            On POSIX systems, you can create a shared memory object without a file
            using shm_open. The function returns a file descriptor.
            Sorry I missed the OP, but you might be interested in this shared memory
            module for Python:
            http://NikitaTheSpider.com/python/shm/
            >
            >
            Thanks; I just downloaded it. It seems to be missing the INSTALL file;
            any idea where I could find that, or should I write to the author?
            The main author has been AWOL for some years now. I'm the current
            maintainer. Sorry about the missing INSTALL file. Looks like I made
            reference to it in the README but never created it. It installs with the
            normal setup.py:

            sudo python setup.py

            I'll put out an updated package with an INSTALL file one of these days.
            Thanks for pointing that out.

            Cheers

            --
            Philip

            Whole-site HTML validation, link checking and more

            Comment

            Working...