gpt4 book ai didi

c - 防止在子进程中 fork

转载 作者:太空狗 更新时间:2023-10-29 11:18:50 25 4
gpt4 key购买 nike

我有一个 Linux 进程(C 程序),它生成了几个子进程。我想禁止在系统级别上对这些子进程进行另一次 fork ,这样如果它们尝试 fork ,这些进程就会被杀死。如何实现?我更喜欢将此策略嵌入到主机 C 代码中。 setrlimit(2) 可以吗?

最佳答案

是的,setrlimit() 可以做到这一点。请参阅手册页并阅读

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. Upon encountering this limit, fork(2) fails with the error EAGAIN.

或者,您可以使用 /etc/security/limits.conf 对进程设置硬限制。在文件中查找一些示例。

#@student        hard    nproc           20
#@faculty soft nproc 20
#@faculty hard nproc 50
#ftp hard nproc 0

关于c - 防止在子进程中 fork ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27348744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com