[LCP] creating a file descriptor entry for a piece of memory

Mark Farnell mark.farnell at gmail.com
Thu Nov 6 12:42:35 EST 2008


So do you reckon the best way is to use shm_open?

Pseudocode:

use a random generator (use timer as seed) to generate a number and
convert to string, this is name of shared object

fd = shm_open(name, O_CREAT | O_EXCL etc.) (fd is global shared)

if fails, generate a different name and try again

spawn pthreads

in each thread, mmap fd to a mapping owned by the thread.

Now each thread can do independent mprotect actions on its own mapping

each thread munmap its own mapping before it closes.

master join all threads

master shm_unlink(fd)

master exits

Is this the way to work?

So after calling shm_unlink, the shared object, together with the
entry under /dev/shm/ will be deleted?

If the process crashes before calling shm_unlink(), you told me that
shared memory will be freed.  However will the entry under /dev/shm
stay?

Thanks!

Mark

On Wed, Nov 5, 2008 at 4:01 PM, Paul Gearon <gearon at ieee.org> wrote:
> On Tue, Nov 4, 2008 at 6:18 PM, Mark Farnell <mark.farnell at gmail.com> wrote:
>> mmap maps a file to the memory space and the anoynomous mode of mmap
>> allows allocation of a piece of memory to an address space, then share
>> this address space to child processes via fork.
>
> Sure.
>
>> That's great.  However are there any ways to allocate a piece of
>> memory and assign it to a file ID (no files actually created on HD or
>> in /dev), then I can mmap that file ID as many times as I like?
>
> No, that can't be done. Your options are to not use a file descriptor,
> and to use one that is already allocated. You can't attach a file
> descriptor after a memory allocation with mmap.
>
> Using shared mode for memory mapping allows you to have multiple
> processes to access that memory. But then you need some kind of
> mechanism for communicating the memory you want to share. If you spawn
> a child, then you can bring a file descriptor with you. Otherwise, you
> can use an identifier that maps to the filesystem. This is essentially
> the mechanism for communicating with other processes. If you don't
> have any way of communicating a "name" like this, then there's no way
> for another process to know how to find that same memory.
>
>> The trouble of shm_create is that it creates an entry on the file
>> system and therefore if I run multiple programs concurrently (which
>> calls shm_create with the same hard-coded name) I will be in trouble.
>
> This is the same deal as for mmap. You need some kind of name to
> identify the memory to be shared, and the filesystem is used to
> communicate this.
>
>> That memory should be freed if the process quits by any means.
>
> Allocated memory is always freed when processes quit. If it's shared,
> then it's freed from the address space of any exiting process, and
> will be removed completely when the last process exits.
>
> Paul
>
> _______________________________________________
> This is the Linux C Programming List
> :  http://lists.linux.org.au/listinfo/linuxcprogramming List
>



More information about the linuxCprogramming mailing list