1753732 Members
4475 Online
108799 Solutions
New Discussion юеВ

Re: shared memory

 
SOLVED
Go to solution
unix_starter
Occasional Contributor

shared memory

hello all,
i have problem with shared memory.
we have to create shared memory of about more than 700mb. and it looks like when creating it with shmget() command it is created as a contiguous block.( which i am not sure).

so my question is when we create shared memory will it be created as contiguous. if it is there a way i can make to create non contiguous for the same key value.
or is there a way i can create shared memory dynamically for the same key.

please give your thoughts
thanks in advance.
3 REPLIES 3
Don Morris_1
Honored Contributor

Re: shared memory

When you call shmget() it will create a shared memory virtual object. Such an object is assigned a virtual address range (the start of which is returned via shmat() calls).

Being a single virtual object -- um... it rather *must* be virtually contiguous. (How precisely would you find the other parts of the segment? There is no "shmat_again()" interface or anything like it). If what you want is a shared memory segment using holes in your virtual address space but created in a single call -- there is no such thing and frankly, I wouldn't expect any enhancement request for such an interface to be fulfilled. (If you really want this kind of thing -- just use MAP_SHARED mmap() with either MAP_ANONYMOUS or a temporary file or something).

There is zero guarantee that the physical memory when assigned to the object is contiguous or not.

There is zero guarantee (barring use of MPAS, but then only for the Private virtual address assigned on shmat() calls) that subsequent objects created via other shmget() [or mmap(), etc.] calls are virtually contiguous.
unix_starter
Occasional Contributor

Re: shared memory

hello thanks for info.
i have one clarification though, so when i create with mmap then it will create the memory not in one contiguous block right ..?

and can we share this block of memory created with mmap be shared among process.

thanks
Don Morris_1
Honored Contributor
Solution

Re: shared memory

No, each call to mmap() will be a contiguous virtual object. The difference is that you can make multiple mmap() calls to share a single underlying file between processes (so the data can live beyond a single process and the mapped ranges don't have to be contiguous even though the data is).

MAP_SHARED|MAP_FILE will mmap a file range for a given offset and size, use that within each process.

If you don't want to use file backing -- MAP_ANONYMOUS will use RAM to back the mmap -- but these can only be shared among a process and its descendants, and will not live beyond the last mmap'er [so if all the processes quit -- any data is lost. Fine if that's your intention, but worth pointing out].

man 2 mmap for full details -- there's quite simply a lot of details to cover that it would be pointless to repeat here.