어떻게 부스트 메모리 매핑 파일이 작동하는지 이해하려고합니다. 다음 코드는 작동하지만 수행 할 작업을 수행하지만 문제는 생성되는 파일이 메모리 대신 실행 파일의 같은 디렉터리에 디스크에 저장된다는 것입니다. 아마 어딘가에 설정할 수있는 깃발이 있지만, 나는 그것을 찾을 수 없습니다 ...
미리 정보를 주셔서 감사합니다!메모리가 매핑 된 파일을 부스트하는 문제 : RAM 대신 디스크로 이동합니다.
#include <iostream>
#include <string>
#include <cstring>
#include <boost/iostreams/device/mapped_file.hpp>
using std::cout;
using std::endl;
int main(int argc, char** argv) {
const int blockSize = 64;
bool writer = false;
if(argc > 1) {
if(!strcmp(argv[1], "w"))
writer = true;
}
boost::iostreams::mapped_file_params params;
params.path = "map.dat";
// params.length = 1024; // default: all the file
params.new_file_size = blockSize;
if(writer) {
cout << "Writer" << endl;
params.mode = std::ios_base::out;
}
else {
cout << "Reader" << endl;
params.mode = std::ios_base::in;
}
boost::iostreams::mapped_file mf;
mf.open(params);
if(writer)
{
char *block = mf.data();
strcpy(block, "Test data block...\0");
cout << "Written: " << block << endl;
}
else
{
cout << "Reading: " << mf.const_data() << endl;
}
mf.close();
return 0;
}
/*
Compiler options: -Wall -I$(PATH_BOOST_INCLUDE) -ggdb
Linker options: -L$(PATH_BOOST_LIBS) -lboost_iostreams-mt -lboost_system-mt -lboost_filesystem-mt -DBOOST_FILESYSTEM_NO_DEPRECATED
*/
은 컴파일러 사용 : GCC 4.2.1
부스트 1.41.0
OS : 메모리에 맥 OS X 10.6.2
미안하지만 MAP_ANONYMOUS 플래그가있는 mmap은 파일 시스템 객체를 필요로하지 않습니다. –
MAP_ANONYMOUS 플래그는/dev/zero를 매핑하여 얻은 것과 동일한 매핑을 제공합니다. 이것은 매우 특별한 몇 가지 상황에서 사용될 수 있지만 그 질문에 관한 것이 아닙니다. –
나는 mmap을 사용하지 않고 boost :: iostreams :: mapped_file을 사용하고있다. 그것이 내 문제와 관련이 있습니다. – Pietro