Question

In: Computer Science

Using C, use mmap() to map a file to memory, and then search through it. For...

Using C, use mmap() to map a file to memory, and then search through it.

For example, get and print out the first character of each line in the file:

abc

def

ghi

Solutions

Expert Solution

Hi,

mmap as a way of mapping physical memory to address space. But mmap is known for its ability to map files to the address space also.

Here is the code for above-mentioned question. I commented in between for better understanding.

#include <stdio.h>

#include <sys/mman.h>

#include <unistd.h>

#include <fcntl.h>

#include <string.h>

int main(void) {
int fd = open("words", O_RDONLY);
size_t pagesize = getpagesize();

//mapping the file named words to the memory using mmap
char * region = mmap(
    (void * )(pagesize * (1 << 20)), pagesize,
    PROT_READ, MAP_FILE | MAP_PRIVATE,
    fd, 0
);

//printing first charecter

if ( * region != '\0') {

printf("%c", * region++);

}

//loop to print first charecter of every line
while ( * region != '\0') {
    if ( * region == '\n') {
      printf("\n%c", *++region);
    }
    * region++;
}
//fwrite(region, 1, pagesize, stdout);
int unmap_result = munmap(region, pagesize);
close(fd);
return 0;
}


Related Solutions

Compare and Contrast Hash Map to Binary Search Tree. Memory, Time Efficiency for Operations for Retrieving...
Compare and Contrast Hash Map to Binary Search Tree. Memory, Time Efficiency for Operations for Retrieving Singular Values, Maintaining relationships between data. Please be sure to use your own words.
Create C# code that can search a text file and output the data at the line...
Create C# code that can search a text file and output the data at the line number inputted and amount of entries needed. Example of call in command window: Search16s filename.txt 273   10 Where 273 is the line number to start the output from, and 10 is the number of sequences that the program should output. The number of sequences entered on call should always be a odd number or give an error in console. The output should also display...
Write a C program that can search for a string within a text file, replace it...
Write a C program that can search for a string within a text file, replace it with another string and put results in a new file.
Create a C program that solves the word search puzzle contained in the file 'WordSearchPuzzle.txt'. The...
Create a C program that solves the word search puzzle contained in the file 'WordSearchPuzzle.txt'. The words that need to be found are in the file 'WordList.txt'. There are 100 words to be found. You must find the location of the first and last letter of each word as well as the cardinal direction that describes the word's orientation. The location of first and last letters are to be described in terms of row and column, with indexing starting at...
Exercise 1 Create a data file in SPSS using the following memory scores for this sample...
Exercise 1 Create a data file in SPSS using the following memory scores for this sample of 15 fictional patients with dementia: 8 3 11 7 6 5 4 8 9 8 7 9 5 4 10 Higher memory number indicates better memory performance. Use this information to complete the following in SPSS. 17) Run descriptive statistics on the dementia patients’ memory scores (make sure to include the mean and confidence interval by using the “Explore” option as shown in...
In C++ Consider the binary search tree implementation in file bintree.cp a)Add to the TreeNode class...
In C++ Consider the binary search tree implementation in file bintree.cp a)Add to the TreeNode class a pointer to the parent node. Modify the insert and erase functions to properly set those pointers. b)Then define a TreeIterator class that contains a pointer to a TreeNode and has member functions get and next. i)The iterator’s get member function should return the data value of the node to which it points. ii)The iterator’s next member function should find the next element in...
C++ Write a word search program that searches an input data file for a word specified...
C++ Write a word search program that searches an input data file for a word specified by the user. The program should display the number of times the word appears in the input data file. In addition, the program should count and display the number of grammatical characters in the input data file. Your program must do this by providing the following function: void processFile(ifstream &inFile, string wordSearch, int &wordCount, int &grammaticalCount); void processFile(ifstream &inFile, string wordSearch, int &wordCount, int...
C++ sort a file with 140 integers where only 20 integers maybe placed into memory at...
C++ sort a file with 140 integers where only 20 integers maybe placed into memory at any one time Main idea: break data into blocks, sort blocks into runs, merge runs less. Call inFile1 our source file (the one with the initial 140 records to be sorted. You will also need an inFile2, and 2 other files outFile1 and outFile2. Break the file into blocks of size 20: in this case there will be 7 blocks ( 140/20 ) Sort...
Use your favorite search engine and search for “world’s greatest data breaches and hacks.” Scan through...
Use your favorite search engine and search for “world’s greatest data breaches and hacks.” Scan through the hits until you find visual diagrams or a text-based list of major data breaches that have occurred recently. (Major data breaches are defined as those in excess of 30,000 records.) Select and carefully review at least two of these data breaches. Briefly describe the two data breaches you selected. Explain in layman’s terms how you think these breaches occurred. Discuss whether or not...
How to read and print all contents in a binary file using a Binary Search Tree...
How to read and print all contents in a binary file using a Binary Search Tree inorder traversal in C. Additionally, how to search using a Binary Search Tree to display the specific Athlete and his/her information. An example would be a sports record binary file that consist of name of athlete, height , weight, championships won. Athlete: Michael Jordan Height: 6’6” Weight : 205 lbs Championships won: 6 =================== Athlete: LeBron James Height: 6’8” Weight: 250 lbs Championships won:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT