0

I need to be able to use fork() for a small project. The thing is that the example code is not working:

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main()
{ 
    pid_t pID = fork();
    if (pID == 0) // child
    {
        printf("CHILD\n");
        // Code only executed by child process
    }
    else if (pID < 0) // failed to fork
    {
        printf("FAIL\n");
    }
    else // parent
    {
        printf("PARENT\n"); // Code only executed by parent process
    }
    // Code executed by both parent and child.

    system("PAUSE");
    return 0;   
}

The compiler says: "20 D:\Untitled1.cpp `fork' undeclared (first use this function)"

But I have read in the internet that it should be located in #include <unistd.h>.

Any ideas? Thanks!

Inisheer
  • 20,376
  • 9
  • 50
  • 82
Oliver Hoffman
  • 540
  • 1
  • 9
  • 22

1 Answers1

6

On Windows you can't use fork(). Use CreateProcess()/CreateProcessEx() instead.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Mayhem
  • 396
  • 1
  • 10