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

using python3 Write a Python program that lets a user search through the 'data.txt' file for...
using python3 Write a Python program that lets a user search through the 'data.txt' file for a given first name, last name, or email address, and print all the matches/results. Prompt the user for which field to search, (f) for first name, (l) for last name, or (e) for email data.txt entries are all formatted as: first_name, last_name, email, separated by TABS Your program should not read the entire file into memory, it should read line by line, and only...
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.
Problem: On an ARM processor using big endian format, given the following memory map and [R1]...
Problem: On an ARM processor using big endian format, given the following memory map and [R1] = 0xA10E0C2D, [R2] = 0x00000060, [R3] = 0x00000002, [R4] = 0x0000000C, predict [R1] and [R2] and draw the updated memory map after an ARM data transfer instruction is executed in EACH case. (hint: (1) in this map, each memory location is a word long and occupies 4 bytes; also you only need to draw the section of the memory including the changed word and...
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...
Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
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...
Please use javascript. Starting with an array containing the numbers 1 through 10, use filter, map...
Please use javascript. Starting with an array containing the numbers 1 through 10, use filter, map and reduce to produce the following. Use console.log to display the results. a. An array of odd numbers b. An array of numbers divisible by 2 or 5 c. An array of numbers divisible by 3 squared d. The sum of the following: square the numbers divisible by 5
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT