This is a collection of non-portable *nix functions.
Ryan has stated that he does not want anything in node that cannot be ported to windows. So I have started this addon for *nix functions that are commonly found in the standard libraries of other languages.
Feel free to suggest additional functions via pull requests / issue tickets!
To install this package, simply run:
npm install nix
Using this module, forking a running node.js process is as simple as:
var nix = require('nix');
var pid = nix.fork();
if (pid == 0) {
// this is the child process, pid = 0
} else {
// this is the parent process, pid = child pid
}
Calling this after creating and listening on a simple http/tcp server should work as expected for most parts. That is, requests will be randomly distributed to the new child and parent process by the kernel.
However, if you plan on calling this during a tcp/http request, make sure that
you destroy()
this stream in either the parent or the child. Otherwise
both of them will try to watch the socket, causing packets to randomly be
seen by either process.
A binding to fork(2).
Returns 0
for the child process and the pid of the child process for the
parent.
This function will create a new child process that is an identical copy of the parent process. All I/O watchers will be left in-tact, so you need to understand what this means when calling this function during the runtime phase of your program.
This function is blocking and takes ~3-4ms per call in my benchmarks. Making it non-blocking would be rather stupid idea.
Warning: Do not call this function from the repl on OSX, unless you want to trigger a kernel panic.
node-nix is licensed under the MIT license.