守护进程如何在不使用内存的情况下保持活动状态?

How can a daemon stay active while using no memory?

我在使用 ps aux 命令时想到了这个问题。

这里我可以看到有几个进程在0% CPU 0% MEM 0 VSZ 0 RSS.

如果守护进程没有使用任何内存,为什么以及如何首先显示它?我有点理解 0% CPU usage 意味着该进程当前未在使用中,但不会 0% MEM 意味着没有有没有过程?

我想检查这是否是特定于系统守护程序的,所以我制作了一个带有无限循环且没有任何变量的简单 C 程序。

void main()
{
        while (1){}
}

此时VSZ和RSS有实际值,而MEM保持0%。

这里发生了什么?

%MEM 可能未在您的系统上完整记录。关于 ps 命令的 AIX 手册说:

%MEM

Calculated as the sum of the number of working segment and code segment pages in memory times 4 (that is, the RSS value), divided by the size of the real memory in use, in the machine in KB, times 100, rounded to the nearest full percentage point. This value attempts to convey the percentage of real memory being used by the process. Unfortunately, like RSS, it tends the exaggerate the cost of a process that is sharing program text with other processes. Further, the rounding to the nearest percentage point causes all of the processes in the system that have RSS values under 0.005 times real memory size to have a %MEM of 0.0.

正如您通过检查输出所怀疑的那样,应用了一些舍入。因此,如果值太低,则会打印 %0.0。

而且,这个衡量实际内存使用的百分比,这意味着它不反映进程的大小,而只反映进程的哪一部分实际映射到实际内存。

在您的第一种情况下,CPU 的 %0.0 仅表示该进程存在但实际上什么也不做,它可能处于等待状态(或消耗很小比例的处理能力),而不是 "that it is is not currently in use"。在你的第二种情况下,你的进程是活跃的,它实际上很忙(这是%97.7反映的),但它所做的是愚蠢的(无限循环什么都不做)。

要了解所有这些,您可以阅读有关进程状态、进程调度和虚拟内存的内容。

虽然 Jean-Baptiste 的回答就目前而言是正确的,但我相信在这种情况下更重要的是,您注意到的所有三个字段进程中的所有 0 内存都是内核线程。它们的内存都是内核内存,不会出现在top或者ps上。您可以通过括号中包含 ps 的命令和 VSZ 列中不消耗内存的进程来判断它是 linux 上的内核线程。 (该列基本上代表了可以被视为进程内存的所有内容。内核线程仅为 0,这只是因为它们没有正确报告其内存。

另请注意,开始时间为 2018 年且耗时不超过 1 分 41 秒,其中 none 个工作确实非常活跃。