1

I was trying the following code for an assignment in visual studio code:

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

int main(int argc, char *argv[]) {
printf("hello world (pid:%d)\n", (int) getpid());
int rc = fork();
if (rc < 0) {
// fork failed
fprintf(stderr, "fork failed\n");
exit(1);
} else if (rc == 0) {
// child (new process)
printf("hello, I am child (pid:%d)\n", (int) getpid());
} else {
// parent goes down this path (main)
printf("hello, I am parent of %d (pid:%d)\n",
rc, (int) getpid());
}
return 0;
}

I initially had problems with the inbuilt compiler so I Switched to MinGW for windows gcc compiler which now gives me the error:

p1.c: In function 'main':
p1.c:9:10: warning: implicit declaration of function 'fork' [-Wimplicit-function-declaration]
 int rc = fork();
          ^~~~
C:\Users\X1\AppData\Local\Temp\ccGunGbh.o:p1.c:(.text+0x28): undefined reference to `fork'
collect2.exe: error: ld returned 1 exit status

Now as I understand the fork() function is missing from gcc or something. Is there any other compiler for C that I can try to test this function? or is there some workaround for gcc?

frithalien
  • 11
  • 2

1 Answers1

2

Go install Cygwin or WSL and build in that environment; then it will work.

There is no fork() on Windows and trying to make it is an exercise in straining your mind for no particularly good reason.

Joshua
  • 40,822
  • 8
  • 72
  • 132