[LCP] mapping memory

Mark Farnell mark.farnell at gmail.com
Tue Nov 4 14:51:01 EST 2008


One more question:

Suppose if my process calls shm_open() to create a shared-memory
object (with O_CREAT | O_EXCL) and its threads make some mmap
mappings, then my process crashes (i.e. no opportunity to call
shm_unlink()).  Suppose if this object is *not* shared (or opened) by
other processes, then will the crash of my process trigger the shared
object to be deleted (i.e. shared memory freed).  Otherwise this would
result in a big memory leak that can be resolved only by rebooting!

Thanks!

Mark

On Tue, Nov 4, 2008 at 3:59 PM, Mark Farnell <mark.farnell at gmail.com> wrote:
> I see,
>
> So you mean that I let *each thread* to open the shared memory object,
> the each thread can use the FD given to make its own memory map and
> make mprotect() calls?
>
> Am I correct?  If so, then that's great!  Thanks a lot!
>
> Cheers,
>
> Mark
>
> On Tue, Nov 4, 2008 at 3:03 PM, Kim Hawtin <kim.hawtin at adelaide.edu.au> wrote:
>> Hi Mark,
>>
>> Mark Farnell wrote:
>>> I mean different logical address.
>>>
>>> In a process, suppose I malloc a piece a memory
>>>
>>> #define SIZE 1024
>>>
>>> void *a = malloc(SIZE);
>>>
>>> Are there any functions which produces a logical mapping of the chunk
>>> I've malloced on a *different* address space (but in fact point to the
>>> same chunk)
>>>
>>> void *b = map(a, size);
>>>
>>> so that b is the base address of a different block (address looks
>>> different to a), yet if a thread changes the content pointed to b, it
>>> actually maps to the block pointed to by a.
>>>
>>> The application is that I have a process which spawns multiple POSIX
>>> threads.  The process malloc's a piece of memory.  Then what I intend
>>> to do is that for each thread, map that piece of memory to a different
>>> address space, each mapping can have individual mprotect operations
>>> that does not affect other mappings owned by other threads.
>>>
>>> However if a mapping is written to, it actually writes to the piece of
>>> memory malloc'ed by the master.
>>>
>>> So memcpy etc will *not* do the job.  I am not asking how to copy contents.
>>>
>>> Can this be done?
>>
>> inter-process stuff may be better covered by POSIX shared memory.
>> eg; http://linux.about.com/library/cmd/blcmdl3_shm_open.htm
>>
>> alternatively, mmap might be useful, depends on your application;
>> eg; http://linux.about.com/library/cmd/blcmdl2_mmap.htm
>>
>> otherwise start here =0
>> http://www.google.com.au/search?hl=en&q=posix+shared+memory+linux
>>
>> regards,
>>
>> Kim
>> --
>> Operating Systems, Services and Operations
>> Information Technology Services, The University of Adelaide
>> kim.hawtin at adelaide.edu.au
>>
>> _______________________________________________
>> This is the Linux C Programming List
>> :  http://lists.linux.org.au/listinfo/linuxcprogramming List
>>
>



More information about the linuxCprogramming mailing list