0

Let's say I have a process that is a "manager" and it spins up "workers":

  child = childProcess.fork("./worker", [], {execArgv: args} );
  child.send(startSignal);

The child processes will be doing the heavy lifting while the manager just ... manages.

Will those child processes be locked into using the processor core that the manager is using or will the OS be free to farm them off to other processor cores if the manager core is too busy?

I realize this may be OS specific. I'm developing on Ubuntu but could deploy it out to any linux (likely CoreOS). For posterity Windows and Mac would be worth discussing too.

jcollum
  • 43,623
  • 55
  • 191
  • 321
  • A new process is a new process. It will run wherever the OS decides it's going to run. On any reasonably modern OS, I would imagine it would balance out to different cores quite optimally. And if it doesn't, there's nothing you're going to do about it. – MobA11y Aug 07 '14 at 19:14
  • But it's not a completely new process, it's a child process. Is a child process exactly the same as a new process? Etc. – jcollum Aug 07 '14 at 19:33
  • A process is a process. A process being the "child" of another, simply means that the parent process gets certain signals from the child. If it should have to abort, exit codes, and any signals you specifically build in. But besides that, they should be thought of as completely separate. In fact, if the parent process aborts, the child process will keep running, becoming orphaned, and end up sending those signals to the parents parent process, usually an OS process. You should read the wikipedia article about parent processes, it's a pretty reasonable overview. – MobA11y Aug 07 '14 at 19:44
  • It appears to be more complicated than you make it sound: http://stackoverflow.com/questions/4592575/is-it-possible-to-prevent-children-inheriting-the-cpu-core-affinity-of-the-paren – jcollum Aug 07 '14 at 19:56
  • If by more complicated, you mean my list of stuff you can manage was incomplete, yes. I only get 600 characters. The ramifications of setting processor afinity are relatively small. And what you're doing at that point is preventing the OS from doing it's own internal balancing (a bad idea in almost all circumstances). Also, you're post is about Node.js, so my list is actually fairly complete, if you really want to tweak processes, you should look into a lower level language. Sending child processes to whatever core is the default, and most likely desired, behavior. – MobA11y Aug 07 '14 at 20:03

1 Answers1

2

Linux answer only, short answer YES.

Long answer: On checking the Node.js docs (last page):-

This is a special case of the spawn() functionality for spawning Node processes. In addition to having all the methods in a normal ChildProcess instance, the returned object has a communication channel built-in. See child.send(message, [sendHandle]) for details.

These child Nodes are still whole new instances of V8. Assume at least 30ms startup and 10mb memory for each new Node. That is, you cannot create many thousands of them.

Since it's a new V8 instance, the kernel will slice the threads and multitask the job. Linux OS been doing multitasking with multi-core a long time ago (from kernel 2.4). Hence, you want to spawn only as many child processes as the CPU. Use require('os').cpus().length to determine the CPU count.

Mac is unix (Next?) based, so I'd assume that the kernel behaves the same way.

Alvin K.
  • 4,329
  • 20
  • 25