Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat settings in my mmap code sets all the files data to zero?
    primarykey
    data
    text
    <p>I am implementing one half of a stream where you should be able to jump X integers back or forth. On top of that when I open a file it should preserve the data in the file. In this implementation I try to use mmap. My problem is when I open a file a second time the mapped data is purely zeroes. My open method is called createStream and looks like this:</p> <pre><code>void MapperOutForward::createStream(const char* filename) { this-&gt;filename = filename; pFile = open(filename, O_RDWR | O_CREAT | O_SYNC, S_IRWXG|S_IRWXO|S_IRWXU); if ( pFile == -1) { int errsv = errno; cout &lt;&lt; "MappedStreamOut createStream open error: " &lt;&lt; strerror( errno ) &lt;&lt; endl; } //getting file size and making the file big enough struct stat filestatus; stat( filename, &amp;filestatus ); if(filestatus.st_size &lt; mapSize + 1) { lseek (pFile, mapSize + 1, SEEK_SET); write (pFile, "", 1); } lseek (pFile, 0, SEEK_SET); map = (int*) mmap (0, mapSize, PROT_WRITE | PROT_READ, MAP_PRIVATE, pFile, 0); close (pFile); if ( map == (int*) -1) { cout &lt;&lt; "MappedStreamOut createStream mmap error: " &lt;&lt; strerror(errno) &lt;&lt; endl; } numberOfOffsets = 1; numberOfElementsInMap = 0; cout &lt;&lt; "Kopirer 5 før og 4 efter i Create" &lt;&lt; endl; for(int i = 0; i &lt; numberOfElementsInMap + 5; i++) { cout &lt;&lt; map[i] &lt;&lt; endl; } cout &lt;&lt; "Create numberOfOffsets:numberOfElementsInMap " &lt;&lt; numberOfOffsets &lt;&lt; ":" &lt;&lt; numberOfElementsInMap &lt;&lt; endl; } </code></pre> <p>The output at the end tells me that when I am done opening I am at the beginning of the file and the first 5 integers are 0. </p> <p>The O_CREAT flag in the open command should only create a file if it is not existing, and I can se that the file keeps its size after I call this command a second time. <a href="http://linux.die.net/man/2/open" rel="nofollow">man open</a></p> <p>The PROT_WRITE | PROT_READ flags in the mmap command seems only to activate some security and not changing anything in the file. <a href="http://linux.die.net/man/2/mmap" rel="nofollow">man mmap</a></p> <p>The rest of the class is here:</p> <pre><code>#include "MapperOutForward.h" using namespace std; #include &lt;cstdlib&gt; #include &lt;iostream&gt; #include &lt;fcntl.h&gt; #include &lt;sys/stat.h&gt; #include &lt;cmath&gt; MapperOutForward::MapperOutForward(int mapSize) //number of pages { pageSize = getpagesize(); this-&gt;mapSize = mapSize * pageSize; //we work in pages numberOfElementsInMap = 0; numberOfOffsets = 0; } MapperOutForward::~MapperOutForward() { munmap(map, mapSize); } void MapperOutForward::createStream(const char* filename) { this-&gt;filename = filename; pFile = open(filename, O_RDWR | O_CREAT | O_SYNC, S_IRWXG|S_IRWXO|S_IRWXU); if ( pFile == -1) { int errsv = errno; cout &lt;&lt; "MappedStreamOut createStream open error: " &lt;&lt; strerror( errno ) &lt;&lt; endl; } //getting file size and making the file big enough struct stat filestatus; stat( filename, &amp;filestatus ); if(filestatus.st_size &lt; mapSize + 1) { lseek (pFile, mapSize + 1, SEEK_SET); write (pFile, "", 1); } lseek (pFile, 0, SEEK_SET); map = (int*) mmap (0, mapSize, PROT_WRITE | PROT_READ, MAP_PRIVATE, pFile, 0); close (pFile); if ( map == (int*) -1) { cout &lt;&lt; "MappedStreamOut createStream mmap error: " &lt;&lt; strerror(errno) &lt;&lt; endl; } numberOfOffsets = 1; numberOfElementsInMap = 0; cout &lt;&lt; "Kopirer 5 før og 4 efter i Create" &lt;&lt; endl; for(int i = 0; i &lt; numberOfElementsInMap + 5; i++) { cout &lt;&lt; map[i] &lt;&lt; endl; } cout &lt;&lt; "Create numberOfOffsets:numberOfElementsInMap " &lt;&lt; numberOfOffsets &lt;&lt; ":" &lt;&lt; numberOfElementsInMap &lt;&lt; endl; } void MapperOutForward::writeNext(int* data, int numberOfElements) { for(int i = 0; i &lt; numberOfElements; i++) { writeHelper(data[i]); } cout &lt;&lt; "Kopirer 5 før og 4 efter i writeNext" &lt;&lt; endl; for(int i = numberOfElementsInMap - 5; i &lt; numberOfElementsInMap + 5; i++) { cout &lt;&lt; map[i] &lt;&lt; endl; } //cout &lt;&lt; "WriteNext numberOfOffsets:numberOfElementsInMap " &lt;&lt; numberOfOffsets &lt;&lt; ":" &lt;&lt; numberOfElementsInMap &lt;&lt; endl; } void MapperOutForward::writeHelper(int data) { int sizeOfInt = 4; //bytes if(numberOfElementsInMap &gt;= mapSize / sizeOfInt) //We need to create the next part of the file { munmap(map, mapSize); pFile = open(filename, O_RDWR | O_CREAT | O_SYNC, S_IRWXG|S_IRWXO|S_IRWXU); if ( pFile == -1) { std::cout&lt;&lt;"filename: "&lt;&lt;filename&lt;&lt;std::endl; std::cout&lt;&lt;"mapSize: "&lt;&lt;mapSize&lt;&lt;std::endl; int errsv = errno; cout &lt;&lt; "MappedStreamOut writeNext open error: " &lt;&lt; strerror( errno ) &lt;&lt; endl; } numberOfOffsets++; //Make the file bigger lseek (pFile, mapSize * numberOfOffsets, SEEK_SET); write (pFile, "", 1); lseek (pFile, 0, SEEK_SET); map = (int*) mmap (0, mapSize, PROT_WRITE | PROT_READ, MAP_PRIVATE, pFile, mapSize * (numberOfOffsets - 1)); close (pFile); if ( map == (int*) -1) { cout &lt;&lt; "MappedStreamOut writeNext mmap error: " &lt;&lt; strerror(errno) &lt;&lt; endl; } numberOfElementsInMap = 0; } map[numberOfElementsInMap] = data; numberOfElementsInMap++; } void MapperOutForward::jumpTo(int offset) { int sizeOfInt = 4; //bytes //cout &lt;&lt; "numberOfElementsInMap + offset " &lt;&lt; numberOfElementsInMap + offset &lt;&lt; endl; //cout &lt;&lt; "mapSize / sizeOfInt " &lt;&lt; mapSize / sizeOfInt &lt;&lt; endl; if(numberOfElementsInMap + offset &gt;= mapSize / sizeOfInt) { munmap(map, mapSize); pFile = open(filename, O_RDWR | O_CREAT | O_SYNC, S_IRWXG|S_IRWXO|S_IRWXU); if ( pFile == -1) { std::cout&lt;&lt;"filename: "&lt;&lt;filename&lt;&lt;std::endl; std::cout&lt;&lt;"mapSize: "&lt;&lt;mapSize&lt;&lt;std::endl; int errsv = errno; cout &lt;&lt; "MappedStreamOut writeNext open error: " &lt;&lt; strerror( errno ) &lt;&lt; endl; } numberOfOffsets += (int) ceil( (double) (numberOfElementsInMap + offset - (mapSize / sizeOfInt)) / (mapSize / sizeOfInt)); // rundet op ((resterende antal ints ud over mapSize) / (Antal ints i en mapsize)) = Antal offsets der skal springes //Make the file bigger lseek (pFile, mapSize * numberOfOffsets, SEEK_SET); write (pFile, "", 1); lseek (pFile, 0, SEEK_SET); map = (int*) mmap (0, mapSize, PROT_WRITE | PROT_READ, MAP_PRIVATE, pFile, mapSize * (numberOfOffsets - 1)); close (pFile); if ( map == (int*) -1) { cout &lt;&lt; "MappedStreamOut writeNext mmap error: " &lt;&lt; strerror(errno) &lt;&lt; endl; } numberOfElementsInMap = (numberOfElementsInMap + offset - (mapSize / sizeOfInt)) % (mapSize / sizeOfInt); //(resterende antal ints ud over mapSize) % (Antal ints i en mapsize) = Antal elementer ind i offsettet //cout &lt;&lt; "JumpTo offsetForward numberOfOffsets:numberOfElementsInMap " &lt;&lt; numberOfOffsets &lt;&lt; ":" &lt;&lt; numberOfElementsInMap &lt;&lt; endl; } else if(numberOfElementsInMap + offset &lt; 0) { munmap(map, mapSize); pFile = open(filename, O_RDWR | O_CREAT | O_SYNC, S_IRWXG|S_IRWXO|S_IRWXU); if ( pFile == -1) { std::cout&lt;&lt;"filename: "&lt;&lt;filename&lt;&lt;std::endl; std::cout&lt;&lt;"mapSize: "&lt;&lt;mapSize&lt;&lt;std::endl; int errsv = errno; cout &lt;&lt; "MappedStreamOut writeNext open error: " &lt;&lt; strerror( errno ) &lt;&lt; endl; } numberOfOffsets += (int) floor( (double) (numberOfElementsInMap + offset) / (mapSize / sizeOfInt)); // rundet op ((resterende antal ints ud over mapSize) / (Antal ints i en mapsize)) = Antal offsets der skal springes //Make the file bigger lseek (pFile, mapSize * numberOfOffsets, SEEK_SET); write (pFile, "", 1); lseek (pFile, 0, SEEK_SET); map = (int*) mmap (0, mapSize, PROT_WRITE | PROT_READ, MAP_PRIVATE, pFile, mapSize * (numberOfOffsets - 1)); close (pFile); if ( map == (int*) -1) { cout &lt;&lt; "MappedStreamOut writeNext mmap error: " &lt;&lt; strerror(errno) &lt;&lt; endl; } numberOfElementsInMap = (mapSize + (numberOfElementsInMap + offset)) % (mapSize / sizeOfInt); //(resterende antal ints ud over mapSize) % (Antal ints i en mapsize) = Antal elementer ind i offsettet //cout &lt;&lt; "JumpTo offsetBacward numberOfOffsets:numberOfElementsInMap " &lt;&lt; numberOfOffsets &lt;&lt; ":" &lt;&lt; numberOfElementsInMap &lt;&lt; endl; } else { numberOfElementsInMap += offset; //cout &lt;&lt; "JumpTo else numberOfOffsets:numberOfElementsInMap " &lt;&lt; numberOfOffsets &lt;&lt; ":" &lt;&lt; numberOfElementsInMap &lt;&lt; endl; } } void MapperOutForward::closeStream() { munmap(map, mapSize); } </code></pre> <p>And the main file is here:</p> <pre><code>#include "MapperInForward.h" #include "MapperOutForward.h" //#include "MapperOutBackward.h" //#include "MapperInBackward.h" //#include "MapperStreamFactory.h" #include &lt;functional&gt; int main( int argc, const char* argv[] ) { /*********************Tester Buffer Stream*************************************/ close(open((char*) "mapperTest", O_WRONLY | O_CREAT | O_SYNC | O_TRUNC, S_IRWXG|S_IRWXO|S_IRWXU)); //BufferStreamFactory* bsf = new BufferStreamFactory(3); MapperOutForward* mso = new MapperOutForward(1); mso-&gt;createStream((char*) "mapperTest"); int data[4000]; for(int i = 0; i &lt; 4000; i++) { data[i] = i + 1; } mso-&gt;writeNext(data, 4000); mso-&gt;closeStream(); MapperOutForward* mso1 = new MapperOutForward(1); mso1-&gt;createStream((char*) "mapperTest"); data[0] = 9999; mso1-&gt;jumpTo(0); mso1-&gt;jumpTo(100); mso1-&gt;writeNext(data, 1); mso1-&gt;jumpTo(1000); mso1-&gt;writeNext(data, 1); mso1-&gt;jumpTo(-900); mso1-&gt;writeNext(data, 1); mso1-&gt;closeStream(); } </code></pre> <p>It is all running on Ubuntu Linux and compiled with g++.</p> <p>Thanks for the time used to read all this and I hope you can help me. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload