기본 콘텐츠로 건너뛰기

Memory Map

Memory-map은 파일내용을 메모리와 동기화하는 것이다. 예를 들어 mmap.txt에 'Hello, World!'를 저장하면 mmap.txt를 연결한 프로세스의 메모리에 해당 내용이 바뀐다.

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <iostream>
using namespace std;

size_t memsize = 30;

int
main(int,char**)
{
    int fd = open("mmap.txt", O_RDWR|O_CREAT);
    if (0 > fd)
    {
        cerr << "failed to create or open file" << endl;
        return 1;
    }

    char* fp(NULL);

    fp = (char*)mmap(0, memsize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    if ( MAP_FAILED == fp )
    {
        cerr << "failed to map memory" << endl;
        return 1;
    }

    cerr << "fp: " << (void*)fp << endl;
    cerr << "write" << endl;

    sprintf(fp, "Hello, world!");

    cerr << "all done." << endl;

    return 0;
}

이렇게 하면 파일에 ;Hello, World'가 들어간다. 그 반대도 마찬가지다.

댓글

이 블로그의 인기 게시물

탐색기에서 OneDrive 이 2개로 보이는 문제

왜 2개가 보이는지 모르겠지만, Registry 삭제하면 됨 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace 하위 키에서 OneDrive 둘 중에 하나만 지워도 바로 반영됨. 참조:  https://answers.microsoft.com/en-us/msoffice/forum/all/duplicate-onedrives-in-file-explorer/49c935a6-287b-43a5-aed5-2dee2a1c1b22