mmap virtual memory (HDD) and main memory (RAM) allocation

I'm working with mmap() function. I Confiused about something in mmap flags.

MAP_SHARED with fd → Usage To allocated virtual memory which uses Hard Drive (HDD) and physical memory (RAM) will not be used. I allocated 2 GB using this flag with fd and my RAM remained unused (what I expected).

MAP_PRIVATE|MAP_ANONYMOUS with (fd = -1) → Usage To allocated main memory, which is RAM. I allocated 2 GB using this flag and I filled it with something then I checked my RAM usage and yes, 2 GB in use ...

Now my point, first method (MAP_SHARED) uses virtual memory (HDD) and second method (MAP_PRIVATE|MAP_ANONYMOUS) uses main memory (RAM) so 100% the second method MUST be different in performance since RAM is too much faster than HDD ... am I right ? If I'm right, so why mmap function is called as a 'virtual memory allocation' function !!!??? By flag MAP_PRIVATE|MAP_ANONYMOUS allocates main memory (RAM) too !!!!!!!

If MAP_PRIVATE|MAP_ANONYMOUS does not guarantee to provide 'main memory (RAM)', what should I do to allocated memory from main memory (RAM) ?!

What I got from mmap, mmap checks if a private flag (MAP_PRIVATE|MAP_ANONYMOUS) is provided, checks for unused RAM space and provides memory from RAM (main memory), if there is available space on RAM and if there isn't, it uses virtual memory (HDD). Right !?

Update

I decide to put 2 tests. one with MAP_SHARED and one with MAP_PRIVATE|MAP_ANONYMOUS

test 1 Memory allocation based on a file (MAP_SHARED)

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

int
main() {
    double time_spent = 0.0;
    int fd = open("/home/alireza/Documents/mem", O_CREAT|O_RDWR);
    ftruncate(fd, (4294967296));
    char * memory = mmap(NULL, (4294967296), PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);
    if(memory == MAP_FAILED) {
        printf("(%d)", errno);
        exit(0);
    }
    write(0, "started ...\n", sizeof("started ...\n")-1);
    clock_t begin = clock();
    for (long i = 0; i < (4294967296); i++) {
        memory[i] = 'c';
    }
    clock_t end = clock();
    write(0, "done\n", sizeof("done\n")-1);
    time_spent += (double)(end - begin) / CLOCKS_PER_SEC;
    printf("time (%f)\n", time_spent);
    exit(0);
}

Result (9 seconds (since it uses virtual memory (HDD)) !!!)

started ...
done
time (9.087634)

test 2 Memory allocation using RAM (MAP_PRIVATE|MAP_ANONYMOUS)

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

int
main() {
    double time_spent = 0.0;
    char * memory = mmap(NULL, (4294967296), PROT_WRITE|PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
    if(memory == MAP_FAILED) {
        printf("(%d)", errno);
        exit(0);
    }
    write(0, "started ...\n", sizeof("started ...\n")-1);
    clock_t begin = clock();
    for (long i = 0; i < (4294967296); i++) {
        memory[i] = 'c';
    }
    clock_t end = clock();
    write(0, "done\n", sizeof("done\n")-1);
    time_spent += (double)(end - begin) / CLOCKS_PER_SEC;
    printf("time (%f)\n", time_spent);
    exit(0);
}

Result (1 second)

started ...
done
time (1.405220)

So mmap is not just virtual memory allocation (HDD). it's main memory allocation too and it's decide it based on the flag ! so using MAP_PRIVATE|MAP_ANONYMOUS flag to use real memory (RAM) is completely different from using SHARED flag !



from Recent Questions - Stack Overflow https://ift.tt/3h4xxrJ
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation