views:

188

answers:

3

Since C# supports threading, is there any way to implement fork concept in C#?

Thanks in advance....

+2  A: 

Please see below post

saurabh
+4  A: 

This is more a matter of .NET / CLR than of C#. Generally, it's a matter of the underlying operating system. Windows do not support fork()-like semantics of spawning new processes. Also, fork() has nothing to do with multithreading support.

The semantics of fork() involves duplicating the contents of the original process's address space. My opinion is this is an obsolete approach to process creation and has barely any room in the Windows world, because it involves a lot of security and operating system architecture concerns.

From the .NET point of view, the fundamental problem with fork() would be the approach to duplicating and/or sharing unmanaged resources (file handles, synchronization objects, window handles (!), etc.) between the old and the new process. I think there is no serious reason to introduce such concept either to .NET or to the underlying Windows operating system.

For further discussion see saurabh's link.

Ondrej Tucny
A: 

Try out this one, I am not sure this is actually forking but it creates two different threads in the task manager. But this code is not stable. Help me out to make this code stable.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace ConsoleApplication2
{
class Program
{
    static void Main(string[] args)
    {

        int rc;

        fork();
    }

    public static int fork()
    {
        int pid = Process.GetCurrentProcess().Id;
        int val = 0;

        try
        {
            Console.WriteLine("Pid {0}", pid);

            string fileName = Process.GetCurrentProcess().MainModule.FileName.Replace(".vshost","");

            ProcessStartInfo info = new ProcessStartInfo(fileName);

            info.UseShellExecute = false;

            Process processChild = Process.Start(info);

            val = 1;
        }
        catch
        {
            val = 0;
        }

        return val;

    }

   }
}
wiz kid