기본 콘텐츠로 건너뛰기

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'가 들어간다. 그 반대도 마찬가지다.

댓글

이 블로그의 인기 게시물

설치한 패키지에서 RPM 추출하기

오래된 패키지를 관리할 저장소가 없어졌고, 기존 패키지로 다른 서버를 세팅해야할 일이 생겼다면 RPM의 리패키지 기능을 이용해보자. $ rpm -e --repackage [PACKAGE_NAME] 위와 같이 리패키지하면, /var/spool/repackage/ 에 생성한 RPM파일이 있다. :-)

Winget 해시 무시하기

가끔씩 Winget 에서 패키지를 다운로드 했을 때, "설치 관리자 해시가 일치하지 않습니다." 오류가 뜰 때가 있다. 보안 이슈가 있지만, 그냥 무시하고 싶을 때, 아래 순서로 무시해준다. 관리자 권한 winget settings --enable InstallerHashOverride 설치 winget install --ignore-security-hash --id NirSoft.NirCmd