Hacker News new | comments | show | ask | jobs | submit login

Ok, dumb question--I'm a bit naive on process forking. On Linux with Ruby, if I fork a process, must the new process stay on the same core? Forked processes begin by sharing memory initially in Ruby, correct? Can anyone enlighten me on the mechanics of forking and if there are scenarios where it helps with utilizing multiple cores (maybe relatively independent workers that just read shared settings?).

Forking seems...a little weird to me coming from my previous Windows background.

Forking creates a new process, initially "sharing" memory via copy-on-write semantics (and therefore being eligible to run on another core). However, C Ruby's default mark-sweep garbage collector touches pretty much all memory immediately, eliminating the typical memory savings of a forking model.

As I commented earlier, Ruby Enterprise Edition (made by the same guys as Passenger) ships an alternate GC that doesn't actually write to the memory space of the original objects in order to mark them.

As a result, some memory (but not necessarily as much as you'd expect) can be shared between processes forked from the same parent and running on multiple cores. Sort of a middle ground between totally separate processes and shared memory via non-GIL'ed threads.

So you won't get much by way of memory savings, but you might expand the volume of work each batch of threads (now on separate processors) could complete if you forked cores - 1 times?

There are different kinds of memory in a process, e.g. code segment, read-only data segment, and read-write data segment. The code and read-only memory can be shared between the parent and child processes. The read-write memory pages are copied-on-write when they are modified. There can be fair amount of sharing.

The linux kernel will schedule the new process on whatever core it wants.

Forked process in ruby have posix semantics, ie, NOT shared memory. Linux uses Copy-On-Write pages to conserve the amount of memory copying when the new process is created.

The process will be scheduled by the OS just like any other process. I think fork() is similar to CreateProcess() on Windows, but I haven't done any Windows programming that needed to create more processes.

CreateProcess can't implement fork() semantics. To my knowledge there's no good way to fork() on Windows.

Section 5.6 of http://www.redhat.com/support/wpapers/cygnus/cygnus_cygwin/a... is an interesting read about how the Cygwin team implemented fork.

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact