user6655110
user6655110

Reputation:

why there is an error when executing the fork() system call?

there is an error when trying to executing the fork() call. warning: implicit declaration of function ‘fork’ [-Wimplicit-function-declaration fork(); here is my code

#include <stdio.h>
int main()
{
    int a, b;
    b = fork();
    printf("hello");
    if (b == 0)
    {
        printf("child");
    }
    else
    {
        printf("parent");
    }
}

Upvotes: -1

Views: 2653

Answers (3)

Manish Kumar
Manish Kumar

Reputation: 558

  ## try this code##
  #include <stdio.h>
  #include <unistd.h>
  int main()
{
    int a;
    pid_t b;
    b = fork();
   printf("HELLO");
   if (b == 0)
   {
      printf("child");
   }
   else
   {
    printf("parent");
   }

}

Upvotes: 0

flmng0
flmng0

Reputation: 427

Generally, the error -Wimplicit-function-declaration is only thrown when the method or function that you're trying to use has not been defined in any of the headers that have been included.

e.g: trying to use printf without including stdio.h

The fork function is included from unistd.h.

Upvotes: 0

secon25
secon25

Reputation: 60

Try

#include <stdio.h>
#include <unistd.h>
int main()
{
    int a;
    pid_t b;
    b = fork();
    printf("hello");
    if (b == 0)
    {
        printf("child");
    }
    else
    {
        printf("parent");
    }
}

Upvotes: 1

Related Questions