[erlang-questions] how are erlang process lightweight?

Richard Carlsson richardc@REDACTED
Tue Oct 3 13:03:28 CEST 2006


Ben Dougall wrote:
> I'm trying to find out how Erlang processes work / what they're made
> of.

As Ulf said, chech out the discussion at LtU, but here are
some quick answers to fill in the blanks:

> They stop processing when they come to a point where they're waiting
> for a message or after they've been processing for a certain amount
> of time. All this is handled by the language/runtime system and
> happens within user space.

Yes. Process switching is not triggered by real time, but by a counter
which is decremented at (approximately) each function call. (Using
interrupts has too much overhead.)

> Each process is very lightweight. I'm struggling to find out how 
> they're lightweight. They're stackless? Is that correct?

No, each process has a stack. But the stack is initially very small
(on the order of 16-32 words, I think), and grows as needed.

> What happens to a process's local variables when it's switched out of
> control (due to coming to a point where it's waiting for a message or
> reached its time limit)? Each process must have local variables don't
> they? So what happens to them? Where do they get stored? If you have
> a set of local variables per process then how is each process
> considered so lightweight?

Of course processes have local variables, and these are stored on the
stack while the process is switched out. How much memory is used (stack
plus heap) per process depends completely on what the process does and
how it was written. Processes which only receive messages and respond
to them quickly before going back to sleep, and which do not need to
keep track of state between messages, might never need to grow their
stacks at all, and might only use heap memory temporarily.

> if you have hundreds of thousands of processes that's not so
> lightweight is it?

Yes it is, compared to threads which typically need at least a few K of
allocated stack regardless of whether it will be used. Thread
implementations have become better in recent years, but Erlang processes
are still lightweight any way you count.

> Also how do processes correspond to functions? Is it just one
> function per process? Processes and functions are very much tied up
> with each other; one to one?

Functions are just program text. Just like several people can read from
the same piece of paper, several processes may run the same code
simultaneously (and usually do). For example, a single function might
implement the main loop of a server process, with 100.000 process
instances running that loop at the same time (and not all of them being
at the same program point).

	/Richard




More information about the erlang-questions mailing list