Child processes let Node.js run multiple tasks at the same time. This helps when one task takes a long time and you don't want to stop everything else.
When you need to run a heavy calculation without freezing your app.
When you want to run a separate program or script from your Node.js code.
When you want to handle multiple tasks in parallel to speed up work.
When you want to keep your main app responsive while doing background jobs.
This program creates a child process that runs worker.js. It sends a message to start work and listens for replies. This keeps the main app free to do other things.
Node.js
const { fork } = require('child_process');
// Fork a child process to run worker.js
const child = fork('./worker.js');
// Send a message to the child
child.send('Start work');
// Listen for messages from the child
child.on('message', (msg) => {
console.log('Message from child:', msg);
});
OutputSuccess
Important Notes
Child processes run independently, so if one crashes, it won't crash the main app.
Use child processes to improve app speed and responsiveness.
Summary
Child processes let Node.js do many things at once.
They keep your app from freezing during heavy tasks.
You can send messages between main and child processes easily.
Practice
(1/5)
1. Why do Node.js applications use child processes?
easy
A. To make the app load faster on the internet
B. To reduce the size of the app files
C. To run heavy tasks without freezing the main app
D. To automatically update Node.js version
Solution
Step 1: Understand Node.js single-threaded nature
Node.js runs JavaScript in a single thread, so heavy tasks can block the app.
Step 2: Role of child processes
Child processes run tasks separately, so the main app stays responsive.
Final Answer:
To run heavy tasks without freezing the main app -> Option C
Quick Check:
Child processes prevent freezing = B [OK]
Hint: Child processes keep main app responsive during heavy work [OK]
Common Mistakes:
Thinking child processes speed up internet loading