4

The man page on fork() states that it does not copy data pages, it maps them into the child process and puts a copy-on-write flag. Is that behavior:

  • consistent between flavors of Linux?
  • considered an implementation detail and therefore likely to change?

I'm wondering if I can use fork() as a means to get a shared read-only memory block on the cheap. If the memory is physically copied, it would be rather expensive - there's a lot of forking going on, and the data area is big enough - but I'm hoping not...

5 Answers 5

3

Linux running on machines without a MMU (memory management unit) will copy all process memory on fork().

However, those systems are usually very small and embedded and you probably don't have to worry about them.

Many services such as Apache's fork model, use the initialize and fork() method to share initialized data structures.

You should be aware that if you are using languages like Perl and Python that use reference-counted variables, or C++ shared_ptr's, this model will not work. It will not work because as the reference counts are adjusted up and down, the memory becomes unshared and gets copied.

This causes huge amounts of memory usage in Perl daemons like SpamAssassin that attempt to use an initialize and fork model.

1
  • Point taken, but I'm not targeting those. It's a plain vanilla hosted Web server environment. Sep 8, 2010 at 19:08
3

Yes you can certainly rely on it on MMU-Linux kernels; this is almost everything.

However, the page size isn't the same everywhere.

It is possible to explicitly make a shared memory area for forked process, by using mmap() to create an anonymous map - one which is not backed by a physical file. On fork, this area will always remain shared (provided the child doesn't unmap it, or map something else in at the same address). You can mprotect it to be readonly if you want.

Memory allocated with (for example) malloc can easily end up sharing a page with something that isn't readonly, which means it gets copied anyway when another structure is modified. This includes internal structures used by the malloc implementation. So you might want to mmap a specific area for this purpose and allocate from that.

2

Can you rely on the fact that all Linux flavors do it this way? No. But you can rely on the fact that those who don't use an even faster method.

Therefore you should use the feature and rely on it and revisit your decision if you get a performance problem.

2
  • Can you explicate on the nature of the different, faster method? Is that a guess based on the general ideology of the system, or solid knowledge? Sep 8, 2010 at 18:04
  • Linux hackers always want to make things faster, better, smaller. Most releases of Linux do more with less lines of code, for example. So it's very unlikely that they would drop support for copy-on-write unless something even better came up (which is very unlikely with the current CPU architectures). Sep 9, 2010 at 7:05
2

The success of this approach depends on how well you stick to your self-imposed "read-only" limitation. Both parent and child have to obey this stricture, else the memory gets copied.

This may not be the catastrophe you're envisioning, however. The kernel can copy as little as a single page (typically 4 KB) to implement CoW semantics. A typical Linux server will use something more complex, some sort of slab allocator, so the copied region could be much larger.

The main point is that this is decoupled from your program's conception of its memory use. If you malloc() 1 GB of RAM, fork off a child, and the child changes just the first byte of that memory block, the entire 1 GB block isn't copied. Perhaps as little as one page is copied, up to the slab size containing that first byte.

1

Yes

All the linux distros use the same kernel, albeit with slightly different versions and releases of it.

It's unlikely that another underlying fork(2) implementation will be faster any time soon, so it's a safe bet that copy-on-write will continue to be the mechanism. Perhaps it won't be forever, but for years, definitely.

Certainly some major software systems (for example, Phusion Passenger) use fork(2) in the same way that you want to, so you would not be the only one taking advantage of CoW.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.