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
Copy file name to clipboardExpand all lines: README.md
+60-35Lines changed: 60 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1383,6 +1383,66 @@ Cluster supports two types of load distribution:
1383
1383
<b><a href="#table-of-contents">↥ back to top</a></b>
1384
1384
</div>
1385
1385
1386
+
## Q. How does the cluster module work in Node.js?
1387
+
1388
+
The cluster module provides a way of creating child processes that runs simultaneously and share the same server port.
1389
+
1390
+
Node.js runs single threaded programming, which is very memory efficient, but to take advantage of computers multi-core systems, the Cluster module allows you to easily create child processes that each runs on their own single thread, to handle the load.
1391
+
1392
+
**Example:**
1393
+
1394
+
```js
1395
+
/**
1396
+
* Cluster Module
1397
+
*/
1398
+
constcluster=require('cluster');
1399
+
1400
+
if (cluster.isWorker) {
1401
+
console.log('I am a worker');
1402
+
} else {
1403
+
console.log('I am a master');
1404
+
cluster.fork();
1405
+
cluster.fork();
1406
+
}
1407
+
```
1408
+
1409
+
**Output:**
1410
+
1411
+
```js
1412
+
I am a master
1413
+
I am a worker
1414
+
I am a worker
1415
+
```
1416
+
1417
+
<divalign="right">
1418
+
<b><a href="#table-of-contents">↥ back to top</a></b>
1419
+
</div>
1420
+
1421
+
## Q. Explain cluster methods supported by Node.JS?
1422
+
1423
+
|Method |Description |
1424
+
|---------------|-----------------------|
1425
+
|disconnect() |Disconnects all workers|
1426
+
|exitedAfterDisconnect |Returns true if a worker was exited after disconnect, or the kill method|
1427
+
|fork() |Creates a new worker, from a master|
1428
+
|id |A unique id for a worker|
1429
+
|isConnected |Returns true if the worker is connected to its master, otherwise false|
1430
+
|isDead |Returns true if the worker\'s process is dead, otherwise false|
1431
+
|isMaster |Returns true if the current process is master, otherwise false|
1432
+
|isWorker |Returns true if the current process is worker, otherwise false|
1433
+
|kill() |Kills the current worker|
1434
+
|process |Returns the global Child Process|
1435
+
|schedulingPolicy|Sets or gets the schedulingPolicy|
1436
+
|send() |sends a message to a master or a worker|
1437
+
|settings |Returns an object containing the cluster\'s settings|
1438
+
|setupMaster() |Changes the settings of a cluster|
1439
+
|worker |Returns the current worker object|
1440
+
|workers |Returns all workers of a master|
1441
+
1442
+
<divalign="right">
1443
+
<b><a href="#table-of-contents">↥ back to top</a></b>
1444
+
</div>
1445
+
1386
1446
## Q. How to make use of all CPUs in Node.JS?
1387
1447
1388
1448
A single instance of Node.js runs in a single thread. To take advantage of multi-core systems, the user will sometimes want to launch a **cluster** of Node.js processes to handle the load. The cluster module allows easy creation of child processes that all share server ports.
@@ -1501,41 +1561,6 @@ Please note - before pid. This converts a pid to a group of pids for process kil
1501
1561
<b><a href="#table-of-contents">↥ back to top</a></b>
1502
1562
</div>
1503
1563
1504
-
## Q. How does the cluster module work in Node.js?
1505
-
1506
-
The cluster module provides a way of creating child processes that runs simultaneously and share the same server port.
1507
-
1508
-
Node.js runs single threaded programming, which is very memory efficient, but to take advantage of computers multi-core systems, the Cluster module allows you to easily create child processes that each runs on their own single thread, to handle the load.
1509
-
1510
-
**Example:**
1511
-
1512
-
```js
1513
-
/**
1514
-
* Cluster Module
1515
-
*/
1516
-
constcluster=require('cluster');
1517
-
1518
-
if (cluster.isWorker) {
1519
-
console.log('I am a worker');
1520
-
} else {
1521
-
console.log('I am a master');
1522
-
cluster.fork();
1523
-
cluster.fork();
1524
-
}
1525
-
```
1526
-
1527
-
**Output:**
1528
-
1529
-
```js
1530
-
I am a master
1531
-
I am a worker
1532
-
I am a worker
1533
-
```
1534
-
1535
-
<divalign="right">
1536
-
<b><a href="#table-of-contents">↥ back to top</a></b>
0 commit comments