0
0
Node.jsframework~20 mins

fork for Node.js child processes in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Node.js Fork Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Node.js fork example?
Consider this code that uses fork from the child_process module. What will be printed to the console?
Node.js
import { fork } from 'child_process';

const child = fork('./child.js');

child.on('message', (msg) => {
  console.log('Parent received:', msg);
});

child.send('Hello child');

// child.js content:
// process.on('message', (msg) => {
//   process.send(msg + ' world');
// });
ASyntaxError: Unexpected token import
BParent received: Hello child world
CNo output, program hangs
DParent received: Hello child
Attempts:
2 left
💡 Hint
Remember that fork creates a child process that can communicate via messages.
📝 Syntax
intermediate
1:30remaining
Which option correctly imports and uses fork in Node.js ES modules?
You want to create a child process using fork in a Node.js ES module. Which code snippet is correct?
A
import { fork } from 'child_process';
const child = fork();
B
const { fork } = require('child_process');
const child = fork('script.js');
C
import fork from 'child_process';
const child = fork('script.js');
D
import { fork } from 'child_process';
const child = fork('script.js');
Attempts:
2 left
💡 Hint
ES modules use import syntax and fork requires a script path.
🔧 Debug
advanced
2:30remaining
Why does this forked child process not receive messages?
Given this parent and child code, why does the child never log the message sent by the parent?
Node.js
/* parent.js */
import { fork } from 'child_process';
const child = fork('./child.js');
child.send('ping');

/* child.js */
console.log('Child started');
process.on('message', (msg) => {
  console.log('Child received:', msg);
});
AThe child process exits immediately before receiving messages
BThe child.js file has a syntax error preventing execution
CThe parent never sends any message
DThe fork call is missing the stdio option to enable IPC
Attempts:
2 left
💡 Hint
Check if the child process stays alive to receive messages.
state_output
advanced
2:00remaining
What is the value of variable 'count' after this forked process communication?
In this example, the parent and child share a variable 'count'. What is the final value of 'count' in the parent after the child sends messages?
Node.js
import { fork } from 'child_process';

let count = 0;
const child = fork('./child.js');

child.on('message', (msg) => {
  if (msg === 'increment') count++;
});

child.send('start');

// child.js
// process.on('message', (msg) => {
//   if (msg === 'start') {
//     process.send('increment');
//     process.send('increment');
//   }
// });
A2
B1
C0
DUndefined
Attempts:
2 left
💡 Hint
The child sends two 'increment' messages, each incrementing count by 1.
🧠 Conceptual
expert
3:00remaining
Which statement about Node.js fork and IPC is true?
Select the correct statement about the behavior of fork and inter-process communication (IPC) in Node.js.
AThe forked child process shares the same memory space as the parent, so variables are shared directly.
BThe fork method can only run JavaScript files located in the same directory as the parent.
CMessages sent via <code>child.send()</code> are serialized and sent asynchronously over IPC channels.
DThe child process automatically inherits all open file descriptors from the parent.
Attempts:
2 left
💡 Hint
Think about how processes communicate and memory isolation.