一个进程锁定了多少内存

时间:2021-03-29 21:21:27

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兼容系统上运行的锁定内存量?

1 个解决方案

#1


3  

You will probably need to check for each system and implement it accordingly. On Linux:

您可能需要检查每个系统并相应地实施它。在Linux上:

cat /proc/$PID/status | grep VmLck

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. Something like:

您可能需要在C中执行相同的操作(逐行读取/ proc并搜索VmLck),因为此信息是在函数task_mem(在array.c中)中创建的,我认为您无法直接访问它。就像是:

#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"
}

#1


3  

You will probably need to check for each system and implement it accordingly. On Linux:

您可能需要检查每个系统并相应地实施它。在Linux上:

cat /proc/$PID/status | grep VmLck

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. Something like:

您可能需要在C中执行相同的操作(逐行读取/ proc并搜索VmLck),因为此信息是在函数task_mem(在array.c中)中创建的,我认为您无法直接访问它。就像是:

#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"
}