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");开发者_如何学JAVA
}
}
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.
fork()
is available on posix systems only. It's certainly not available on windows. Are you sure your operating system provides fork?
精彩评论