2
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  console.log(`Master ${process.pid} is running`);

  // Fork workers.
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('exit', (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} died`);
  });
} else {
  // Workers can share any TCP connection
  // In this case it is an HTTP server
  http.createServer((req, res) => {
    res.writeHead(200);
    res.end('hello world\n');
  }).listen(8000);

  console.log(`Worker ${process.pid} started`);
}

We spawn as many number of workers as there are cores in system. so for 4 cores, we spawn 4 workers in master process. Then where exactly master process runs, i.e. on which core? And since master routes request to workers, how is it ensured that master is always running and listening on a port?

2 Answers 2

1

That's a part of cpu scheduling ,os will manage which core should run which process.

You can create more child processes than your available cpus (which is not suggested ) so os will handle which cpu core will run which process ,master process can share cpu with some child process.

Sign up to request clarification or add additional context in comments.

Comments

1

We spawn as many number of workers as there are cores in system. so for 4 cores, we spawn 4 workers in master process. Then where exactly master process runs, i.e. on which core?

Process assignment to the core is done by OS based on certain params. However, if you are not running the cluster module Node.js runs the single thread and if you run the Node.js application with cluster the main thread becomes master and you can spawn child processes based on your OS and hardware configuration, not going deep in that.

And since master routes request to workers, how is it ensured that master is always running and listening on a port?

Sometimes it happens that your master process is killed, but the child process won't receive signal. So, they do run. However, you can handle such scenario by using simple code:

const k = cp.spawn();
childPids.push(k.pid)

Or,

const k = cp.fork();
childPids.push(k.pid)

And with the pid you can kill the child process. This is something you need to do when your child process won't killed. There could be lot of scenarios can't cover all of them, but I tried to give a general overview to avoid such situations.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.