-1

I have a C sample program that I need to convert to C#, but I don't know how.

Here's the sample code:

#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    printf("--beginning of program\n");

    int counter = 0;
    pid_t pid = fork();

    if (pid == 0)
    {
        // child process
        int i = 0;
        for (; i < 5; ++i)
        {
            printf("child process: counter=%d\n", ++counter);
        }
    }
    else if (pid > 0)
    {
        // parent process
        int j = 0;
        for (; j < 5; ++j)
        {
            printf("parent process: counter=%d\n", ++counter);
        }
    }
    else
    {
        // fork failed
        printf("fork() failed!\n");
        return 1;
    }

    printf("--end of program--\n");

    return 0;
}
Frxstrem
  • 38,761
  • 9
  • 79
  • 119

2 Answers2

4

The Windows equivalent to fork is using threads, so your code is trivially written in C# as follows:

new Thread(new ThreadStart(()=>
{
  for(int i=0;i<5;++i)
    Console.WriteLine("child process, counter="+i);
})).Start();

for(int i=0;i<5;++i)
  Console.WriteLine("parent process, counter="+i);
Blindy
  • 65,249
  • 10
  • 91
  • 131
  • Threads are indeed the most realistic way to implement "fork-like" functionality on Windows. More precise implementation requires serious effort working with CreateProcess (and likely lower level methods) - some links can be found http://stackoverflow.com/questions/985281/what-is-the-closest-thing-windows-has-to-fork/985525#985525 – Alexei Levenkov May 30 '15 at 00:56
  • 1
    @Alexei, That has to be the most backwards thing I've ever seen, emulating `fork` on Windows when you have proper threading support... I get why they need it in Cygwin, but chasing after it when you *can* write threaded code is extremely weird. – Blindy May 30 '15 at 01:00
2

The posix fork method creates a copy of the current address space as a new process and returns either the new process id (to the original process that called fork) or 0 to the newly-created process. Execution continues at the same place in both cases. There is no direct equivalent of this in C# or any other language that does not use posix-compatible libraries.

In C# we use threads as Blindy has already pointed out. The significant difference between threads and fork is that with threads you have a single process with a single memory space. That means that when you change a shared variable it changes for all threads. With a forked process each process gets its own memory space and any changes you make in that memory space are not automatically reflected in the other processes.

There are other differences, but that's probably the biggest one I can see. Creating multiple processes has some overheads and limitations that don't necessarily appear with multiple threads, not least of which is that if you're just starting a new process to do a small operation it is going to have the entire content of your program within that process. Threads can be simple or complex without much impact on the memory space of their process.

Corey
  • 15,524
  • 2
  • 35
  • 68