native-fork

0.0.3 • Public • Published

native-fork

Exposes the POSIX syscall fork(2).

Only works in systems that have the said syscall.

Use at your own risk.

const fork = require('native-fork')
const pid = fork()

if (pid) {
	console.log('I am the parent, child is', pid)
} else {
	console.log('I am the child')
}

Caveats

Calling fork(2) probably breaks a lot of assumptions that Node.js makes and is therefore likely to break things. For example, in the child process, process.pid still points to the parent's pid, as demonstrated by this code:

let fork = require('native-fork')
let cp = require('child_process')

let pid = fork()

if (pid) {
	console.log('parent: I am the parent, child is', pid)
	console.log('parent: Node.js thinks my pid is', process.pid)
	// Get the real pid by spawning a shell and asking it to tell its parent's pid:
	console.log(cp.execSync('echo "parent: actual pid is $PPID"').toString().trim())
} else {
	console.log('child: I am the child')
	console.log('child: Node.js thinks my pid is', process.pid)
	console.log(cp.execSync('echo "child: actual pid is $PPID"').toString().trim())
}

Readme

Keywords

none

Package Sidebar

Install

npm i native-fork

Weekly Downloads

1

Version

0.0.3

License

MIT

Unpacked Size

2.27 kB

Total Files

8

Last publish

Collaborators

  • anttikissa