Bird
0
0

Analyze this child process code:

medium📝 Debug Q7 of 15
Node.js - Worker Threads
Analyze this child process code:
const { fork } = require('child_process');
const child = fork('child.js');
child.send('start');
child.on('message', msg => console.log(msg));

And child.js:
process.on('message', msg => {
  console.log('Child received:', msg);
});

Why might the parent not receive any messages from the child?
AThe parent must call <code>child.disconnect()</code> before receiving messages.
BThe parent should use <code>child.on('data')</code> instead of 'message'.
CThe child.js file path is incorrect, so the child never starts.
DThe child process never sends a message back to the parent.
Step-by-Step Solution
Solution:
  1. Step 1: Check child message handling

    The child listens for messages but does not send any back.
  2. Step 2: Parent expects messages

    The parent waits for messages, but none are sent, so no output occurs.
  3. Final Answer:

    Child never sends messages back to parent. -> Option D
  4. Quick Check:

    Child must send messages explicitly [OK]
Quick Trick: Child must send messages to parent explicitly [OK]
Common Mistakes:
  • Confusing 'message' with 'data' event
  • Assuming sending messages is automatic
  • Believing disconnect is needed before messaging

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes