DEV Community

Oyedele Temitope
Oyedele Temitope

Posted on

Understanding the Child Process Module in Node.js

The child process module is significant in Node.js applications as it helps facilitate parallel processing and allows you to offload resource-intensive tasks to separate processes.

In this article, we'll take a look at the child process module, explaining its purpose, use cases, and how to create it.

What is the child process module

The child process is a core module that allows users to create and control subprocesses. These processes can execute system commands, run scripts in various languages, or even fork new instances of Node.js.

The primary purpose of the child process module is to allow the execution of multiple processes simultaneously without blocking the main event loop.

Using a child process is important for applications that need to handle CPU-intensive operations or execute external commands and scripts. With it, your applications can maintain high performance and responsiveness.

Use cases for child processes

The child processes can be used for tasks including the following:

  • Parallel processing: Child processing enables parallel processing by allowing your applications to distribute workloads across multiple CPU cores, considerably improving performance for CPU-intensive activities such as image processing and data analysis.

  • Running shell scripts: Child processes can be used to execute shell scripts. You can use the exec technique to run shell commands and capture their output, and also the spawn method which offers greater control when directly running scripts.

  • Communication with other services: The child processes play a vital role when it comes to communication. It communicates with external services such as databases, APIs, or microservices. They can be used to make calls to external APIs, do database queries, and communicate with other microservices.

    Creating a child process

To create a child process, Node.js provides us with four primary methods for creating a child process which are the exec(),execFile(),spawn(),fork()

The exec() method

The exec() method runs a command in a shell and buffers the output. It is handy for running shell commands and recording the output. However, due to buffering, it has a memory limit.

Below is an example of using exec() method to execute a command that fetches and processes some system information.



const { exec } = require("child_process");

exec("df -h", (error, stdout, stderr) => {
  if (error) {
    console.error(`exec error: ${error.message}`);
    return;
  }
  if (stderr) {
    console.error(`stderr: ${stderr}`);
    return;
  }

  const lines = stdout.trim().split("\n");
  const diskInfo = lines[1].split(/\s+/);

  const totalSpace = diskInfo[1];
  const usedSpace = diskInfo[2];
  const availableSpace = diskInfo[3];
  const usagePercent = diskInfo[4];

  console.log(`Total Space: ${totalSpace}`);
  console.log(`Used Space: ${usedSpace}`);
  console.log(`Available Space: ${availableSpace}`);
  console.log(`Usage: ${usagePercent}`);
});


Enter fullscreen mode Exit fullscreen mode

This will execute the df -h command, parse its output, and display the total, used, and available disk space along with the usage percentage.

When you run the code, you should see something like this:
using the exec method

The execFile() method

The execFile() method runs an executable file without a shell. It’s more efficient than exec() for running binaries and scripts directly since it avoids the overhead of a shell.

Below is an example of how to use the execFile() method:



const { execFile } = require('child_process');
execFile("node", ["--version"], (error, stdout, stderr) => {
  if (error) {
    console.error(`execFile error: ${error.message}`);
    return;
  }
  if (stderr) {
    console.error(`stderr: ${stderr}`);
    return;
  }
  console.log(`stdout: ${stdout}`);
});


Enter fullscreen mode Exit fullscreen mode

This example demonstrates running the Node.js executable to get its version. execFile() is preferred when the command does not require shell features.

The spawn() method

Unlike the exec methods, which are primarily used for executing shell commands or executables and capturing their output, the spawn() method offers more control over child processes through stream-based output handling, direct interaction with executables, event-driven communication.

The spawn() method can be used in complex scenarios where direct interaction is necessary. spawn() launches a new process with a given command and provides streams i.e stdout, stderr for directly handling the process's output and errors.

Below is an example of how to use the spawn() method



const { spawn } = require("child_process");

const ls = spawn("ls", ["-lh", "/usr"]);

ls.stdout.on("data", (data) => {
  console.log(`stdout: ${data}`);
});

ls.stderr.on("data", (data) => {
  console.error(`stderr: ${data}`);
});

ls.on("close", (code) => {
  console.log(`child process exited with code ${code}`);
});


Enter fullscreen mode Exit fullscreen mode

In this example, spawn() runs the ls command with specific arguments and attaches event listeners to handle the process’s output and exit status.

Running the code, you should see something like this:
using the fork() method

The fork() method

The fork() method can be said to be a variation of spawn() designed for creating new Node.js processes. Unlike spawn(), which can launch any type of process, the fork() method is optimized for creating child processes that are themselves Node.js applications.

It provides an additional communication channel into the child process, allowing for simple message transmission between the parent and child processes.

Below is an example of how to use the fork() method.

The parent.js file:



const { fork } = require("child_process");

const child = fork("child.js");

child.on("message", (message) => {
  console.log(`Message from child: ${message}`);
});

child.send("Hello, child process!");


Enter fullscreen mode Exit fullscreen mode

The child.js file:



process.on("message", (message) => {
  console.log(`Message from parent: ${message}`);
  process.send("Hello from child process!");
});


Enter fullscreen mode Exit fullscreen mode

Here, the fork() creates a new Node.js process running the child.js script. It allows message passing between the parent and child process using send() and message events.

When you run the code, you should see something like this:
using the fork() method

Differences between fork() and spawn()

Some of the differences between fork and spawn include:

  • Built-in communication channel: The fork() automatically sets up an IPC (inter-process communication) channel between the parent and child processes. In contrast, spawn() does not establish this channel by default; developers must manually configure IPC if needed.

  • Isolation and independence: Each child process generated by fork() is a separate Node.js process with its own V8 instance. The spawn() method, on the other hand, can launch any process, including Node.js apps, and does not provide the same level of isolation.

  • Use case specificity: The fork() technique is specifically intended for situations in which you need to generate worker processes that are part of the same application but operate concurrently. In contrast, spawn() is more general-purpose and is used when you need to launch arbitrary commands or scripts outside of the Node.js ecosystem.

    Handling child process output

When a child process is created, it produces output in two main streams:stdout and stderr. These streams hold the results of the command or script run by the child process. To collect this output, add event listeners to the child process object for the data event on stdout and stderr.

Node.js provides you with several event listeners that are particularly useful for handling child process output and errors. Some of these include:

  • Data: Data event is emitted by the streams that represent the child process's stdout and stderr.

  • Error:The error event is triggered when an error occurs during the execution of the child process. This could be due to issues with the command itself or problems reading from stdout or stderr.

  • Close:The close event is emitted when the child process ends, indicating whether it terminated successfully or was killed.

Let's take a look at an example that shows how to spawn a child process, capture its output, and handle any errors that may occur.

We'll use the spawn() method to launch a simple command (echo) and listen for output and errors.



const { spawn } = require("child_process");

const child = spawn("echo", ["Hello, world"]);

child.stdout.on("data", (data) => {
  console.log(`stdout: ${data}`);
});

child.stderr.on("data", (data) => {
  console.error(`stderr: ${data}`);
});

child.on("error", (error) => {
  console.error(`Error occurred: ${error.message}`);
});

child.on("close", (code) => {
  console.log(`Child process exited with code ${code}`);
});


Enter fullscreen mode Exit fullscreen mode

In this example, the parent process spawns a child process to execute the echo command, which simply prints Hello, world! to the terminal.

The parent process captures this output through the data event listeners attached to stdout and stderr. Additionally, it listens for the error event to catch any errors that might occur during the execution of the child process.

Finally, the close event indicates when the child process has finished executing, along with the exit code, which can be used to determine if the process was completed successfully.

Communication between processes

In distributed systems and concurrent programming, process communication is critical for component coordination. Node.js improves this communication with its child process module. This allows you to create child processes and facilitate inter-process communication (IPC).

This IPC mechanism, characterized by the exchange of data between parent and child processes, is pivotal for constructing modular and scalable applications.

There are several methods for facilitating; one that stands out is message passing via the send() method and message. This method leverages the send() function available on child process objects to send messages to child processes, and child processes listen for these messages using the message event.

Let's illustrate this with a practical example. We'll create a simple application where a parent process forks a child process and communicates with it using the send() method and message event.

The parent file, which is the parent.js, will look like this:



const { fork } = require("child_process");

const child = fork("./child.js");

child.send({ greeting: "Hello from parent!" });

child.on("message", (msg) => {
  console.log("Message from child:", msg);
});

console.log("Waiting for response...");


Enter fullscreen mode Exit fullscreen mode

The child process file, which is child.js:



process.on("message", (msg) => {
  console.log("Message received from parent:", msg);

  process.send({ response: "Hello from child!" });
});


Enter fullscreen mode Exit fullscreen mode

In this example, the parent process sends a message containing a greeting to the child process. The child process listens for this message, logs it, and then sends a response back to the parent process. The parent process also listens for incoming messages from the child process and logs them upon receipt.

This pattern of using send() and message events for IPC is particularly useful in scenarios where you need to coordinate actions between parent and child processes, such as sharing data, triggering actions, or implementing request-response patterns in a Node.js application.
send and message

Best practices for managing child processes

Below are some of the best practices used to manage the child process effectively:

  • Monitor system resources: one of the best practices is to regularly monitor the system's CPU, memory, and network usage to identify potential bottlenecks or resource leaks. Utilizing tools like top and ps can provide you with real-time insights into resource consumption.

  • Set timeouts for long-running processes: Use the timeout option in spawn() to automatically terminate child processes after a specified duration. This is beneficial for avoiding resource hogging by long-running activities.

  • Implement graceful shutdown procedures: Graceful shutdown procedures is when all requests to the server have been responded to, and no data processing work remains. Always ensure that child processes can be shut down gracefully, releasing resources and performing any necessary cleanup. This can be achieved by listening to termination signals and responding accordingly.

  • Apply the kill method: Use appropriate signals (e.g., SIGTERM for graceful shutdown, SIGKILL as a last resort). Verify that the process has been terminated successfully. Handle potential errors when applying the kill method.

Child process vs workers

While child processes are useful for some jobs, Node.js also provides another method for parallel execution. This method is known as workers, which is entirely different from the child process.

The worker threads module was introduced in Node.js v10.50. It provides a more efficient way to handle multithreading compared to child processes. Below is a table highlighting their differences

Feature Worker threads Child processes
Memory Isolation Shares memory space with the parent process Each child process has its own memory heap (complete isolation)
Communication Uses message passing with less overhead (via SharedArrayBuffer) Communicates via IPC channels with more overhead
Performance More lightweight, lower overhead for frequent data exchange Higher overhead, especially for frequent communication
Use Cases Parallel computations, tasks requiring frequent data exchange CPU-intensive tasks, running external scripts, isolated tasks
Error Isolation Errors can affect the main thread Errors in child processes do not affect the parent process
API Module worker_threads child_process
Creation Method new Worker() spawn(), fork(), exec(), execFile()
Multithreading True multithreading with shared memory Separate processes, no true multithreading
Overhead Lower due to shared memory Higher due to complete process creation
Resource Management Managed within the same process Separate resource allocation for each process

When to use the child process

Thechild process can be used when you need complete isolation, such as running external scripts or managing CPU-intensive tasks that could potentially crash the parent process.

When to use workers

The worker threads, on the other hand, can be used for tasks that benefit from shared memory and require frequent communication, such as data processing and parallel computations.

Conclusion

The child process module helps enable the execution of parallel tasks, running external scripts, and managing subprocesses efficiently. When you understand these methods and how to implement them, you will be able to significantly enhance your application's performance and scalability.

Top comments (0)