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:
- 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.
- Click Next and then choose install from internet.
- Set the Cygwin installation directory (best leaves C: cygwin64).
- Choose a directory to store downloaded files.
- Select a site to download.
- 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:
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 :)
by the way there is no ONLY in English.
– tayllan