简体   繁体   English

一个进程锁定了多少内存

[英]How much memory locked in a process

Using getrlimit(RLIMIT_MEMLOCK), one can get the allowed amount of locked memory a process can allocate (mlock() or mlockall()). 使用getrlimit(RLIMIT_MEMLOCK),可以获得进程可以分配的允许的锁定内存量(mlock()或mlockall())。

But how to retrieve the currently locked memory amount ? 但是如何检索当前锁定的内存量?

For example, there's no information returned by getrusage(). 例如,getrusage()没有返回任何信息。

Under Linux, it is possible to read /proc/self/status and extract the amount of locked memory from the line beginning with VmLck. 在Linux下,可以读取/ proc / self / status并从VmLck开始的行中提取锁定内存量。

Is there a portable way to retrieve the amount of locked memory which would work on Linux, *BSD and other POSIX compatible systems ? 是否有一种可移植的方法来检索可在Linux,* BSD和其他POSIX兼容系统上运行的锁定内存量?

You will probably need to check for each system and implement it accordingly. 您可能需要检查每个系统并相应地实施它。 On Linux: 在Linux上:

cat /proc/$PID/status | grep VmLck

You will probably need to do the same in C (read /proc line by line and search for VmLck ), as this information is created in the function task_mem (in array.c ) which I don't think you can access directly. 您可能需要在C中执行相同的操作(逐行读取/proc并搜索VmLck ),因为此信息是在函数task_mem (在array.c中 )中创建的,我认为您无法直接访问该函数。 Something like: 就像是:

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

char cwd[PATH_MAX];
sprintf(cwd, "/proc/%d/status", getpid());

FILE* fp = fopen(cwd, "r");
if(!fp) {
    exit(EXIT_FAILURE);
}

while((read = getline(&line, &len, fp)) != -1) {
    // search for line starting by "VmLck"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM