C++ fork module
npm install @gabictrlz/c-forkc-fork creates a new process by duplicating the calling process.
The new process is referred to as the child process. The calling
process is referred to as the parent process.
The child process and the parent process run in separate memory
spaces. At the time of fork() both memory spaces have the same
content.
For more information check out the fork(2) man page
``bash`
npm i @gabictrlz/c-fork
* Returns: { number } Returns the child_process pid to the parent and 0 to the child
`javascript
import { cfork } from '@gabictrlz/c-fork'
console.log('should print this once') // you'll get this log only once
const pid = cfork.fork()
console.log('forked pid: ' + pid) // you'll get this log twice, for the parent and child
`
* pid { number } The pid of the child process
* Returns: { number } 0 if the child process is running, otherwise returns the terminating signal number
`javascript
import { cfork } from '@gabictrlz/c-fork'
const pid = cfork.fork()
if (pid === 0) {
// ... do something
} else {
console.log('parent is running')
cfork.isRunning(pid) // returns true if child is running, false otherwise
}
`
* pid { number } The pid to kill
* Returns: { void }
`javascript
import { cfork } from '@gabictrlz/c-fork'
const pid = cfork.fork()
if (pid === 0) {
// ... do something
} else {
console.log('parent is running')
cfork.kill(pid) // sends SIGTERM to child
}
`
* code { number } The exit code, either 0 or 1
* Returns: { void }
`javascript
import { cfork } from '@gabictrlz/c-fork'
cfork.exit(0) // kills the current process with exit code 0 or 1
`
* pid { number } The pid of the child processtimeout
* { number } The timeout in milliseconds
* Returns: { Promise< number > } Returns a promise that resolves to the exit code of the child process
This function returns a promise that resolves when the child process has settled.
In case the child process has thrown an error, the promise will reject with the error.
The promise will also reject in case of timeout.
`javascript
import { cfork } from '@gabictrlz/c-fork'
const pid = cfork.fork()
if (pid === 0) {
// ... do something
}
else {
await cfork.waitForChildToSettle(pid, 10_000) // wait for child
console.log('child exited successfully')
}
`
* fn { T extends () => void } The pid of the child processtimeout
* { number } The timeout in milliseconds
* Returns: { Promise< void > } Returns a promise that resolves if all was successful, throws an error if child failed or timeout reached
This function allows you to run a specific function in the child process, allowing you the ability to terminate this function midway.
The runFunctionInChild wraps all of the annoying boilerplate for you and provides a clean API.
This is the main core of c-fork, as of before, if you fired a promise you had no way to stop it in the middle, it'll run until it settles
`javascript
import { cfork } from '@gabictrlz/c-fork'
const pid = cfork.fork()
cfork.runFunctionInChild(async () => {
// all of your logic goes here, all the async, sync, everything
}, timeout)
``
Pull requests are welcome!