简体   繁体   中英

Physical memory management in Userspace?

I am working on an embedded device with only 512MB of RAM and the device is running Linux kernel. 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 . Controling the MMU is normally a privileged operation, so only an operating system kernel has access to it. 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 . Refer another Q&A .

I wrote the test code and run it on my 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
  • Process 1 limit of 128MB - Uses 64MB
  • Process 2 imit of 128MB - Uses 64MB
  • Process 3 limit of 256MB - Uses 256MB then runs out of memory, when in fact 128MB is still available.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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