[英]Prevent forking in a child process
I have a Linux process (C program) which spawns a couple of child processes. 我有一个Linux进程(C程序),它产生了几个子进程。 I'd like to forbid another forking in those child processes on the system level, so that those processes would get killed if they tried to fork anyway.
我想禁止在系统级别的那些子进程中进行另一次分叉,这样如果他们试图分叉那些进程就会被杀死。 How to achieve that?
怎么实现呢? I prefer to embed this policy in the host C code.
我更喜欢将此策略嵌入主机C代码中。 Can
setrlimit(2)
do that? setrlimit(2)
做到吗?
Yes, setrlimit() can do this. 是的,setrlimit()可以做到这一点。 Refer to man page and read up on
请参阅手册页并阅读
RLIMIT_NPROC
RLIMIT_NPROC
The maximum number of processes (or, more precisely on Linux, threads) that can be created for the real user ID of the calling process.可以为调用进程的真实用户ID创建的最大进程数(或者更准确地说,在Linux上,线程)。 Upon encountering this limit, fork(2) fails with the error EAGAIN.
遇到此限制时,fork(2)失败并显示错误EAGAIN。
Alternatively, you can set a hard limit on a process using /etc/security/limits.conf
. 或者,您可以使用
/etc/security/limits.conf
为进程设置硬限制。 Look up some of the examples in the file. 查找文件中的一些示例。
#@student hard nproc 20
#@faculty soft nproc 20
#@faculty hard nproc 50
#ftp hard nproc 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.