fork() in Mono

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

fork() in Mono

Gene Thomas
Hello,

I have noticed that in mono/mcs/class/Mono.Posix/Mono.Unix.Native/Syscall.cs fork() is #if'd out with the message "DO NOT directly call fork(2); it bypasses essential shutdown code. Use System.Diagnostics.Process instead".

What happens specifically? fork() starts a process, it does not shut anything down? fork() should just work, does it not (effectively (copy on write)) copy the entire VM which then each shuts down independently? fork() does work in Python on Unix (multiprocessing module).

fork() is powerful, it supports using processes as an alternative to threads. One can not test for a one in a million race condition. System.Diagnostics.Process only replaces fork() then exec(). I wish to use fork() to support replicated processes, starting with a complete copy of the application's state greatly simplifies this.


Thanks,


Gene Thomas.

<a href="tel:+64%2021%20436%20384" value="+6421436384" target="_blank">+64 21 436384
[hidden email]


_______________________________________________
Mono-devel-list mailing list
[hidden email]
http://lists.dot.net/mailman/listinfo/mono-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: fork() in Mono

Robert Jordan
On 04.12.2017 02:23, Gene Thomas wrote:

> What happens specifically?|fork()|starts a process, it does not shut
> anything down?|fork()|should just work, does it not (effectively (copy
> on write)) copy the entire VM which then each shuts down
> independently?|fork()|does work in Python on Unix (multiprocessing module).
>
> |fork()|is powerful, it supports using processes as an alternative to
> threads. One can not test for a one in a million race
> condition.|System.Diagnostics.Process|only replaces|fork()|then|exec()|.
> I wish to use|fork()|to support replicated processes, starting with a
> complete copy of the application's state greatly simplifies this.

You may want to look at fork(2)'s specs, especially to those parts
regarding multithreading.

The forked child will start with one running thread only, etc.

This means that W/out some kind of advanced runtime support
for fork(2), you won't go very far. The GC and other service
threads won't run anymore, etc.

For toying around, you can test fork(2) even w/out Mono.Posix:

---
using System;
using System.Runtime.InteropServices;

class Program {
        [DllImport("libc")]
        public static extern int fork();

        public static void Main()
        {
                int pid = fork();
                if (pid == -1) {
                        Console.WriteLine("fork failed");
                } else if (pid == 0) {
                        Console.WriteLine ("child");
                } else
                        Console.WriteLine ("parent, child's pid={0}", pid);
        }
}
---

Robert

_______________________________________________
Mono-devel-list mailing list
[hidden email]
http://lists.dot.net/mailman/listinfo/mono-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: fork() in Mono

Ludovic Henry (XAMARIN) via Mono-devel-list
Very good observation Robert.

When the original warning was added, Mono was slightly different, it shipped with an IO-layer that added a new set of problems.

The problems were that we used to keep a table of file descriptors in a shared state, and there was no support for handling this properly, so our internal fork/exec code handled this, but not the public fork code.

The existing reason for the comment no longer really applies, but the point that you make certainly will continue to apply.

On 12/4/17, 7:06 AM, "Mono-devel-list on behalf of Robert Jordan" <[hidden email] on behalf of [hidden email]> wrote:

    On 04.12.2017 02:23, Gene Thomas wrote:
    > What happens specifically?|fork()|starts a process, it does not shut
    > anything down?|fork()|should just work, does it not (effectively (copy
    > on write)) copy the entire VM which then each shuts down
    > independently?|fork()|does work in Python on Unix (multiprocessing module).
    >
    > |fork()|is powerful, it supports using processes as an alternative to
    > threads. One can not test for a one in a million race
    > condition.|System.Diagnostics.Process|only replaces|fork()|then|exec()|.
    > I wish to use|fork()|to support replicated processes, starting with a
    > complete copy of the application's state greatly simplifies this.
   
    You may want to look at fork(2)'s specs, especially to those parts
    regarding multithreading.
   
    The forked child will start with one running thread only, etc.
   
    This means that W/out some kind of advanced runtime support
    for fork(2), you won't go very far. The GC and other service
    threads won't run anymore, etc.
   
    For toying around, you can test fork(2) even w/out Mono.Posix:
   
    ---
    using System;
    using System.Runtime.InteropServices;
   
    class Program {
    [DllImport("libc")]
    public static extern int fork();
   
    public static void Main()
    {
    int pid = fork();
    if (pid == -1) {
    Console.WriteLine("fork failed");
    } else if (pid == 0) {
    Console.WriteLine ("child");
    } else
    Console.WriteLine ("parent, child's pid={0}", pid);
    }
    }
    ---
   
    Robert
   
    _______________________________________________
    Mono-devel-list mailing list
    [hidden email]
    https://na01.safelinks.protection.outlook.com/?url=http%3A%2F%2Flists.dot.net%2Fmailman%2Flistinfo%2Fmono-devel-list&data=02%7C01%7Cmiguel%40microsoft.com%7C6e3952171b074542eada08d53b0f7706%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636479859975498069&sdata=PVoqSEnaE8W2MELmBoZSnfCA%2FY93PycZIpw3ULcLTc4%3D&reserved=0
   

_______________________________________________
Mono-devel-list mailing list
[hidden email]
http://lists.dot.net/mailman/listinfo/mono-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: fork() in Mono

Ludovic Henry (XAMARIN) via Mono-devel-list
In reply to this post by Robert Jordan
One last bit:

Nowadays it should be possible to fork() and perform an exec or die quickly, provided you do not need the additional servies like the GC thread.

On 12/4/17, 7:06 AM, "Mono-devel-list on behalf of Robert Jordan" <[hidden email] on behalf of [hidden email]> wrote:

    On 04.12.2017 02:23, Gene Thomas wrote:
    > What happens specifically?|fork()|starts a process, it does not shut
    > anything down?|fork()|should just work, does it not (effectively (copy
    > on write)) copy the entire VM which then each shuts down
    > independently?|fork()|does work in Python on Unix (multiprocessing module).
    >
    > |fork()|is powerful, it supports using processes as an alternative to
    > threads. One can not test for a one in a million race
    > condition.|System.Diagnostics.Process|only replaces|fork()|then|exec()|.
    > I wish to use|fork()|to support replicated processes, starting with a
    > complete copy of the application's state greatly simplifies this.
   
    You may want to look at fork(2)'s specs, especially to those parts
    regarding multithreading.
   
    The forked child will start with one running thread only, etc.
   
    This means that W/out some kind of advanced runtime support
    for fork(2), you won't go very far. The GC and other service
    threads won't run anymore, etc.
   
    For toying around, you can test fork(2) even w/out Mono.Posix:
   
    ---
    using System;
    using System.Runtime.InteropServices;
   
    class Program {
    [DllImport("libc")]
    public static extern int fork();
   
    public static void Main()
    {
    int pid = fork();
    if (pid == -1) {
    Console.WriteLine("fork failed");
    } else if (pid == 0) {
    Console.WriteLine ("child");
    } else
    Console.WriteLine ("parent, child's pid={0}", pid);
    }
    }
    ---
   
    Robert
   
    _______________________________________________
    Mono-devel-list mailing list
    [hidden email]
    https://na01.safelinks.protection.outlook.com/?url=http%3A%2F%2Flists.dot.net%2Fmailman%2Flistinfo%2Fmono-devel-list&data=02%7C01%7Cmiguel%40microsoft.com%7C6e3952171b074542eada08d53b0f7706%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636479859975498069&sdata=PVoqSEnaE8W2MELmBoZSnfCA%2FY93PycZIpw3ULcLTc4%3D&reserved=0
   

_______________________________________________
Mono-devel-list mailing list
[hidden email]
http://lists.dot.net/mailman/listinfo/mono-devel-list