0
0
Node.jsframework~5 mins

fork for Node.js child processes in Node.js - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the fork method in Node.js do?
It creates a new child process that runs a separate Node.js script. This allows running code in parallel without blocking the main program.
Click to reveal answer
beginner
How do parent and child processes communicate when using fork?
They communicate via a built-in IPC channel. Parent uses child.send() and child.on('message'); child uses process.send() and process.on('message').
Click to reveal answer
intermediate
What is a common use case for using fork in Node.js?
To run CPU-heavy tasks or separate workloads in parallel without blocking the main event loop, improving performance and responsiveness.
Click to reveal answer
beginner
Which module do you need to import to use <code>fork</code> in Node.js?
You need to import the <code>child_process</code> module using <code>const { fork } = require('child_process');</code>.
Click to reveal answer
intermediate
What happens if the child process created by fork exits unexpectedly?
The parent process can listen for the 'exit' or 'close' events on the child process object to handle cleanup or restart the child.
Click to reveal answer
What does fork return in Node.js?
AA Promise
BA boolean indicating success
CA string with the child process ID
DA ChildProcess object
Which method is used to send a message from the parent to the child process?
Achild.send()
Bprocess.send()
Cchild.emit()
Dprocess.emit()
How does the child process listen for messages from the parent?
Aprocess.receive('message', callback)
Bchild.on('message', callback)
Cprocess.on('message', callback)
Dchild.receive('message', callback)
Which module must be imported to use fork?
Aprocess
Bchild_process
Ccluster
Dos
What is a key benefit of using fork over exec or spawn?
ABuilt-in communication channel between parent and child
BRuns shell commands directly
CConsumes less memory
DAutomatically restarts on failure
Explain how to create a child process using fork and how the parent and child communicate.
Think about the steps to start and talk between processes.
You got /5 concepts.
    Describe a scenario where using fork improves a Node.js application's performance.
    Consider tasks that slow down the main program.
    You got /5 concepts.