gpt4 book ai didi

macos - 我不能运行超过 100 个进程

转载 作者:行者123 更新时间:2023-12-01 08:42:02 25 4
gpt4 key购买 nike

我有大量的 shell 命令通过授权服务的“AuthorizationExecuteWithPrivileges”调用以 root/admin 权限执行。问题是在一段时间后(10-15 秒,可能是 100 个 shell 命令)程序停止响应,并在调试器中出现此错误:

无法 fork :errno 35

然后,当应用程序运行时,我无法启动更多应用程序。我研究了这个问题,显然这意味着系统没有更多线程可供使用。但是,我使用 Activity Monitor 进行了检查,我的应用只使用了 4-5 个线程。

为了解决这个问题,我认为我需要做的是将 shell 命令分离到一个单独的线程中(远离主线程)。我以前从未使用过线程,我不确定从哪里开始(我找不到全面的示例)

谢谢

最佳答案

正如 Louis Gerbarg 已经指出的那样,您的问题与线程无关。我已经相应地编辑了你的标题和标签。

I have a massive number of shell commands being executed with root/admin priveleges through Authorization Services' "AuthorizationExecuteWithPrivileges" call.

不要那样做。该功能仅存在,因此您可以将 root:admin 所有权和 setuid 模式位恢复到您要以 root 身份运行的工具。

这个想法是你应该将应该以root身份运行的代码与不需要以root身份运行的部分完全分开,以便需要root的部分可以拥有它(通过setuid位) 并且不需要 root 的部分可以没有它(通过没有 setuid)。

Authorization Services Programming Guide 中有一个代码示例。 .

The issue is that after a while (10-15 seconds, maybe 100 shell commands) the program stops responding with this error in the debugger:

couldn't fork: errno 35

是的。您一次只能运行几百个进程。这是操作系统强制的限制。

这是一个软限制,这意味着你可以提高它——但只能达到硬限制,你不能提高它。查看 limit 的输出和 limit -h (在zsh中;我不知道其他shell)。

在运行更多进程之前,您需要等待进程完成。

And then while the app is running, I cannot launch any more applications.

因为您已经在运行尽可能多的进程。 x-hundred-process 限制是每个用户,而不是每个进程。

I researched this issue and apparently it means that there are no more threads available for the system to use.

不,它没有。

errno 错误代码用于许多事情。 EAGAIN (35,“资源暂时不可用”)可能意味着当由启动线程的系统调用设置时不再有线程,但这并不意味着当由另一个系统调用或函数设置时。

您明确引用的错误消息说它是由 fork 设置的。 ,这是启动一个新的进程的系统调用,而不是一个新的线程。在这种情况下,EAGAIN意思是“你已经在运行尽可能多的进程”。见 the fork manpage .

However, I checked using Activity Monitor and my app is only using 4-5 threads.

看到了吗?

To fix this problem, I think what I need to do is separate the shell commands into a separate thread (away from the main thread).

每个线程启动一个进程只会帮助您更快地用完进程。

I have never used threading before …

听起来你还没有,因为你所指的函数启动了一个进程,而不是一个线程。

关于macos - 我不能运行超过 100 个进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1317722/

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