CS2106 Tutorial 8
Q1. Compare the relative advantages and disadvantages of inodes versus file allocation tables.
- inode - stores metadata about a file including TOC.
- FAT - Global TOC.
inode
+ Direct access for single file as needed
–– Requires disk access to load the inode
++ Fast access for random block
++ Support sparse files (holes in file)
– inode table size limits number of unique files
– File size limited by inode TOC size
FAT
++ Cached in memory, fast access
– Requires linked list traversal (though it's in memory)
+ File size only limited by FAT size
– FAT size limits number of blocks
Refer to lecture slide 54
| 10 |
| NULL |
| 27 |
| NULL |
| NULL |
| 29 |
| 81 |
| NULL |
| ...... |
inode
+ Direct access for single file as needed
–– Requires disk access to load the inode
++ Fast access for random block
++ Support sparse files (holes in file)
– inode table size limits number of unique files
– File size limited by inode TOC size
FAT
++ Cached in memory, fast access
– Requires linked list traversal (though it's in memory)
+ File size only limited by FAT size
– FAT size limits number of blocks
???
???
Which one supports larger file?
- In a Unix implementation where inode TOC has 13 entries, with 10 direct pointer, 1 single/double/triple indirection pointer each, and every index block contains 128 pointers. Further assume that the block size is 8KB.
- File sizes in most of the FAT implementations are limited by other factors.
- FAT12 supports up to 32MB volumes.
- FAT16 supports up to 2GB volumes.
- FAT32 supports up to 4GB minus 1 byte file size (why?)
Q2. Operation with inode
int fseek(FILE *stream, long int offset, int whence)
/* seek to the position of whence + offset.
whence can be
SEEK_SET (beginning of file),
SEEK_CUR (current pos),
SEEK_END (end of file). */
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
/* read nmemb number of elements, each with size `size` */
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
/* similar to fread */| TOC | Range |
|---|---|
| 15 | 0-2047 |
| 18 | 2048-4095 |
| 12 | 4096-6143 |
| 22 | 6144-8191 |
| Op | Offset | Block number |
|---|---|---|
| fseek(fp, 1121, SEEK_SET) | 1121 | (15) |
| fwrite(buff, sizeof(char), 128, fp) | 1121+128 = 1249 | 15 |
| fseek(fp, 5523, SEEK_SET) | 5523 | (12) |
| fread(buff, sizeof(char), 256, fp) | 5523+256 = 5779 | 12 |
| fseek(fp, 8121, SEEK_SET) | 8121 | (22) |
| fwrite(buff, sizeof(int), 25, fp) | 8121+ 100 = 8221 | 22 + ??? |
Bracket means pointer is at that block
but nothing read / written
| Op | Offset | Block number |
|---|---|---|
| fseek(fp, 1121, SEEK_SET) | ||
| fwrite(buff, sizeof(char), 128, fp) | ||
| fseek(fp, 5523, SEEK_SET) | ||
| fread(buff, sizeof(char), 256, fp) | ||
| fseek(fp, 8121, SEEK_SET) | ||
| fwrite(buff, sizeof(int), 25, fp) |
Q3. More operations...
| Block # | First Byte |
|---|---|
| 0 | 0 |
| 1 | 2048 |
| 2 | 4096 |
| 3 | 6144 |
| 4 | 8192 |
| 5 | 10240 |
| 6 | 12288 |
| 7 | ... |
fwrite(buffer, sizeof(int), 1024, fp);
// sizeof(int) = 4 bytes
fseek(fp, 8897, SEEK_SET);
fwrite(buffer, sizeof(char), 1124, fp);
fseek(fp, 3072, SEEK_SET);
fread(buffer, sizeof(char), 1024, fp);1 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0
1 1 1 1 1 1 0 1 0 0 0 0 0 0 0 0
| 1 |
| 4 |
| NULL |
| NULL |
| 5 |
| NULL |
Buffer contains the data in block 4
Copy of Copy of CS2106 Tutorial 8
By Chen Minqi
Copy of Copy of CS2106 Tutorial 8
- 678