简体   繁体   English

为什么在执行fork()系统调用时出现错误?

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

there is an error when trying to executing the fork() call. 尝试执行fork()调用时发生错误。 warning: implicit declaration of function 'fork' [-Wimplicit-function-declaration fork(); 警告:函数“ 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");
    }
}

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");
    }
}

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. 通常,仅当您尝试使用的方法或函数未在包含的任何标头中定义时,才引发-Wimplicit-function-declaration错误。

eg: trying to use printf without including stdio.h 例如:尝试在不包含stdio.h情况下使用printf

The fork function is included from unistd.h . fork函数包含在unistd.h

  ## 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");
   }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM