LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Does Linux have virtual memory management APIs? (https://www.linuxquestions.org/questions/programming-9/does-linux-have-virtual-memory-management-apis-329514/)

neo_in_matrix 06-02-2005 06:54 AM

Does Linux have virtual memory management APIs?
 
On Windows platform m$ provides a set of APIs that manages virtual memory. I consulted Linux man, but could not find such topics.

I can use malloc to allocate a large block of memory that is larger than system's physical memory. I want to know if there are a *special* set of APIs to manage virtual memory in Linux?

I am using FC4 test 3.

Hko 06-02-2005 08:49 AM

ls /proc/sys/vm

neo_in_matrix 06-03-2005 05:12 PM

I see a list of 0-byte files. What should I do?

Hko 06-04-2005 04:14 AM

Look inside them anyway.

All in /proc are not real files on disk. But a way to read or set kernel configuration.

E.g. To make your system swap less and increase tendency to keep more processes in RAM, lower the contents of /proc/sys/vm/swappiness:
Code:

# Seems to have 0 bytes:
ls -l /proc/sys/vm/swappiness
-rw-r--r--  1 root root 0 Jun  4 11:12

# Yet not empty:
$ cat /proc/sys/vm/swappiness
60

# Make system swap less:
echo 30 > /proc/sys/vm/swappiness

# Check to see the change:
$ cat /proc/sys/vm/swappiness
30


neo_in_matrix 06-04-2005 03:34 PM

That's interesting.

But how do I explicitly allocate memory from swapfiles using C/C++ calls?

win32sux 06-05-2005 01:19 AM

Quote:

Get an overview of the memory management techniques that are available to Linux™ programmers, focusing on the C language but applicable to other languages as well. This article gives you the details of how memory management works, and then goes on to show how to manage memory manually, how to manage memory semi-manually using referencing counting or pooling, and how to manage memory automatically using garbage collection.
Complete Article


neo_in_matrix 06-05-2005 02:24 AM

Wow, thanks!

neo_in_matrix 06-05-2005 10:44 AM

Can someone look at my test code and tell me if I am doing things right?

Code:

#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>

void test_mmap()
{
        int                fd;
        int                page_size;
        char*        p;
        char*        file;
       
        file = "mmapfile";
        fd = open(file, O_RDWR | O_NONBLOCK);
        if(fd < 0)
        {
                printf("Unabled to open target file:\n%s", file);
                return;
        }

        page_size = getpagesize();
        printf("page size is %d\n", (int)page_size);

        p = (char*)mmap(0, page_size,
                PROT_READ | PROT_WRITE, MAP_SHARED,
                fd, 0);
        if(p)
        {
                puts("Dumping first 1000 bytes:");
                for(int i = 0; i < 1000; i++)
                {
                        printf("0x%04x:0x%02x\n", i, (int)p[i] & 0xff);
                }

                puts("\nPadding with garbage data...");
                for(int i = 0; i < page_size; i++)
                {
                        p[i] = i;
                }
                munmap(p, page_size);
        }
        else
        {
                puts("mmap failed");
        }
        close(fd);

        puts("Done!");
}

int main()
{
        test_mmap();

        return 0;
}



All times are GMT -5. The time now is 08:39 AM.