0

This program is not working on gcc......and giving an linker error..i.e. undefined reference to fork...

#include<stdio.h>  
#include<stdlib.h>  
#include<unistd.h>    

int main()  
{  
    int a=10;  
    if (a==10 && fork())  
        printf("hello");  
    else  
        printf("world");  

    system("pause");  
    return 0;  
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Tushar Gaurav
  • 486
  • 4
  • 18

2 Answers2

6

The fact that you're using system("pause") (a bad idea, by the way, when you can simply use getchar()) leads me to believe that you're running on Windows. Unless you use an emulation layer like CygWin, fork is not available on that platform.

To be honest, I'm not sure how you even managed to compile since Windows doesn't generally have unistd.h either unless perhaps you've installed SFU or MinGW (which, despite having unistd.h to ease the task of compiling code (less conditional compilation), does not provide more functionality than what you get with Windows).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2

fork does not exist on Windows (except if you are using Cygwin). Use CreateProcess instead.

md5
  • 23,373
  • 3
  • 44
  • 93