4

When working with child_process or cluster, is it best to have as many subprocess as CPU cores, or should we subtract one to that number, as the master process is already sitting on one of them ?

Expanding on this question, how are things handled by Node.js when we spawn more processes than there are CPU cores on the machine ?

Yves M.
  • 29,855
  • 23
  • 108
  • 144
mlarcher
  • 447
  • 1
  • 7
  • 16
  • 2
    Each node process does the exact same thing regardless of how many nodejs processes there are relative to the number of CPUs. The issue is how the OS splits the available CPU time among the nodejs processes. So, this is mostly an OS question. If you have more nodejs processes than CPUs and you use only non-blocking code in your nodejs server, then you will probably be slowing things down by having more nodejs servers than CPUs because you will cause more processor context switching, but the details of that are OS and hardware specific. – jfriend00 Apr 28 '17 at 16:29
  • any idea how it is handled on osx vs linux ? – mlarcher May 01 '17 at 12:13
  • how what is handled? – jfriend00 May 01 '17 at 13:02
  • I mean the different approach used by osx/linux when dealing with how the OS splits the available CPU time among the nodejs processes. – mlarcher May 02 '17 at 08:09
  • It's not clear to me what exactly you're asking about. All modern OS's have a scheme for time slicing available CPUs between the processes that are not blocked. It is pre-emptive so it will simply suspend or start execution of the process as the process is given CPU time. A node.js process works no differently than any other process written in any language (running at the same priority level). If you're trying to ask a generic question about how does OS CPU scheduling work across processes and threads, then you'd have to ask a question specific to a particular OS and a particular situation. – jfriend00 May 02 '17 at 10:16
  • FYI: [What scheduling algorithms does Linux kernel use?](http://stackoverflow.com/questions/1603341/what-scheduling-algorithms-does-linux-kernel-use) and [Understanding Linux Kernel](http://web.archive.org/web/20141016155917/http://oreilly.com/catalog/linuxkernel/chapter/ch10.html). – jfriend00 May 02 '17 at 10:22
  • @jfriend00, I was just trying to get a clearer picture of what you said in your first answer. Thanks for the links, I'll dig into that :) As per my original request, you say that "each node process does the exact same thing regardless of how many nodejs processes there are relative to the number of CPUs". That's only true to some extent, as there is one node process whose only role is to spawn and monitor the others. What I am trying to determine is if it can share a cpu core with one of the subprocess or if that is a suboptimal setup due to the context switching it would generate. – mlarcher May 03 '17 at 12:45

0 Answers0