在Linux上,哪些条件可能导致fork()或system()调用失败? [英] What are some conditions that may cause fork() or system() calls to fail on Linux?

查看:467
本文介绍了在Linux上,哪些条件可能导致fork()或system()调用失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

又如何找出它们是否正在发生,并导致fork()或system()返回错误?换句话说,如果fork()或system()返回错误,那么在Linux中我可以检查哪些内容以诊断为什么发生了该特定错误?

And how can one find out whether any of them are occuring, and leading to an error returned by fork() or system()? In other words, if fork() or system() returns with an error, what are some things in Linux that I can check to diagnose why that particular error is happening?

例如:

  • 只是内存不足(导致errno ENOMEM出现)-用'free'等检查内存使用情况.
  • 内核没有足够的内存来复制页表和父进程的其他记帐信息(结果为errno EAGAIN)
  • 是否存在全局流程限制? (还会导致errno EAGAIN吗?)
  • 是否有针对每个用户的进程限制?我怎么知道它是什么?
  • ...?

推荐答案

又如何找出其中是否正在发生?

And how can one find out whether any of them are occuring?

如果结果(返回值)为-1,则检查 errno

Check the errno value if the result (return value) is -1

在Linux的手册页中:

From the man page on Linux:

返回值
成功后,将在父级中返回子进程的PID,并在子级中返回0.失败时,将在父级中返回-1,不创建任何子级进程,并正确设置errno.

RETURN VALUE
On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.

错误
EAGAIN
fork()无法分配足够的内存来复制父级的页表并为子级分配任务结构.
EAGAIN
无法创建新进程,因为遇到了调用者的RLIMIT_NPROC资源限制.要超出此限制,该进程必须具有CAP_SYS_ADMIN或CAP_SYS_RESOURCE功能.
ENOMEM
由于内存紧张,fork()无法分配必要的内核结构.

ERRORS
EAGAIN
fork() cannot allocate sufficient memory to copy the parent's page tables and allocate a task structure for the child.
EAGAIN
It was not possible to create a new process because the caller's RLIMIT_NPROC resource limit was encountered. To exceed this limit, the process must have either the CAP_SYS_ADMIN or the CAP_SYS_RESOURCE capability.
ENOMEM
fork() failed to allocate the necessary kernel structures because memory is tight.

符合 SVr4、4.3BSD,POSIX.1-2001.

CONFORMING TO SVr4, 4.3BSD, POSIX.1-2001.

这篇关于在Linux上,哪些条件可能导致fork()或system()调用失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆