Bird
0
0

Identify the error in this IPC code snippet:

medium📝 Debug Q14 of 15
Node.js - Child Processes
Identify the error in this IPC code snippet:
const { fork } = require('child_process');
const child = fork('child.js');
child.send('Start');
child.on('message', msg => console.log(msg));

// child.js
process.send('Ready');
process.on('message', msg => console.log('Child got:', msg));
AParent sends message before child process is ready to receive.
BChild process sends message before 'message' event listener is set.
CChild process calls process.send() before parent listens.
DNo error; code works correctly.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze message timing and understand child readiness

    Parent sends 'Start' immediately after fork; child may not be ready yet. Child sends 'Ready' immediately but parent may miss it if not listening yet.
  2. Final Answer:

    Parent sends message before child process is ready to receive. -> Option A
  3. Quick Check:

    Parent must wait for child's 'Ready' before sending [OK]
Quick Trick: Wait for child's ready message before sending [OK]
Common Mistakes:
  • Assuming child is ready immediately after fork
  • Ignoring asynchronous nature of IPC
  • Thinking process.send() order causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes