-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfork.js
52 lines (35 loc) · 1.16 KB
/
fork.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const {fork, wait, read, write, pipe, close} = require('./build/Release/fork');
module.exports.fork = function() {
pid = fork();
return pid == 0;
}
module.exports.wait = wait;
module.exports.read = read;
module.exports.write = write;
module.exports.pipe = pipe;
module.exports.close = close;
if (process.argv[2] === "test") {
const before_pipes = Date.now();
const pipes = pipe();
const before_fork = Date.now();
if (fork()) { // parent
console.log("Fork took ", Date.now()-before_fork);
console.log("Pipe took", before_fork-before_pipes)
close(pipes.write_end);
console.log("In parent");
message = read(pipes.read_end);
console.log("After read: ", Date.now());
console.log("Waiting");
wait();
close(pipes.read_end);
console.log("Waited");
console.log(message);
} else { // child
close(pipes.read_end);
console.log("In child");
console.log("Before write: ", Date.now());
write(pipes.write_end, "Hello from child");
console.log("Sent");
close(pipes.write_end);
}
}