Complete the following program in order you work with files mapped in memory.Pro
ID: 3820130 • Letter: C
Question
Complete the following program in order you work with files mapped in memory.Program will map a binary file (people.bin) that has struct Person records (used in program 4) for listing thefile and searching persons in the file.The mapping will be stored in a struct Memory variable the that keeps the pointer to memory and the size of thememory block.Functionality:
void print(const struct Person &p): It will print the person p.
(Implemented)
void mapFile(struct Memory &memo): It will map the file "people.bin" inmemory and it will return the mapping in the by reference parameter memo (ptrand size).
void unmapFile(struct Memory &memo): It will unmap the file from memory.
void list(const struct Memory &memo) : It will print all the people in themapped file that it is in the memo parameter.
bool lookFor(string name, const struct Memory &memo, struct Person &p): Itwill search for a person named name in the mapped file that it is in the memo parameter. If found it will return true and the by reference parameter p will befilled with all the information of the person. If not found it will return false.
/* * File: Program9.cpp * Author: *
* Created on April XX, 2017, hh:mm AM/PM */
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
struct Person {
char name[80];
int age;
char sex; /* M = Male and F = Female */
char lookingFor; /* M = Male and F = Female */
char hobby[80];
};
struct Memory {
void *ptr;
int size;
};
void print(const struct Person &p);
void mapFile(struct Memory &memo);
void unmapFile(struct Memory &memo);
void list(const struct Memory &memo);
bool lookFor(string name, const struct Memory &memo, struct Person &p);
int main(int argc, char** argv) {
struct Memory memo;
struct Person p;
mapFile(memo);
list(memo);
cout << "Looking for Jane ... " << endl;
if (lookFor("Jane",memo, p)) print(p);
else cout << "Jane NOT found" << endl;
cout << endl << "Looking for James ... " << endl;
if (lookFor("James",memo, p)) print(p);
else cout << "James NOT found" << endl;
unmapFile(memo);
return 0;}
void print(const struct Person &p) {
cout << "Name: " << p.name << endl;
cout << "Age: " << p.age << endl;
cout << "Sex: " << p.sex << endl;
cout << "Looking For: " << p.lookingFor << endl;
cout << "Hobby: " << p.hobby << endl;
}void mapFile(struct Memory &memo) {
}
void unmapFile(struct Memory &memo) {
}
void list(const struct Memory &memo) {
}
bool lookFor(string name, const struct Memory &memo, struct Person &p){
return false;}
Execution Example:
Name: John
Age: 20
Sex: M
Looking For: F
Hobby: Basketball
Name: Jane
Age: 21
Sex: F
Looking For: M
Hobby: Basketball
Name: Peter
Age: 22
Sex: M
Looking For: F
Hobby: Football
Name: Mary
Age: 20
Sex: F
Looking For: M
Hobby: Art
Name: Bob
Age: 21
Sex: M
Looking For: F
Hobby: Baseball
Name: Bill
Age: 24
Sex: M
Looking For: F
Hobby: baseball
Looking for Jane ...
Name: Jane
Age: 21
Sex: F
Looking For: M
Hobby: Basketball
Looking for James ...
James NOT found
Explanation / Answer
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class MemoryMappedFileExample
{
static int length = 0x8FFFFFF;
public static void main(String[] args) throws Exception
{
MappedByteBuffer out = new RandomAccessFile("howtodoinjava.dat", "rw")
.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, length);
for (int i = 0; i < length; i++)
{
out.put((byte) 'x');
}
System.out.println("Finished writing");
}
}
public class MemoryMapReader {
public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException {
FileChannel fc = new RandomAccessFile(new File("c:/tmp/mapped.txt"), "rw").getChannel();
long bufferSize=8*1000;
MappedByteBuffer mem = fc.map(FileChannel.MapMode.READ_ONLY, 0, bufferSize);
long oldSize=fc.size();
long currentPos = 0;
long xx=currentPos;
long startTime = System.currentTimeMillis();
long lastValue=-1;
for(;;)
{
while(mem.hasRemaining())
{
lastValue=mem.getLong();
currentPos +=8;
}
if(currentPos < oldSize)
{
xx = xx + mem.position();
mem = fc.map(FileChannel.MapMode.READ_ONLY,xx, bufferSize);
continue;
}
else
{
long end = System.currentTimeMillis();
long tot = end-startTime;
System.out.println(String.format("Last Value Read %s , Time(ms) %s ",lastValue, tot));
System.out.println("Waiting for message");
while(true)
{
long newSize=fc.size();
if(newSize>oldSize)
{
oldSize = newSize;
xx = xx + mem.position();
mem = fc.map(FileChannel.MapMode.READ_ONLY,xx , oldSize-xx);
System.out.println("Got some data");
break;
}
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.