0

Under Unix-y systems (by unix-y I mean those that implement POSIX fork & exec, but no other process creation mechanism), processes are created by forking and exec-ing the target executable. However, it is well-known that forking threaded applications is a bad idea. Since all Mono applications are multithreaded by the virtue of the GC running in its own thread(s), how does Mono handle Process.Start()?

I've dug a bit in the codebase, but all I could find is a regular fork & exec. What's happening to the locks? Does the runtime do some thread magic?

mostanes
  • 149
  • 6
  • The caveat about blending threads and processes concerns ongoing activity in each; for example you have 3 threads, fork, and expect the 3 threads in the child to function seemlessly. The magic to make that happen is pthread_atfork(3), but magic needs to be summoned, and often it isn't. A simple fork() from a multi-threaded program that does not interact with the thread environment can be expected to work -- so fork() + exec() is typically ok. Mono (or other frameworks) may be able to interfere with this, but I can't imagine why it would. – mevets Sep 22 '20 at 15:16
  • Between fork and exec, one needs to call various helper functions for things like redirecting stdio or doing tracing. But even memory allocation requires locks (https://stackoverflow.com/questions/4524437/in-multithreaded-c-c-does-malloc-new-lock-the-heap-when-allocating-memory). – mostanes Sep 24 '20 at 05:21
  • For example, the code call mono_trace in the child, which, under tracing (https://github.com/mono/mono/blob/3c8a5e775073f93cabeac0990a264ce1995eb396/mono/utils/mono-logger-internals.h) calls mono_tracev_inner (https://github.com/mono/mono/blob/3c8a5e775073f93cabeac0990a264ce1995eb396/mono/utils/mono-logger.c), which calls g_vasprintf, which allocates memory: https://people.gnome.org/~ryanl/glib-docs/glib-String-Utility-Functions.html#g-vasprintf – mostanes Sep 24 '20 at 05:23

0 Answers0