emscripten-core/emscripten

fork() support?

IngwiePhoenix opened this issue · 4 comments

Example code: http://timmurphy.org/2014/04/26/using-fork-in-cc-a-minimum-working-example/

Running this with emcc currently gets me the message that it can't fork. I know that in theory nodejs doesn't have actual forking support... but is there a chance to implement it via a mix of child_process+longjmp?

Kind regards, Ingwie

That sounds quite hard to do. A proper fork needs to do special things with file descriptors, the state of execution, etc., all of which are not practical to support in JS. In practice, I've refactored codebases to avoid fork.

I see. Well I guess I'll have to do similar. The way fork is used in my end is by a 3rd-party lib which does subprocess handling. Looks like Ill have to give this lib a little makeover with emscripten support, using the child_process APIs.

Thanks for your insights, though! :)

juj commented

The thing that makes this impossible is that the forked processes are supposed to start executing on the next statement after the fork() call. This is impossible in the current features of JavaScript (unless we use something like emterpreter to emulate code execution), since we are not able to duplicate the callstack of the parent to the forked children so that they'd be executing at the same exact functions and local values on their stack.

Emscripten does have pthreads support for Firefox Nightly and Chrome Canary, so if you don't specifically need a subprocess, but a new thread might do, then you could try using that support instead. It will be a while until that support is mature though and browsers are ready to ship.