Fork in Windows

Asked

Viewed 2,560 times

16

There is something in Windows like (or some alternative similar to) fork of the POSIX (UNIX and Mac) systems to create a child process that is an exact copy of the parent and runs from the call point?

As far as I know CreateProcess (closest that I know) does neither of these two things, as it creates any process (informed via parameters) and executes it from the beginning (as a new same call).

2 answers

8


One of the biggest difficulties in porting Unix programs to Windows is precisely the process model of the two operating systems. Windows does not have the Fork call.

Depending on how Fork is used in the program, it can be replaced by Createprocess, trying to get around the differences of the two calls. In other cases, a Unix application that creates copies of itself can be modeled as a single multi-threaded process on Windows with the call Createthread.

An easier way would be to use the Cygwin library, which provides the functionality of the POSIX API with the cygwin1.dll DLL, including the Fork call.

To use the library just run the following steps:

  1. Log in https://cygwin.com/index.html and download the file setup-x86.exe or setup-x86_64.exe depending on your system. Run the installer.
  2. Click Next and then choose install from internet.
  3. Set the Cygwin installation directory (best leaves C: cygwin64).
  4. Choose a directory to store downloaded files.
  5. Select a site to download.
  6. When you arrive at the Select Packages option type gcc and select gcc-core and gcc-g++. The selection is made by clicking on Skip as shown in the figure:

inserir a descrição da imagem aqui

After installing Cygwin enter the folder in which it was installed, in my case C: cygwin64 home Sergio and put the code you want to compile there. Open Cygwin and Compile the program normally using the Fork call with gcc.

Below is a very simple code from an example of using Fork:

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

int main(int argc, char **argv)
 {
    printf("Iniciando o programa\n");
    int i, j, contador = 0;
    pid_t pid = fork();

    if(pid == 0){
        for (i=0; i < 5; ++i)
             printf("Processo filho: contador=%d\n", ++contador);
    }
    else if(pid > 0){
        for (j=0; j < 5; ++j)
             printf("Processo pai: contador=%d\n", ++contador);
    }
    else{
        printf("fork() falhou!\n");
        return 1;
    }

    printf("Programa terminou\n");
    return 0;
 }

In my case I compiled and executed so:
gcc a.c -o hello
./hello

With Cygwin you can even mix Unix calls with Windows calls, with some limitations. But remember, the process model of the two operating systems is quite different and therefore calls to Fork on Windows through Cygwin will be quite slow!

I hope it helped :)

  • 1

    Helped. Do you know if you can call Fork directly from the code compiled in Windows (with Visual Studio, for example)? Maybe it was only a matter of importing the cygwin1.dll DLL you mentioned, wasn’t it? If this were possible, it would be transparent to the application user the need for Cygwin behind the scenes.

  • Well, what I know you can do is a Makefile project in Visual Studio that will call a Makefile that you made yourself, so you can compile with the g++ of Cygwin, but it would not be a very useful solution. I don’t know if I got it straight, what did you mean by compiled code? After compiling the code, you can’t use Cygwin anymore! And if you use before compiling it, cygwin1.dll will conflict with Visual Studio dlls, and this I think will be boring to resolve.

  • You’re right. gcc in Cygwin probably generates one. exe anyway, so from the user’s point of view gives in anyway (it does not necessarily need to open Cygwin before running the application - that was my concern). :)

  • 1

    Yes, gcc in Cygwin will generate an . exe! For it to run out of Cygwin just add the cygwin1.dll that will be in C: cygwin64 bin in the same executable folder, or in the Windows path system. And it becomes transparent to the user!

1

In the Windows API there is nothing resembling Fork(). But the Cygwin library implements Fork() as closely as possible to the POSIX standard.

  • And you would have an example of use in C++ of fork no/with Cygwin?

  • Unfortunately not, but Cygwin as a whole is open source, so it should be easy to find.

  • 4

    I have no doubt it’s easy to find. But the point is that it would make it a lot easier (not just for me, but also for other users who are interested in the subject), and would greatly improve their response. That’s why I requested it. If you complement it, you get my +1. :)

  • Things have changed in the last few days with this "Ubuntu on Windows". Surely this layer offers a more faithful implementation of Fork() than Cygwin.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.