views:

262

answers:

3

Is there any way to implement Erlang-style light-weight processes in .NET?

I found some projects that implement Erlang messaging model (actors model). For example, Axum. But I found nothing about light-weight processes implementation. I mean multiple processes that run in a context of a single OS-thread or OS-process.

A: 

This makes no sense. "Multiple process that run in the context of a single OS thread or OS process" is logically inconclusive. THis is basically a logical application level thing - which you can easily repro in .NET. But there is no such thing like a "process within a process" on the OS level.

TomTom
"process within a process" - Win32 have a fibers.
W55tKQbuRu28Q4xv
But fibers are self-scheduled - THREADS (and make no sense in MOST cases). A Process is defined as having a memory barrier, which a fiber lacks. So, a fiber is by no means a process within a process.
TomTom
You clearly haven't looked at Erlang's runtime design.Yes, you can implement Erlang-like processes at the application level, but that is likely to amount to cooperative multitasking, a technique I haven't seen used since my DOS/Win16 days. If you have support in the runtime, you can get this effect with preemptive multitasking, but it would be something MS would have to add to .NET.
Warren Young
Have a look at http://stackoverflow.com/questions/2708033/technically-why-is-processes-in-erlang-more-efficient-than-os-threads
Jonas
+2  A: 

The CLR can be hosted and exposes mechanisms for the host to implement its own task abstraction. Theoretically, they can be threads, fibers, LWPs - anything as long as the host implements the necessary interfaces.

Getting it right is somewhat hard. MS took a chance on it in order to host the CLR in SQL Server Fiber Mode.

At the last moment, there were some stress bugs, so they pulled the plug on it according to Joe Duffy and Dino Vhieland (who ran a series about writing a custom CLR host that implements its own task abstraction - with fibers - on his blog).
Right now there is some plumbing missing - ICLRTask::SwitchOut() - and even if one gets around that, the same bugs that hit MS at the stress test iteration would probably haunt the adventurous soul as well.

Suppose for a moment that all the problems are somehow fixed and the whole runtime is prepared to run on fibers, LWPs, whatever. There is still the problem of P/Invoke that might potentially call into blocking operations. This kind of scheduling is hard to do without kernel support.

This is addressed in the 64bit versions of Windows 7 and Windows 2008 Server R2. There is now User-Mode Scheduling that can yield control back to a user-mode - as opposed to kernel-mode - scheduler if a call blocks in the kernel. Also, these user-mode scheduled threads are real threads with their own TLS. These are great improvements and make many of the fiber mode headaches go away.

Right now, UMS is utilized in the Concurrency Runtime that is available for C++ and is part of the C Runtime Library (CRT).
The latter means that you can use it out of the box with Visual Studio 2010.

andras
Does some of this go away if you limit its use to F# or another functional language, so as to avoid some of the difficulties with one process mucking with another's storage?
Warren Young
@Warren: The task management here does not really concern itself with preventing one thread/fiber/LWP writing to the memory of another. After all, it is intended behavior - you do this every time you start a multi-threaded program - all threads access and share the memory of the process.
andras
@Warren: ...with that said, avoiding or minimizing shared state is one of the great ways to increase parallelism. It can be done in C# as well - though the language does not enforce or specifically encourage it.
andras
+2  A: 

Have You had a look at retlang? I only read about it, but I didn't do anything with it, yet...

bsmr