14
File Systems

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

Embed Size (px)

Citation preview

Page 1: 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

File Systems

Page 2: 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

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

ExecutableFile on Disk

Page 3: 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

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 ExecutableFile on Disk

Process Running Executable InMemory

Page 4: 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

int binarySearch(int size, int value){

// lower bound index of the interval that may contain valueint lower=0;

// upper bound index of interval that may contain valueint upper=size-1;

// middle of intervalint 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 intervalwhile (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 fileint theMiddleValue = getValueFromBinFileRandom(middle);

 // Return the array index if value foundif (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

Page 5: 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

File Attributes

Page 6: 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

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

Page 7: 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

int binarySearch(int array[], int size, int value){

// lower bound index of the interval that may contain valueint lower=0;

// upper bound index of interval that may contain valueint upper=size-1;

// middle of intervalint 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 intervalwhile (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 fileint theMiddleValue = array[middle];

 // Return the array index if value foundif (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

Page 8: 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

Disk Drive Anatomy

Page 9: 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

Disk Platter, 7 Tracks, 25 Sectors/Track

Page 10: 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

Cylinder Skew

Page 11: 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

Files: Contiguous Allocation

Page 12: 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

Directories: Storage

Page 13: 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

Block Size: Tradeoff between Space and Timefor 2KB Files

Page 14: 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

Free Block Management: Linked List vs. Bitmap