title: Any simple/quick way to fork() on Windows (ANSI C) (_beginthread/_beginthreadex/CreateProcess)
body: <p>Can somebody help me to make this C POSIX code to port it to run under Windows? (no Cygwin, MinGW just Windows native APIs and Visual Studio), tried many things without any luck.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;time.h&gt;
  #include &lt;stdlib.h&gt;
  
  void usage(char *s);
  int test_function(char *host);
  FILE *infile, *outfile;
  int numforks = 0;
  
  void usage(char *s) {
          printf("Usage: %s &lt;input&gt; &lt;childs&gt;\n", s);
          exit(EXIT_SUCCESS);
  }
  
  int main(int argc, char *argv[]) {
          char buf[1024];
          time_t start;
          if (argc &lt; 2)
                  usage(argv[0]);
          outfile = fopen("test.txt", "a+");
          infile = fopen(argv[1], "r");
          printf("[*] In: %s Childs: %s\n", argv[1], argv[2]);
          start = time(0);
          while (!feof(infile)) {
                  fgets((char *)&amp;buf, sizeof(buf), infile);
                  if (buf[strlen (buf) - 1] == '\n')
                          buf[strlen (buf) - 1] = '\0';
                  if (!(fork())) {
                          test_function(buf);
                          exit(0);
                  } else {
                          numforks++;
                          if (numforks &gt; atoi(argv[2]))
                                  for (numforks; numforks &gt; atoi(argv[2]); numforks--)
                                          wait(NULL);
                  }
          }
          fclose(infile);
          fclose(outfile);
          printf("[*] Completed in: %lu secs\n", (time(0) - start));
          exit(EXIT_SUCCESS);
  }
  
  int test_function(char *host) {
    usleep(2000000); // for debugging
    fprintf(outfile, "%s\n", host);
    printf("%s\n", host);
    return 1;
  }
  </code></pre>
  
  <p>I tried to do this:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #ifdef LINUX
    #include &lt;unistd.h&gt;
  #endif
  #include &lt;time.h&gt;
  #include &lt;stdlib.h&gt;
  
  #ifdef WIN32
    #define fork _forkWin32
    #define usleep Sleep
    #include &lt;cstdlib&gt;
    #include &lt;cstring&gt;
    #include &lt;iostream&gt;
    #include &lt;process.h&gt;
    #include &lt;windows.h&gt;
    using   namespace  std;
  #endif
  
  void usage(char *s);
  #ifdef WIN32
  unsigned int _stdcall test_function(void *ptr);
  #else
  int test_function(char *host);
  #endif
  FILE *infile, *outfile;
  int numforks = 0;
  char proc;
  
  void usage(char *s) {
          printf("Usage: %s &lt;input&gt; &lt;threads&gt;\n", s);
          exit(EXIT_SUCCESS);
  }
  
  int main(int argc, char *argv[]) {
          char buf[1024];
          time_t start;
          //#ifdef WIN32
          //HANDLE      child;
          //#endif
          if (argc &lt; 3)
                  usage(argv[0]);
          outfile = fopen("test.txt", "a+");
          infile = fopen(argv[1], "r");
          printf("[*] In: %s Threads: %s\n", argv[1], argv[2]);
          start = time(0);
          HANDLE      child;
          while (!feof(infile)) {
                  fgets((char *)&amp;buf, sizeof(buf), infile);
                  if (buf[strlen (buf) - 1] == '\n')
                          buf[strlen (buf) - 1] = '\0';
                  #ifdef WIN32
                  child = (HANDLE) _beginthreadex(NULL, 0, test_function, buf, 0, NULL);
                  if (child != 0) {
                  #else
                  if (!(fork())) {
                          test_function(buf);
                  #endif
                          exit(0);
                  } else {
                          numforks++;
                          if (numforks &gt; atoi(argv[2]))
                                  for (numforks; numforks &gt; atoi(argv[2]); numforks--)
                                          #ifdef WIN32
                                          WaitForSingleObject(child, INFINITE);
                                          #else
                                          wait(NULL);
                                          #endif
                  }
          }
          fclose(infile);
          fclose(outfile);
          printf("[*] Completed in: %lu secs\n", (time(0) - start));
          exit(EXIT_SUCCESS);
  }
  
  #ifdef WIN32
  unsigned int test_function(void *ptr) {
      char* host = (char*)ptr;
  #else
  int test_function(char *host) {
  #endif
    usleep(2000000); // for debugging
    fprintf(outfile, "%s\n", host);
    printf("%s\n", host);
    return 1;
  }
  </code></pre>
  
  <p>Doesn't seem to do anything. What am I doing wrong? Is it all wrong?</p>