Here’s my code (created just to test fork()):
#include <stdio.h>
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int pid;
pid=fork();
if (pid==0) {
printf("I am the child\n");
printf("my pid=%d\n", getpid());
}
return 0;
}
I get following warnings:
warning: implicit declaration of function 'fork'
undefined reference to 'fork'
What is wrong with it?
unistd.handforkare part of the POSIX standard. They aren’t available on windows (text.exein your gcc command hints that’s you’re not on *nix).It looks like you’re using gcc as part of MinGW, which does provide the
unistd.hheader but does not implement functions likefork. Cygwin does provide implementations of functions likefork.However, since this is homework you should already have instructions on how to obtain a working environment.