File tree Expand file tree Collapse file tree 3 files changed +38
-1
lines changed Expand file tree Collapse file tree 3 files changed +38
-1
lines changed Original file line number Diff line number Diff line change
1
+ const fibonacci = ( num ) => num <= 1 ? 1 : fibonacci ( num - 1 ) + fibonacci ( num - 2 )
2
+
3
+ process . on ( 'message' , ( { n} ) => {
4
+ process . send ( { fib : fibonacci ( n ) , n } )
5
+ // optional - there is no reason why this child process
6
+ // can't be called multiple times.
7
+ process . exit ( )
8
+ } )
Original file line number Diff line number Diff line change
1
+ const { fork } = require ( 'child_process' )
2
+
3
+ const child1 = fork ( 'fork-child' )
4
+ const child2 = fork ( 'fork-child' )
5
+ const child3 = fork ( 'fork-child' )
6
+
7
+ // send data to the child process to perform the calculation
8
+ child1 . send ( { n : 5 } ) ;
9
+ child2 . send ( { n : 10 } ) ;
10
+ child3 . send ( { n : 45 } ) ;
11
+
12
+ child1 . on ( 'message' , ( m ) => {
13
+ console . log ( 'PARENT child1 got message:' , m ) ;
14
+ } ) ;
15
+ child2 . on ( 'message' , ( m ) => {
16
+ console . log ( 'PARENT child2 got message:' , m ) ;
17
+ } ) ;
18
+ child3 . on ( 'message' , ( m ) => {
19
+ console . log ( 'PARENT child3 got message:' , m ) ;
20
+ } ) ;
21
+
22
+ child1 . on ( 'exit' , ( ) => {
23
+ console . log ( 'child1 exited' ) ;
24
+ } ) ;
25
+ child2 . on ( 'exit' , ( ) => {
26
+ console . log ( 'child2 exited' ) ;
27
+ } ) ;
28
+ child3 . on ( 'exit' , ( ) => {
29
+ console . log ( 'child3 exited' ) ;
30
+ } ) ;
Original file line number Diff line number Diff line change @@ -130,7 +130,6 @@ process.on('message', ({ n }) => {
130
130
The parent process creates 3 child processes, and passes a range of numbers to them for calculating.
131
131
132
132
``` javascript
133
-
134
133
const { fork } = require (' child_process' )
135
134
136
135
const child1 = fork (' fork-child' )
You can’t perform that action at this time.
0 commit comments