Loading, please wait...

Node.js Child Processes

Node.js runs in a single-thread mode, however, it makes use of an event-driven paradigm to take care of concurrency. It also helps the introduction of the child process to leverage parallel processing on multi-core CPU primarily based systems. We can without difficulty spin a child technique using Node’s child procedure module and these child process can easily communicate with every different with a messaging system. The child process module permits us to get entry to Operating System functionalities by using running any device command internal as, well, child process. We can manipulate the child procedure input stream and pay attention to its output stream. We can also manipulate the arguments to be passed to the underlying OS command, and we can do anything we prefer with that command’s output

There are four ways to create a child process in Node:

  • spawn()
  • fork()
  • exec()
  • execFile().

Spawn (): The spawn function launches a command in a new process and we can use it to pass that command any arguments.it has the following syntax:

Parameters are following:

  • command(String) The command to run
  • args(Array) List of string arguments
  • options(Object) may comprise one or more of the following options.
  • cwd(String) Current working directory of the child process.
  • env(Object) Environment key-value pairs.
  • stdio(Array) String Child's stdio configuration.
  • customFds(Array) Deprecated File descriptors for the child to use for stdio.
  • detached(Boolean) The child will be a process group leader.
  • uid(Number) Sets the user identity of the process.
  • gid(Number) Sets the group identity of the process.

The spawn() method returns streams(stdout & stderr) and it should be used when the process returns a volume amount of data. Spawn() starts receiving the response as soon as the process starts executing. As shown in an example

  • Create two js files named app.js and text.js-

File: app.js

File: master.js

Run the above code:

Every child process also gets the three standard stdio streams, which are child.stdin  child.stdout , and child.stderr.

             The fork() Method

A child_process .fork method is a special case of spawn () to create Node processes. The fork function is a variation of the spawn function for spawning node processes. The main difference between spawn and fork is that a communication channel is established to the child process when using fork, in this case, we can use the send function on the forked process along with the global process object itself to exchange messages between the parent and forked processes.

Syntax:

Parameters are as follows:

  • modulePath(String) The module to run in the child.
  • args(Array) List of string arguments
  • options(Object) may comprise one or more of the following options −
  • cwd(String) Current working directory of the child process.
  • env(Object) Environment key-value pairs.
  • execPath(String) Executable used to create the child process.
  • execArgv(Array) List of string arguments passed to the executable (Default: process.execArgv).
  • silent(Boolean) If true, stdin, stdout, and stderr of the child will be piped to the parent, otherwise they will be inherited from the parent, see the "pipe" and "inherit" options for spawn()'s stdio for more details (default is false).
  • uid(Number) Sets the user identity of the process.
  • gid(Number) Sets the group identity of the process.

The fork method returns an object with a built-in communication channel in addition to having all the methods in a normal Child Process instance.

EXAMPLE

File: app.js

File: master.js

Run the above code node master.js

The exec () method

child_process.exec method runs a command in a shell and buffers the output.  By default, the spawn function does not create a shell to execute the command we pass into it. This makes it slightly more efficient than the exec function, which does create a shell. The exec function has one other major difference. It buffers the command’s generated output and passes the whole output value to a callback function.

It has the following signature –

Parameters used are

  • command(String) The command to run, with space-separated arguments
  • options(Object) may comprise one or more of the following options −
  • cwd(String) Current working directory of the child process
  • env(Object) Environment key-value pairs
  • encoding(String) (Default: 'utf8')
  • shell(String) Shell to execute the command with (Default: '/bin/sh' on UNIX, 'cmd.exe' on Windows, The shell should understand the -c switch on UNIX or /s /c on Windows. On Windows, command line parsing should be compatible with cmd.exe.)
  • timeout(Number) (Default: 0)
  • maxBuffer(Number) (Default: 200*1024)
  • killSignal(String) (Default: 'SIGTERM')
  • uid(Number) Sets the user identity of the process.
  • gid(Number) Sets the group identity of the process.
  • callbackThe function gets three arguments errorstdout, and stderr which are called with the output when the process terminates.

The exec() method returns a buffer with a max size and waits for the process to end and tries to return all the buffered data at once.

EXAMPLE

File: node support.js

File: master.js

Run the above code: