Can Malloc Use Mmapped Shared Memory for Efficient Parent-Child Data Sharing?

In summary, the problem is that the parent writing back primes to the list it causes pages to be copied for no reason. The parent could prevent this by using an mmap'ed shared memory file instead.
  • #1
TylerH
729
0
I want to set the area from with malloc gets its memory to a mmap'ed shared memory file, to share data between children and parents without causing pagefaults. It is safe because the data used by children is guaranteed not to be touched by the parent and the children are guaranteed not to malloc at all. Is there a standard way to set the memory area which malloc manages?
 
Technology news on Phys.org
  • #2
In GNU Linux you can install malloc hooks. See here.
This is not portable to other operating systems.

In C++ you can:
- overload the global new operator,
- declare a custom new operator within a class
- specify an allocator for the various C++ containers
- use placement new syntax
All these methods are portable.
 
  • #3
In Windows you can use a shared memory file. MSDN article:

msdn_named_shared_memory.aspx

I'm not sure if the windows debugger functions could be used unless windows allows processes to attach each other. If so, then you could use DebugActiveProcess(), ReadProcessMemory(), WriteProcessMemory(), ... . You'd also probably need to use DuplicateHandle() for any mutexes or semaphores that you'd want to used for synchronization. One method to use DuplicateHandle() is to include the hex values of the main process id and any handles to be shared on the "command line" used for CreateProcess().

msdn_debugger_functions.aspx

msdn_create_process.aspx
 
  • #4
I looked at the glibc malloc hook functions, but I'd really like to avoid writing a full heap allocator. It's outside the scope of my knowledge to do well. I also tried redefining sbrk and brk, but that didn't work either. (They weren't called by malloc while mallocing memory.)

As for the Windows stuff, rcgldr, my application would be totally unfit for Windows because Win processes are so heavy and my program forks a lot. Windows, IIRC, doesn't do COW address space copies. I didn't give enough context for you to know this, though.
 
  • #5
Without knowing more of your problem, this sounds like a terrible idea to me. And if your child processes are so simple that they don't need to allocate, why not use threads? [Note that event printf does a malloc].
 
  • #6
Threading in C++ is still pretty awful. The program makes a list of primes. The parent maintains the list and forks off a child for each number to be checked and the exit status of the child indicates the primality of the number.

The problem I'm having is that the parent writing back primes to the list it causes pages to be copied for no reason, so I was going to mmap an anonymous shared region of memory to use as the heap of the parent to prevent the pages from being copied.
 
  • #7
TylerH said:
The program makes a list of primes. The parent maintains the list and forks off a child for each number to be checked and the exit status of the child indicates the primality of the number. The problem I'm having is that the parent writing back primes to the list it causes pages to be copied for no reason, so I was going to mmap an anonymous shared region of memory to use as the heap of the parent to prevent the pages from being copied.
If this was done using threads, there would only be a single and common virtual address space (just one actual instance of the list of primes) for the parent and all child threads. Each child thread would only need to be spawned one time, and each child thread could pend on a mutex and/or semaphore for each number to be checked, and then post a status also using a mutex and/or semaphore. Why isn't this a viable solution for this program?
 
Last edited:

Related to Can Malloc Use Mmapped Shared Memory for Efficient Parent-Child Data Sharing?

1. How do I set malloc's memory area in my program?

To set malloc's memory area in your program, you can use the malloc_trim function. This function takes in a size parameter and frees any unused memory chunks at the end of the heap, allowing you to set the new memory area.

2. What is the default memory area used by malloc?

The default memory area used by malloc is the heap. This is a dynamically allocated section of memory that is used to store variables and data structures at runtime.

3. Can I change the memory area used by malloc?

Yes, you can change the memory area used by malloc by using the malloc_set_state function. This function allows you to specify a custom memory area for malloc to use.

4. How does setting malloc's memory area affect program performance?

Setting malloc's memory area can have a significant impact on program performance. By setting a larger memory area, you can reduce the number of times malloc needs to request more memory from the operating system, which can improve program efficiency.

5. Are there any potential risks in setting malloc's memory area?

Yes, there are potential risks in setting malloc's memory area. If the memory area is too small, it can lead to memory fragmentation and cause the program to crash. On the other hand, setting a memory area that is too large can result in wasted memory and potentially slow down the program's performance.

Similar threads

  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
686
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Sci-Fi Writing and World Building
3
Replies
96
Views
6K
  • Art, Music, History, and Linguistics
Replies
1
Views
1K
Replies
4
Views
2K
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
Back
Top