Created
February 4, 2018 04:31
-
-
Save hinzundcode/2a07c424f82f20be035d8a588602807c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const ffi = require("ffi"); | |
const ref = require("ref"); | |
const ArrayType = require("ref-array"); | |
let StringArray = ArrayType("string"); | |
let voidPtr = ref.refType(ref.types.void); | |
let stdlib = ffi.Library(null, { | |
"fork": ["int", []], | |
"execvp": ["int", ["string", StringArray]], | |
"dup2": ["int", ["int", "int"]], | |
"wait4": ["int", ["int", "int", "int", voidPtr]], | |
}); | |
const WNOHANG = 1; | |
process.on("SIGCHLD", () => { | |
console.log("SIGCHLD!"); | |
while (true) { | |
let pid = stdlib.wait4(-1, 0, WNOHANG, ref.NULL); | |
if (pid <= 0) break; | |
console.log(`reaped zombie ${pid}`); | |
} | |
}); | |
let pid = stdlib.fork(); | |
if (pid === 0) { | |
console.log("hello from child"); | |
setTimeout(() => { | |
stdlib.dup2(process.stdin._handle.fd, 0); | |
stdlib.dup2(process.stdout._handle.fd, 1); | |
stdlib.dup2(process.stderr._handle.fd, 2); | |
let result = stdlib.execvp("/bin/echo", ["/bin/echo", "luku", ref.NULL]); | |
console.log("execvp", result); | |
}, 1000); | |
} else if (pid < 0) { | |
console.log("error forking"); | |
} else { | |
console.log("child has pid", pid); | |
setInterval(() => {}, 100000); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment