블로그 이미지

하얀현자의 Sweet Whisper!

하양이가 하고 있는, 살고 있는- 소소한 하루의 여정。 by 하얀현자


'MMAP'에 해당되는 글 1건

  1. 2010.01.08 MMAP을 사용해보자!3

MMAP을 사용해보자!

MMAP

What is MMAP?

  • MMAP is Memory Mapped IO, It enables accessing file like memory read or write.
    • MMAP은 Memory Mapped IO 로써, 파일에 대한 접근을 메모리에 접근하는 것과 같이 만들어 준다.
  • Therefore it has benefit in IO throughput which has huge overload of each access.
    • 따라서, 이는 각각의 Access 마다 가지고 있는 큰 Overload를 지닌 IO Throughput 에 있어서 이익을 얻을 수 있다.

Implementation of MMAP

Environment under Windows

  • Windows support MMAP form of function CreateFileMapping. This is quite different from UNIX implementation.
    • Windows 에서는 MMAP을 CreateFileMapping 이라는 함수로써 지원한다. 이는 UNIX에의 구현과 다소 상이한 점이다.
  • But, basically it’s same with UNIX one.
    • 하지만, 기본적으로는 UNIX의 그것과 일맥상통한다.

Simple Source Code

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
void main(){
 
	// Simple Source Code Using MMAP in Win32 Environment
	// 2008.01.08 Written by snowsage
 
	HANDLE hFile = CreateFile(TEXT("\\tmp.txt"), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
	if(hFile == NULL){ printf("<ERROR> CreateFile\n"); return; }
 
	HANDLE fd = CreateFileMapping( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
	if(fd == NULL){ printf("<ERROR> CreateFileMapping\n"); return; }
 
	char* mapFile = (char*)MapViewOfFile(fd, FILE_MAP_READ, 0, 0, 0);
	printf("%s", mapFile);
}

Explanation of Source Code

  1. windows.h has definitions of function which is related to MMAP. So we have to include the file.
    • windows.h 파일이 MMAP과 관련한 함수들을 정의하고 있으므로 이 파일을 Include 해주어야 한다.
  2. First, Create file handler to your file(CreateFile). You have to carefull with access options! It has to equivalent with using in CreateFileMapping function.
    • 먼저 파일에 대한 Handler를 얻어온다(CreateFile). 이때 Access 권한을 설정을 주의하여야한다. 이것은 CreateFileMapping Function 에서 사용 되는 것과 같은 값이어야한다.
  3. Create mapping handle using opened file handler(hFile). Actually, It is real step to map file.
    • Mapping handle 을 file handler(hFile)로 부터 생성한다. 사실 이 과정이 실제 파일로 매핑을 하는 과정이다.
  4. Get pointer from mapped handle(fd). MapViewOfFile function will be return VOID pointer, so we need to type cast VOID to CHAR pointer.
    • Mapping된 handle로부터 포인터값을 얻어온다. MapViewOfFile 함수는 void 포인터로 반환하기 때문에 이에 대해 char*로 TypeCast가 필요하다.

- 원본 글 위치: http://kweb.korea.ac.kr/doku.php?id=kdj-mmap

Top

prev 1 next