Bird
0
0

Examine this IPC code snippet:

medium📝 Debug Q6 of 15
Node.js - Child Processes
Examine this IPC code snippet:
const { fork } = require('child_process');
const child = fork('child.js');
child.on('message', msg => console.log('Parent received:', msg));
child.send('Ping');

// child.js content:
// process.send('Pong');

Why does the parent never log any message?
ABecause 'process.send' is not a valid method in child processes
BBecause the parent sends a message before setting up the 'message' event listener
CBecause the child process never listens for messages and thus never responds to 'Ping'
DBecause the parent should use 'child.on('data')' instead of 'child.on('message')'
Step-by-Step Solution
Solution:
  1. Step 1: Analyze child process code

    The child process calls process.send('Pong') immediately but does not listen for any messages.
  2. Step 2: Parent sends 'Ping' message

    The parent sends 'Ping' to the child, but the child does not handle incoming messages, so no response is triggered.
  3. Step 3: Why no message received?

    The child sends 'Pong' only once at startup, but if the child process exits or the message is sent before IPC channel is ready, parent may not receive it.
  4. Final Answer:

    Because the child process never listens for messages and thus never responds to 'Ping' -> Option C
  5. Quick Check:

    Child must handle messages to respond [OK]
Quick Trick: Child must listen for messages to respond properly [OK]
Common Mistakes:
  • Assuming child automatically responds without message handler
  • Confusing 'message' and 'data' events
  • Believing process.send is invalid in child processes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes