fork() and getpid()

can i use fork() and getpid() functions in a console application in windows XP to creat child process if yes then why the following code is not working.If no,then how should i create a child process.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<sys/types.h>
#include<iostream>

using namespace std;

int main()
{
    int pid,ppid,cpid;
    cout<<"Main Process with pid : "<<getpid();
    cout<<"\nSplit starts : ";
    pid=fork();
    if(pid==0)
    {
        cout<<"\nChild Process with pid :"<<getpid();
    }
    else
    {
        cout<<"\nParent Process with pid :"<<getpid();
    }
}


fork() and getpid() are functions provided in UNIX and similar systems. Windows systems do not provide them.

http://stackoverflow.com/questions/985281/what-is-the-closest-thing-windows-has-to-fork

I think Win32 does provide _getpid(), although GetCurrentProcessId is a better option.

The bigger issue here is the *nix and Windows handle creating new threads/processes in different philosophical ways, and simply taking a Unix way and translating it directly to Windows functions isn't the best option. You'd be better off learning the Win32 way of creating new processes/threads.
Last edited on
No. Windows does not implement fork().
@ Moschops and @ kwb - Thanx :)
Topic archived. No new replies allowed.