Presentation is loading. Please wait.

Presentation is loading. Please wait.

File Systems. It is a simple C program that prints ``hello world'' and then exits. The header describes it as an ELF image with two physical headers (e_phnum.

Similar presentations


Presentation on theme: "File Systems. It is a simple C program that prints ``hello world'' and then exits. The header describes it as an ELF image with two physical headers (e_phnum."— Presentation transcript:

1 File Systems

2 It is a simple C program that prints ``hello world'' and then exits. The header describes it as an ELF image with two physical headers (e_phnum is 2) starting 52 bytes (e_phoff) from the start of the image file. The first physical header describes the executable code in the image. It goes at virtual address 0x8048000 and there is 65532 bytes of it. This is because it is a statically linked image which contains all of the library code for the printf() call to output ``hello world''. The entry point for the image, the first instruction for the program, is not at the start of the image but at virtual address 0x8048090 (e_entry). The code starts immediately after the second physical header. This physical header describes the data for the program and is to be loaded into virtual memory at address 0x8059BB8. This data is both readable and writeable. You will notice that the size of the data in the file is 2200 bytes (p_filesz) whereas its size in memory is 4248 bytes. This because the first 2200 bytes contain pre-initialized data and the next 2048 bytes contain data that will be initialized by the executing code. Linux Executable Binary File Executable File on Disk

3 It's important to note that PE files are not just mapped into memory as a single memory- mapped file. Instead, the Windows loader looks at the PE file and decides what portions of the file to map in. This mapping is consistent in that higher offsets in the file correspond to higher memory addresses when mapped into memory. The offset of an item in the disk file may differ from its offset once loaded into memory. However, all the information is present to allow you to make the translation from disk offset to memory offset (see Figure 1). Windows Executable Binary File Portable Executable File on Disk Process Running Executable In Memory

4 int binarySearch(int size, int value) { // lower bound index of the interval that may contain value int lower=0; // upper bound index of interval that may contain value int upper=size-1; // middle of interval int middle=0; // Search array using smaller and smaller index intervals until // - the value is found // - the interval is smaller than zero // Each iteration of the while loop, halve the interval while (lower <= upper) { // The middle of the interval is halfway between the lower and // upper bounds of the interval. middle = (lower + upper) / 2; // Get the middle value from file int theMiddleValue = getValueFromBinFileRandom(middle); // Return the array index if value found if (value == theMiddleValue) { cout << "Found " << endl; return middle; } // If value is less than middle, then shift the upper bound // of the interval to the left. else if (value < theMiddleValue) { upper = middle -1; } // If value is greater than middle, then shift the lower bound // of the interval to the right. else if (value > theMiddleValue) { lower = middle + 1; } // If we get here, then the array doesn't contain the value. // Return index of -1. cout << "Not Found" << endl; return -1; } int getValueFromBinFileRandom(int index) { int result; char *byteView = (char *) &result; ifstream infile; infile.open("sortedarray.bin"); infile.seekg(index*sizeof(int),ios::beg); infile.get(byteView[0]); infile.get(byteView[1]); infile.get(byteView[2]); infile.get(byteView[3]); infile.close(); return result; } 5ms per search int getValueFromBinFileSequential(int index) { int result; char *byteView = (char *) &result; ifstream infile; infile.open("sortedarray.bin"); for (int i=0; i<index; i++) { infile.get(byteView[0]); infile.get(byteView[1]); infile.get(byteView[2]); infile.get(byteView[3]); } infile.close(); return result; } 7500 ms per search

5 File Attributes

6 Accessing mode bits in st_mode requires two different masks. S_IFMT for the file type information and S_IAMB for the access mode. if ((info.st_mode & S_IFMT) == S_IFDIR) { cout << “Directory!” << endl; } else { cout << “Not a Directory!” << endl; } Members of stat data structure from stat() Decoding the st_mode member

7 int binarySearch(int array[], int size, int value) { // lower bound index of the interval that may contain value int lower=0; // upper bound index of interval that may contain value int upper=size-1; // middle of interval int middle=0; // Search array using smaller and smaller index intervals until // - the value is found // - the interval is smaller than zero // Each iteration of the while loop, halve the interval while (lower <= upper) { // The middle of the interval is halfway between the lower and // upper bounds of the interval. middle = (lower + upper) / 2; // Get the middle value from file int theMiddleValue = array[middle]; // Return the array index if value found if (value == theMiddleValue) { cout << "Found " << endl; return middle; } // If value is less than middle, then shift the upper bound // of the interval to the left. else if (value < theMiddleValue) { upper = middle -1; } // If value is greater than middle, then shift the lower bound // of the interval to the right. else if (value > theMiddleValue) { lower = middle + 1; } // If we get here, then the array doesn't contain the value. // Return index of -1. cout << "Not Found" << endl; return -1; } int main() { int size=MAX_SIZE; int sum = 0; // // HOW TO MEMORY MAP A FILE // // STEP ONE: Open the file using primitive open system call // int fd = open("sortedarray.bin",O_RDONLY); // // STEP TWO: Use mmap() system call to map file to an array // int* array = (int *) mmap(0,MAX_SIZE*sizeof(int), PROT_READ, MAP_SHARED, fd, 0); // // STEP THREE: Use the array normally // cout << "First file entry is: " << array[0] << endl; cout << "Performing search test..." << endl; for (int i=0; i<100; i++) { int start = clock(); int value = rand() % MAX_SIZE; binarySearch(array,size,value); int stop = clock(); sum = sum + stop-start; } cout << "Search array of size " << size << " took " << sum/100.0 << " ms" << endl; // // STEP FOUR: Unmap the file and close // munmap(array,MAX_SIZE*sizeof(int)); close(fd); return 0; } Memory Mapped File Example 0.04ms per search

8 Disk Drive Anatomy

9 Disk Platter, 7 Tracks, 25 Sectors/Track

10 Cylinder Skew

11 Files: Contiguous Allocation

12 Directories: Storage

13 Block Size: Tradeoff between Space and Time for 2KB Files

14 Free Block Management: Linked List vs. Bitmap


Download ppt "File Systems. It is a simple C program that prints ``hello world'' and then exits. The header describes it as an ELF image with two physical headers (e_phnum."

Similar presentations


Ads by Google