7

What is the library that defines fork(). I am learning to use fork(). I found out that the Standard I/O Library : stdio.h is enough for fork() to work but that does not apply in my case.

I am using gcc in Code::Blocks on Windows 8 Pro

My Code is:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
#include <time.h>

int main(void)
{

    pid_t process;
    process = fork();

    if(process< 0)
    {
        printf("fork failed");
    }
    if(process > 0)
    {
        printf("\nParent Process Executed");
    }

    if(process == 0)
    {
        printf("\nChild Process Executed");
    }
    return 0 ;
}

The Exact Error I get is:

useoffork.o:useoffork.c:(.text+0xf): undefined reference to `fork'

cipher
  • 2,414
  • 4
  • 30
  • 54
  • 1
    this may help you man fork...http://linux.die.net/man/2/fork – Omkant Nov 30 '12 at 10:26
  • possible duplicate of [Undefined reference to fork() in Code::Blocks editor in Windows OS](http://stackoverflow.com/questions/8819673/undefined-reference-to-fork-in-codeblocks-editor-in-windows-os) – Rohan Dec 01 '12 at 10:13

3 Answers3

4

The C standard library (glibc) implements fork() which calls a UNIX/Linux-specific system call eventually to create a process, on Windows, you should use the winapi CreateProcess() see this example in MSDN.

Note: Cygwin fork() is just a wrapper around CreateProcess() see How is fork() implemented?

iabdalkader
  • 17,009
  • 4
  • 47
  • 74
3

I am using gcc in Code::Blocks on Windows 8 Pro

You don't have fork on windows. You can use cygwin or something like that though.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
2
#include <unistd.h> 

The C library defines fork().

It is the UNIX/Linux-specific system calls to create a process, on linux etc.

Syscall
  • 19,327
  • 10
  • 37
  • 52