Skip to content

A Node.js interface to the native fork(2) syscall

Notifications You must be signed in to change notification settings

anttikissa/native-fork

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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())
}

About

A Node.js interface to the native fork(2) syscall

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published