C语言 fork() 如何/为什么会失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19990704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:58:10  来源:igfitidea点击:

How/Why can fork() fail

cposix

提问by Juicy

I'm currently studying the fork() function in C. I understand what it does (I think). My question is why do we check it in the following program?

我目前正在研究 C 中的 fork() 函数。我理解它的作用(我认为)。我的问题是为什么我们在下面的程序中检查它?

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
  int pid;
  pid=fork();

  if(pid<0)/* why is this here? */
  {
    fprintf(stderr, "Fork failed");
    exit(-1);
  }
  else if (pid == 0)
  {
    printf("Printed from the child process\n");
  }
  else
  {
    printf("Printed from the parent process\n");
    wait(pid);
  }
}

In this program we check if the PID returned is < 0, which would indicate a failure. Why can fork() fail?

在这个程序中,我们检查返回的 PID 是否 < 0,这表示失败。为什么 fork() 会失败?

回答by Arkku

From the man page:

从手册页:

 Fork() will fail and no child process will be created if:
 [EAGAIN]           The system-imposed limit on the total number of pro-
                    cesses under execution would be exceeded.  This limit
                    is configuration-dependent.

 [EAGAIN]           The system-imposed limit MAXUPRC (<sys/param.h>) on the
                    total number of processes under execution by a single
                    user would be exceeded.

 [ENOMEM]           There is insufficient swap space for the new process.

(This is from the OS?X man page, but the reasons on other systems are similar.)

(这是来自 OS?X 手册页,但其他系统上的原因类似。)

回答by R.. GitHub STOP HELPING ICE

forkcan fail because you live in the real world, not some infinitely-recursive mathematical fantasy-land, and thus resources are finite. In particular, sizeof(pid_t)is finite, and this puts a hard upper bound of 256^sizeof(pid_t) on the number of times forkcould possibly succeed (without any of the processes terminating). Aside from that, you also have other resources to worry about like memory.

fork可能会失败,因为你生活在现实世界中,而不是一些无限递归的数学幻想世界,因此资源是有限的。特别是,sizeof(pid_t)是有限的,这对fork可能成功的次数(没有任何进程终止)设置了 256^sizeof(pid_t) 的硬上限。除此之外,您还有其他资源需要担心,例如内存。

回答by Ben Nelson

Not enough memory available to make the new process perhaps.

可能没有足够的可用内存来创建新进程。

回答by yamafontes

If the kernel fails to allocate memory for example, that's pretty bad and would cause fork()to fail.

例如,如果内核无法分配内存,那就很糟糕了,会导致fork()失败。

Have a look at the error codes here:

看看这里的错误代码:

http://linux.die.net/man/2/fork

http://linux.die.net/man/2/fork

回答by Frank Puck

Apparently it can fail (not really fail but hang infinitely) due to the following things coming together:

显然它可能会失败(不是真的失败,而是无限挂起),因为以下事情结合在一起:

  1. trying to profile some code
  2. many threads
  3. much memory allocation
  1. 试图分析一些代码
  2. 许多线程
  3. 大量内存分配

see also

也可以看看

https://github.com/jvm-profiling-tools/async-profiler/issues/97

https://github.com/jvm-profiling-tools/async-profiler/issues/97

https://github.com/gperftools/gperftools/issues/704

https://github.com/gperftools/gperftools/issues/704

https://bugzilla.redhat.com/show_bug.cgi?id=645528

https://bugzilla.redhat.com/show_bug.cgi?id=645528

example:

例子:

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

int main()
{
    size_t sz = 32*(size_t)(1024*1024*1024);
    char *p = (char*)malloc(sz);
    memset(p, 0, sz);
    fork();
    return 0;
}

build:

建造:

gcc -pg tmp.c

run:

跑:

./a.out