Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a child process using Node.js built-in module.
Node.js
const { [1] } = require('child_process');
const child = [1].fork('child.js'); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'spawn' instead of 'fork' which does not set up IPC automatically.
Using 'exec' which runs a shell command but doesn't create a Node.js child process.
✗ Incorrect
The 'fork' method is used to create a new Node.js process and establish IPC communication.
2fill in blank
mediumComplete the code to send a message from the parent process to the child process.
Node.js
child.[1]({ text: 'Hello child' });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'emit' which is for event emitters but not for IPC message sending.
Using 'write' which is for streams, not IPC messages.
✗ Incorrect
The 'send' method is used to send messages between parent and child processes in Node.js.
3fill in blank
hardFix the error in the child process code to receive messages from the parent.
Node.js
process.[1]('message', (msg) => { console.log('Received:', msg); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'onMessage' which is not a valid method.
Using 'listen' which is not an event listener method.
✗ Incorrect
The 'on' method listens for events, including 'message' events from the parent process.
4fill in blank
hardFill both blanks to send a message from child to parent and listen in parent.
Node.js
// In child process process.[1]({ reply: 'Hi parent' }); // In parent process child.[2]('message', (msg) => { console.log('From child:', msg); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'emit' instead of 'send' for IPC messages.
Using 'write' which is for streams, not IPC.
✗ Incorrect
Child uses 'send' to send messages; parent uses 'on' to listen for 'message' events.
5fill in blank
hardFill all three blanks to create a simple IPC communication where parent sends, child replies, and parent listens.
Node.js
// Parent process
const { [1] } = require('child_process');
const child = [1].fork('child.js');
child.[2]({ greeting: 'Hello child' });
child.[3]('message', (msg) => {
console.log('Child says:', msg);
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'spawn' instead of 'fork' which does not set up IPC.
Using 'emit' instead of 'send' for sending messages.
✗ Incorrect
Parent uses 'fork' to create child, 'send' to send message, and 'on' to listen for child's reply.