简体   繁体   English

用户空间中的物理内存管理?

[英]Physical memory management in Userspace?

I am working on an embedded device with only 512MB of RAM and the device is running Linux kernel. 我正在一个只有512MB RAM的嵌入式设备上工作,并且该设备正在运行Linux内核。 I want to do the memory management of all the processes running in the userspace by my own library. 我想通过自己的库对用户空间中运行的所有进程进行内存管理。 is it possible to do so. 有可能这样做吗 from my understanding, the memory management is done by kernel, Is it possible to have that functionality in User space. 据我了解,内存管理是由内核完成的,是否有可能在用户空间中拥有该功能。

If your embedded device runs Linux, it has an MMU . 如果您的嵌入式设备运行Linux,则它具有MMU Controling the MMU is normally a privileged operation, so only an operating system kernel has access to it. 控制MMU通常是特权操作,因此只有操作系统内核可以访问它。 Therefore the answer is: No , you can't. 因此,答案是: ,您不能。

Of course you can write software running directly on the device, without operating system, but I guess that's not what you wanted. 当然,您可以编写直接在设备上运行的软件,而无需操作系统,但是我想那不是您想要的。 You should probably take one step back, ask yourself what gave you the idea about the memory management and what could be a better way to solve this original problem. 您可能应该退后一步,问自己,是什么使您对内存管理有了想法,什么是解决此原始问题的更好方法。

You can consider using setrlimit . 您可以考虑使用setrlimit Refer another Q&A . 请参阅其他问答

I wrote the test code and run it on my PC. 我编写了测试代码并在PC上运行它。 I can see that memory usage is limited. 我可以看到内存使用受到限制。 The exact relationship of units requires further analysis. 单位之间的确切关系需要进一步分析。

#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>

int main(int argc, char* argv)
{
    long limitSize =      1;
    long testSize  = 140000;

    // 1. BEFORE: getrlimit
    {
        struct rlimit asLimit;
        getrlimit(RLIMIT_AS, &asLimit);
        printf("BEFORE: rlimit(RLIMIT_AS) = %ld,%ld\n", asLimit.rlim_cur, asLimit.rlim_max);
    }

    // 2. BEFORE: test malloc
    {
        char *xx = malloc(testSize);
        if (xx == NULL) 
            perror("malloc FAIL");
        else
            printf("malloc(%ld) OK\n", testSize);
        free(xx);
    }

    // 3. setrlimit
    {
        struct rlimit new;
        new.rlim_cur = limitSize; 
        new.rlim_max = limitSize;
        setrlimit(RLIMIT_AS, &new);
    }

    // 4. AFTER: getrlimit
    {
        struct rlimit asLimit;
        getrlimit(RLIMIT_AS, &asLimit);
        printf("AFTER: rlimit(RLIMIT_AS) = %ld,%ld\n", asLimit.rlim_cur, asLimit.rlim_max);
    }

    // 5. AFTER: test malloc
    {
        char *xx = malloc(testSize);
        if (xx == NULL) 
            perror("malloc FAIL");
        else
            printf("malloc(%ld) OK\n", testSize);
        free(xx);
    }

    return 0;
}

Result: 结果:

BEFORE: rlimit(RLIMIT_AS) = -1,-1
malloc(140000) OK
AFTER: rlimit(RLIMIT_AS) = 1,1
malloc FAIL: Cannot allocate memory

From what I understand of your question, you want to somehow use your own library for handling memory of kernel processes. 据我对您问题的了解,您想以某种方式使用自己的库来处理内核进程的内存。 I presume you are doing this to make sure that rogue processes don't use too much memory, which allows your process to use as much memory as is available. 我假设您正在执行此操作,以确保恶意进程不会使用过多的内存,从而使您的进程能够使用尽可能多的可用内存。 I believe this idea is flawed. 我相信这个想法是有缺陷的。

For example, imagine this scenario: 例如,想象一下这种情况:

  • Total memory 512MB 总内存512MB
  • Process 1 limit of 128MB - Uses 64MB 进程1的128MB限制-使用64MB
  • Process 2 imit of 128MB - Uses 64MB 进程2模仿128MB-使用64MB
  • Process 3 limit of 256MB - Uses 256MB then runs out of memory, when in fact 128MB is still available. 进程3限制为256MB-使用256MB,然后用尽内存,而实际上128MB仍然可用。

I know you THINK this is the answer to your problem, and on 'normal' embedded systems, this would probably work, but you are using a complex kernel, running processes you don't have total control over. 我知道您认为这是问题的答案,并且在“常规”嵌入式系统上,这可能会起作用,但是您使用的是复杂的内核,正在运行的过程您无法完全控制。 You should write YOUR software to be robust when memory gets tight because that is all you can control. 当内存不足时,您应该编写强大的软件,因为这是您可以控制的一切。

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

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