Can a shared memory segment be extended? - Unix

This is a discussion on Can a shared memory segment be extended? - Unix ; Hi Is it possible perhaps using the shmget() function to extend the size of a pre-existing shared memory segment? Ie is there an equivalent of a realloc() call? Or is there some other nice way a parent process could allocate ...

+ Reply to Thread
Results 1 to 7 of 7

Thread: Can a shared memory segment be extended?

  1. Can a shared memory segment be extended?

    Hi

    Is it possible perhaps using the shmget() function to extend the size
    of a pre-existing shared memory segment? Ie is there an equivalent of
    a realloc() call? Or is there some other nice way a parent process
    could allocate more shared mem which child processes could then use?

    Thanks for any help

    B2003


  2. Re: Can a shared memory segment be extended?

    "Boltar" writes:

    > Hi
    >
    > Is it possible perhaps using the shmget() function to extend the size
    > of a pre-existing shared memory segment? Ie is there an equivalent of
    > a realloc() call? Or is there some other nice way a parent process
    > could allocate more shared mem which child processes could then use?


    No, at least not reliably. Think about what would happen if one or
    more of the processes involved has mapped something else after the
    shared segment.

    --
    Måns Rullgård
    mans@mansr.com

  3. Re: Can a shared memory segment be extended?

    On Apr 1, 12:49 pm, Måns Rullgård wrote:
    > "Boltar" writes:
    > > Hi

    >
    > > Is it possible perhaps using the shmget() function to extend the size
    > > of a pre-existing shared memory segment? Ie is there an equivalent of
    > > a realloc() call? Or is there some other nice way a parent process
    > > could allocate more shared mem which child processes could then use?

    >
    > No, at least not reliably. Think about what would happen if one or
    > more of the processes involved has mapped something else after the
    > shared segment.


    Whats the unreliable way? I'd be perfectly happy with a call that may
    occasionally fail , just so long as I have the option to try. The only
    other alternatives I can see are allocation a huge segment to start
    with or have some scheme of writing the key to the next allocated
    segment at the end of the current one which the child process would
    then have to attach to. Pretty ugly IMO.

    B2003


  4. Re: Can a shared memory segment be extended?

    "Boltar" writes:

    > On Apr 1, 12:49 pm, Måns Rullgård wrote:
    >> "Boltar" writes:
    >> > Hi

    >>
    >> > Is it possible perhaps using the shmget() function to extend the size
    >> > of a pre-existing shared memory segment? Ie is there an equivalent of
    >> > a realloc() call? Or is there some other nice way a parent process
    >> > could allocate more shared mem which child processes could then use?

    >>
    >> No, at least not reliably. Think about what would happen if one or
    >> more of the processes involved has mapped something else after the
    >> shared segment.

    >
    > Whats the unreliable way? I'd be perfectly happy with a call that may
    > occasionally fail , just so long as I have the option to try. The only


    I don't know of a way, but I don't know for sure there isn't one.

    > other alternatives I can see are allocation a huge segment to start
    > with or have some scheme of writing the key to the next allocated
    > segment at the end of the current one which the child process would
    > then have to attach to. Pretty ugly IMO.


    Yes, that sounds rather cumbersome.

    --
    Måns Rullgård
    mans@mansr.com

  5. Re: Can a shared memory segment be extended?

    On Apr 1, 12:45 pm, "Boltar" wrote:

    > Is it possible perhaps using the shmget() function to extend the size
    > of a pre-existing shared memory segment? Ie is there an equivalent of
    > a realloc() call? Or is there some other nice way a parent process
    > could allocate more shared mem which child processes could then use?


    I don't know about shmget, but if you use Posix shared memory
    (eg shm_open()/mmap()) you can do it simply by mmapping
    more pages.


  6. Re: Can a shared memory segment be extended?

    On Apr 2, 9:02 pm, "Bill Pursell" wrote:
    > On Apr 1, 12:45 pm, "Boltar" wrote:
    >
    > > Is it possible perhaps using the shmget() function to extend the size
    > > of a pre-existing shared memory segment? Ie is there an equivalent of
    > > a realloc() call? Or is there some other nice way a parent process
    > > could allocate more shared mem which child processes could then use?

    >
    > I don't know about shmget, but if you use Posix shared memory
    > (eg shm_open()/mmap()) you can do it simply by mmapping
    > more pages.


    Presumably that would involve calling mmap again on the object with a
    larger length field? What flags would you use to make it extend the
    shared memory currently in use for that object rather than allocating
    a whole new region?

    B2003


  7. Re: Can a shared memory segment be extended?

    On 1 Apr, 12:45, "Boltar" wrote:
    > Is it possible perhaps using the shmget() function to extend the size
    > of a pre-existing shared memory segment? Ie is there an equivalent of
    > a realloc() call?


    mremap() for Linux.

    > Or is there some other nice way a parent process
    > could allocate more shared mem which child processes could then use?


    If you use POSIX shared memory (shm_open), it is possible for parent
    process to allocate a named shared memory segment and communicate the
    name of it to the child process(es). The child process would then open
    that shared mapping with shm_open() call.


+ Reply to Thread