1

I wrote this basics forking program in C language. But the compiler issues me error. The code is

#include<stdio.h>
#include<sys/stat.h>
#include<ctype.h>

void childProcess(){
    printf("From child process with process id %d\n", getpid());
}


void parentProcess(){
    printf("Parent Process with id %d\n", getpid());
    pid_t pid = fork();
    if (pid == 0){
        childProcess();
    } else if (pid > 0){
        printf("Parent spawned process %d\n", pid);
    } else{
        printf("Forking isn't supported!\n");
    }

}

int main(){
    parentProcess();

}

The error is

C:\Users\ADMINI~1\AppData\Local\Temp\ccORJc6N.o forking.c:(.text+0x3e): undefined reference to `fork'

3 Answers3

2

You are using Windows, looks like you can't do that on Windows. Here are some related posts:

These were the first three links when googling your error: undefined reference to `fork'

Community
  • 1
  • 1
kruthar
  • 309
  • 1
  • 2
  • 10
1

You're missing a library:

Add this to the top of your code

#include  <sys/types.h>
FirebladeDan
  • 1,069
  • 6
  • 14
  • This is a linker error. Adding a header won't fix the issue. – Jim Lewis Aug 20 '15 at 03:15
  • He didn't specify windows and his error is associated with pid_t reference. If it is on windows then yup you're correct – FirebladeDan Aug 20 '15 at 03:17
  • Actually, this IS the error in the program. The other error, the current most-voted answer, is actually an error in the users choice of OS. Keep in mind that the question is about *the error in the program*, and missing an include might actually cause such linker errors... Assuming the correct OS, @JimLewis, this WOULD actually fix the error. – autistic Aug 20 '15 at 03:17
1

To unify the best answers I see so far (which aren't necessarily the most popular):

I found another error in the program. %d tells printf to print an argument of type int, but the actual type you've provided is pid_t. According to the printf manual (which I highly recommend reading and fully understanding multiple times for the purpose of writing better code; you'll learn a lot), "If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined." You need an explicit conversion. For example:

printf("From child process with process id %d\n", (int) getpid());
printf("Parent Process with id %d\n", (int) getpid());
printf("Parent spawned process %d\n", (int) pid);

The conversion to int is itself an error; according to the <sys/types.h> header manual, a POSIX-compliant implementation "shall support one or more programming environments in which the widths of blksize_t, pid_t, size_t, ssize_t, suseconds_t, and useconds_t are no greater than the width of type long." In such an environment, a conversion to long and using the corresponding %ld (that's an ell, by the way) directive would be more appropriate:

printf("From child process with process id %ld\n", (long) getpid());
printf("Parent Process with id %ld\n", (long) getpid());
printf("Parent spawned process %ld\n", (long) pid);
autistic
  • 1
  • 3
  • 35
  • 80