[erlang-questions] How to fork/clone a process in Erlang?
Fri Sep 7 07:26:07 CEST 2012
"I'm just playing around Erlang and wondering if it is possible to
have this functionality."
Since Erlang is Turing-complete, the answer to any such question is
always going to be "yes."
On Fri, Sep 7, 2012 at 1:31 PM, <> wrote:
> Erlang processes offer isolation like UNIX processes (as long
> as you don't link in any C code, which could do arbitrary damage),
> but they are more like threads. In fact they are more like threads
> than threads are, being smaller, cheaper to create, and safe (no
> more *having* to guess how big to make the stack and being at the
> mercy of Cthulhu if you guess wrong!).
> There isn't any 'fork()' to copy the current process because
> *not* copying a whole lot of stuff you'll never want is part
> of what Erlang processes are all about.
> Having said this, it would be tedious rather than difficult to
> provide a continuation-passing transformer for Erlang:
> f(a, Y, b) -> Y;
> f(X, Y, Z) -> P = g(X, Y), h(P, Z).
> f(a, Y, b, K) -> K(Y);
> f(X, Y, Z, K) -> g(X, Y, fun (P) -> h(P, Z, K) end).
> and then your fork() isn't that hard:
> fork(K) -> K(spawn(fun () -> K(child) end)).
> in the library for the continuation passing level, then
> at the normal level
> Which = fork()
> would return 'child' in the child and the child's Pid in the parent.
> But I am having a really hard time figuring out what you would
> want this for. I've been using UNIX since October 1979 and I've
> personally never had any use for a fork() that wasn't followed
> by an exec(). I'm aware that people did this to create workers
> in a process that listens for connections and then forks, but
> these days people seem to prefer using threads for that.
> So what is it you *really* want to do that makes fork()
> seem like a good way to go?
> erlang-questions mailing list
Shinjuku-ku Tokyo 169-0075
"Love does not consist in gazing at each other, but in looking outward
together in the same direction." -- Antoine de Saint-Exupéry
More information about the erlang-questions