1

I am trying to use the fork() and wait() system calls in C++.

My code is really simple. However I get the following error:

error C3861: 'fork': identifier not found 

I have included the following header files. Do I have to include some other headers here? What is it that I am doing wrong?

#include<stdafx.h>
#include <sys/types.h>
#include <signal.h>

int main(){

    if(fork()==0)
    {
        printf("from child");
    }
    else
    {
        printf("from parent");
    }
}
Mat
  • 202,337
  • 40
  • 393
  • 406
user457660
  • 91
  • 1
  • 3
  • 9
  • What operating system are you using? On Windows, fork() doesn't work. Try using cygwin. – Alan Apr 02 '11 at 03:03
  • http://stackoverflow.com/questions/15393218/error-c3861-tcsdup-identifier-not-found -- check here if this can help you somewhat...! – rk_12 Jan 14 '15 at 11:06

2 Answers2

9

Normally, you also need the following to get fork():

 #include <unistd.h>

However, you seem to be using Windows and fork() isn't available on Windows. This page discusses a Windows work-around.

One of the largest areas of difference is in the process model. UNIX has fork; Win32 does not. Depending on the use of fork and the code base, Win32 has two APIs that can be used: CreateProcess and CreateThread. A UNIX application that forks multiple copies of itself can be reworked in Win32 to have either multiple processes or a single process with multiple threads. If multiple processes are used, there are multiple methods of IPC that can be used to communicate between the processes (and perhaps to update the code and data of the new process to be like the parent, if the functionality that fork provides is needed). For more on IPC, see Interprocess Commuications.

Nik Bougalis
  • 10,495
  • 1
  • 21
  • 37
Erik
  • 464
  • 2
  • 5
4

fork() is available on posix systems only. It's certainly not available on windows. Are you sure your operating system provides fork?

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304