0
0
Node.jsframework~10 mins

fork for Node.js child processes in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - fork for Node.js child processes
Main Process Starts
Call fork() to create Child
Child Process Runs Separate Script
Parent and Child Communicate via Messages
Child Sends Message to Parent
Parent Receives and Handles Message
Child Exits
Parent Detects Child Exit
End
The main process uses fork() to create a child process running a separate script. They communicate by sending messages until the child exits and the parent detects it.
Execution Sample
Node.js
const { fork } = require('child_process');
const child = fork('child.js');
child.on('message', msg => console.log('Parent got:', msg));
child.send('Hello Child');
This code forks a child process running 'child.js', sends it a message, and listens for messages from the child.
Execution Table
StepActionProcessMessage SentMessage ReceivedState Change
1Main process startsParentParent running
2Call fork('child.js')ParentChild process created
3Child process starts running 'child.js'ChildChild running
4Parent sends message 'Hello Child'ParentHello ChildMessage sent to child
5Child receives 'Hello Child'ChildHello ChildChild processes message
6Child sends message 'Hello Parent'ChildHello ParentMessage sent to parent
7Parent receives 'Hello Parent'ParentHello ParentParent processes message
8Child exitsChildChild process ends
9Parent detects child exitParentParent cleans up
10Execution endsParentNo more child processes
💡 Child process exits and parent detects it, ending communication.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
childundefinedChildProcess object createdChildProcess object with message sentChildProcess object with message receivedChildProcess object ended
Key Moments - 3 Insights
Why does the child process run a separate script instead of the same code as the parent?
Because fork() creates a new Node.js process running the specified script file separately, allowing independent execution and communication. See execution_table steps 2 and 3.
How do parent and child communicate after forking?
They send messages using child.send() and listen with 'message' event handlers, as shown in steps 4-7 in the execution_table.
What happens when the child process exits?
The parent detects the child's exit event and can clean up resources, shown in steps 8 and 9.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what message does the parent send to the child at step 4?
A"Start"
B"Hello Parent"
C"Hello Child"
D"Exit"
💡 Hint
Check the 'Message Sent' column at step 4 in the execution_table.
At which step does the child process send a message back to the parent?
AStep 5
BStep 6
CStep 7
DStep 8
💡 Hint
Look for 'Message Sent' by the child in the execution_table.
If the child never sends a message back, which step would be missing in the execution_table?
AStep 6
BStep 4
CStep 5
DStep 9
💡 Hint
Step 6 shows the child sending a message to the parent.
Concept Snapshot
fork() creates a new Node.js child process running a separate script.
Parent and child communicate by sending messages with send() and 'message' events.
Child runs independently but can send data back to parent.
Parent listens for child's exit to clean up.
Useful for parallel tasks without blocking main process.
Full Transcript
In Node.js, fork() creates a new child process running a separate JavaScript file. The parent process can send messages to the child using child.send(), and the child listens for these messages. The child can also send messages back to the parent. This communication happens through an IPC channel. When the child finishes its work, it exits, and the parent detects this exit event to clean up. This allows running tasks in parallel without blocking the main process.