0
0
Node.jsframework~10 mins

Forking workers per CPU core in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the cluster module.

Node.js
const cluster = require('[1]');
Drag options to blanks, or click blank then click option'
Ahttp
Bcluster
Cos
Dfs
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'os' instead of 'cluster' to import the cluster module.
Forgetting to import the cluster module.
2fill in blank
medium

Complete the code to import the OS module.

Node.js
const os = require('[1]');
Drag options to blanks, or click blank then click option'
Aos
Bcluster
Chttp
Dpath
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cluster' instead of 'os' to import the OS module.
Confusing 'os' with 'http' or 'path'.
3fill in blank
hard

Fix the error in the code to get the number of CPU cores.

Node.js
const numCPUs = os.[1]();
Drag options to blanks, or click blank then click option'
AcpuCount
Bcores
CcpuCores
Dcpus
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent method like 'cpuCount' or 'cores'.
Forgetting the parentheses after the method name.
4fill in blank
hard

Fill both blanks to fork a worker for each CPU core.

Node.js
if (cluster.[1]) {
  for (let i = 0; i < numCPUs.[2]; i++) {
    cluster.fork();
  }
}
Drag options to blanks, or click blank then click option'
AisPrimary
Blength
CisMaster
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'isMaster' instead of 'isPrimary'.
Using 'count' or 'size' instead of 'length' for array length.
5fill in blank
hard

Fill all three blanks to log when a worker exits and fork a new one.

Node.js
cluster.on('[1]', (worker, code, signal) => {
  console.log(`Worker ${worker.[2] died`);
  cluster.[3]();
});
Drag options to blanks, or click blank then click option'
Aexit
Bpid
Cfork
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'disconnect' or 'close' instead of 'exit' event.
Using 'id' or 'workerId' instead of 'pid' for worker identifier.
Using 'spawn' or 'create' instead of 'fork' to start a new worker.