You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The typical operating system has different processes running in the background, and each process is being managed by a single-core of our CPU and will run a series of calculations each time it is being ticked. To take full advantage of our CPU using a single process, we would need a number of processes that is at least equal to the number of cores in our CPU. In addition, each process might be responsible for running a series of calculations of different logic, which will give the end user a better control over the CPU’s behavior.
@@ -32,7 +31,8 @@ Typically, `spawn` is more suitable for long-running process with large outputs.
32
31
33
32
## A Simple `exec`
34
33
35
-
In this example, we will call the `ls` command to listwith the optional param `-l` to show a long list of details.
34
+
In this example, we will call the `ls` command to listwith the optional param `-l` to show a long list of details.
35
+
36
36
<divclass="repl-code">
37
37
38
38
```javascript
@@ -57,7 +57,9 @@ We will now implement the previous example using the `spawn` method:
57
57
<divclass="repl-code">
58
58
59
59
```javascript
60
+
60
61
const { spawn } =require('child_process');
62
+
61
63
constls=spawn('ls', ['-l']);
62
64
63
65
ls.stdout.on('data', (data) => {
@@ -84,12 +86,14 @@ We are going to run two child processes, and pipe the output from one as the inp
84
86
<divclass="repl-code">
85
87
86
88
```javascript
89
+
87
90
const { spawn } =require('child_process');
91
+
88
92
constls=spawn('ls', ['-l']);
89
-
constwc=spawn('wc')
93
+
constwc=spawn('wc');
90
94
91
95
// pipe output from ls as input to wc
92
-
ls.stdout.pipe(wc.stdin)
96
+
ls.stdout.pipe(wc.stdin);
93
97
94
98
wc.stdout.on('data', (data) => {
95
99
console.log(`wc stdout: ${data}`);
@@ -116,25 +120,26 @@ In this example, we are going to create a child process that can receive a numbe
116
120
117
121
```javascript
118
122
119
-
constfibonacci= (num) => num <=1?1:fibonacci(num -1) +fibonacci(num -2)
123
+
constfibonacci= (num) => num <=1?1:fibonacci(num -1) +fibonacci(num -2);
120
124
121
125
process.on('message', ({ n }) => {
122
-
process.send({ fib:fibonacci(n), n })
126
+
process.send({ fib:fibonacci(n), n });
123
127
// optional - there is no reason why this child process
124
128
// can't be called multiple times.
125
-
process.exit()
129
+
process.exit();
126
130
})
127
131
128
132
```
129
133
130
134
The parent process creates 3 child processes, and passes a range of numbers to them for calculating.
131
135
132
136
```javascript
133
-
const { fork } =require('child_process')
134
137
135
-
constchild1=fork('fork-child')
136
-
constchild2=fork('fork-child')
137
-
constchild3=fork('fork-child')
138
+
const { fork } =require('child_process');
139
+
140
+
constchild1=fork('fork-child');
141
+
constchild2=fork('fork-child');
142
+
constchild3=fork('fork-child');
138
143
139
144
// send data to the child process to perform the calculation
0 commit comments