Home

Child Processes

Node includes the child_process module.
Processes exist in node. This child_process module allows a dev to create NEW processes! The new processes are part of the parent process that created the child_process (hence the "child").

Flexible Processes

Child_processes run commands.
These can be any executable command in any language!. Epic.
The only exception here is thatt the fork api runs a node process, specifically.

Many Ways To Create A Child_Process

More on these below, but here's the node doc links on each:

Exec, Spawn, And Fork

Thanks to the node docs for much more details on these topics!
These 3 types of apis that node provides are ways to create child processes. They each have nuanced differences.

Exec

Exec spawns a shell.
Exec then runs a command passed to that shell.
Exec can buffer the data run by the child process and then send the data back to the parent process on completion. This can be done with a callback function.

ExecFile

This is similar to exec.
This does not spawn a shell (shell can be enabled with an option key/val pair though). Without the shell, this is slightly more efficient than exec.

Spawn

This does not spawn a shell.
This streams data to the parent process via eventEmitter events.
There is no limit to the data that can be streamed to the parent process.

Fork

Fork is like spawn.
Fork spawns node processes.
Fork, maybe as the most unique approach to child processes, includes the send method to pass data from the parent process to the forked process.
Forks have their own memory & v8 processes separate from the parent process. This can make forks more expensive than other child-process methods.
This fork method seems to me to be, by far, the fastest way to manage some async node data workflows between a parent and a child or set of child processes.
Perhaps a write-up comparing the two could help me prove this case. (if that seems interesting to you as a reader, let me know!).

Blocking Version

There are synchronous versions of the exec and spawn methods, execSync and spawnSync.
The fundamental difference in these apis is that they block the event loop

Programs, Processes, and Threads

Thanks to bytebytego for a great rundown of this -

A Program is an executable file. Like a node server. Stored as a file on disk.

A Process is a running program. The program gets executed by the processor. Each process is independent of other processes: one process can't "corrupt" another process. When one process fails or errors, another process will continue, unaffected by the failed/err'd process.

A Thread is a part of a process. Processes have at least 1 thread, the "main" thread. When a program and a process use only the main thread, it could be considered "single-threaded".
In the context of node, this "single-threaded" approach is how node is most commonly talked about.
It is not uncommon for a process, though, to have many threads. Threads within a process share memory. inter-thread-communication can happen. Because of this connection between threads and the "parent" process, if a thread errors the parent process can also encounter errors.

Tags: