0
0
Node.jsframework~20 mins

Forking workers per CPU core in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cluster Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when forking workers equal to CPU cores?
Consider this Node.js code snippet that forks workers equal to the number of CPU cores. What will be logged to the console when the master process runs?
Node.js
import cluster from 'cluster';
import os from 'os';

if (cluster.isPrimary) {
  const cpuCount = os.cpus().length;
  console.log(`Master process running with ${cpuCount} CPUs`);
  for (let i = 0; i < cpuCount; i++) {
    cluster.fork();
  }
} else {
  console.log(`Worker ${process.pid} started`);
}
AMaster process running with 4 CPUs\nWorker 1234 started\nWorker 1235 started\nWorker 1236 started\nWorker 1237 started
BWorker 1234 started\nWorker 1235 started\nWorker 1236 started\nWorker 1237 started
CMaster process running with 4 CPUs
DMaster process running with 1 CPU\nWorker 1234 started
Attempts:
2 left
💡 Hint
Remember that the master process logs first, then forks workers equal to CPU cores, each logging their start.
📝 Syntax
intermediate
2:00remaining
Which option correctly forks workers per CPU core?
Which code snippet correctly forks a worker process for each CPU core using Node.js cluster module?
Aimport cluster from 'cluster'; import os from 'os'; if (cluster.isMaster) { for (let i = 0; i < os.cpus().length; i++) cluster.fork(); }
Bimport cluster from 'cluster'; import os from 'os'; if (cluster.isPrimary) { for (let i = 0; i < os.cpus().length; i++) cluster.fork(); }
Cconst cluster = require('cluster'); const os = require('os'); if (cluster.isMaster) { for (let i = 0; i < os.cpus().length; i++) cluster.fork(); }
Dconst cluster = require('cluster'); const os = require('os'); if (cluster.isPrimary) { for (let i = 0; i < os.cpus().length; i++) cluster.fork(); }
Attempts:
2 left
💡 Hint
Check the correct property name for the primary process in Node.js cluster module in recent versions.
🔧 Debug
advanced
2:00remaining
Why does this cluster code fork only one worker despite multiple CPUs?
Given this code, why does it fork only one worker even if the machine has multiple CPU cores? import cluster from 'cluster'; import os from 'os'; if (cluster.isPrimary) { cluster.fork(); } else { console.log('Worker started'); }
Node.js
import cluster from 'cluster';
import os from 'os';

if (cluster.isPrimary) {
  cluster.fork();
} else {
  console.log('Worker started');
}
ABecause cluster.isPrimary is false, so the master never forks workers.
BBecause os.cpus() is not used, so the code errors out and forks no workers.
CBecause the code calls cluster.fork() only once, so only one worker is created regardless of CPU count.
DBecause cluster.fork() is asynchronous and needs a callback to fork multiple workers.
Attempts:
2 left
💡 Hint
Count how many times cluster.fork() is called in the master process.
state_output
advanced
2:00remaining
What is the value of workers count after forking per CPU core?
In this code, what is the value of Object.keys(cluster.workers).length after the master forks workers equal to CPU cores?
Node.js
import cluster from 'cluster';
import os from 'os';

if (cluster.isPrimary) {
  const cpuCount = os.cpus().length;
  for (let i = 0; i < cpuCount; i++) {
    cluster.fork();
  }
  console.log(Object.keys(cluster.workers).length);
}
AAlways 1, because cluster.workers only tracks one worker.
BUndefined, because cluster.workers is not a valid property.
C0, because cluster.workers is empty until workers send a message.
DThe number of CPU cores on the machine (e.g., 4 or 8).
Attempts:
2 left
💡 Hint
cluster.workers holds all forked worker processes indexed by their ids.
🧠 Conceptual
expert
2:00remaining
What happens if a worker crashes in a cluster with workers per CPU core?
In a Node.js cluster setup where one worker is forked per CPU core, what is the typical behavior when a worker process crashes unexpectedly?
AThe master process detects the exit event and automatically forks a new worker to replace the crashed one.
BAll workers shut down immediately and the master process exits.
CThe crashed worker restarts itself without master intervention.
DNothing happens; the crashed worker stays dead and no new workers are created.
Attempts:
2 left
💡 Hint
Think about how cluster module handles worker exits by default.