asked 137k views
3 votes
I have the following code, but the code is not outputting what i wanted it to output. I need mainly focus on computeOverheadBlocks. the result that the code should output will look like this. Mainly goal is to have the same expected and got value.

Mismatch for 0 : Expected 0, got 1
Mismatch for 1 : Expected 0, got 1
Mismatch for 10 : Expected 0, got 1
Mismatch for 100 : Expected 1, got 2
Mismatch for 1000 : Expected 1, got 2
Mismatch for 10000 : Expected 1, got 2
Mismatch for 15000 : Expected 1, got 2
Mismatch for 16000 : Expected 1, got 2
Mismatch for 16400 : Expected 1, got 3
Mismatch for 16480 : Expected 1, got 3
Mismatch for 16500 : Expected 3, got 4
Mismatch for 17000 : Expected 3, got 4
Mismatch for 20000 : Expected 3, got 4
Mismatch for 30000 : Expected 3, got 4
Mismatch for 40000 : Expected 4, got 6
Mismatch for 100000 : Expected 8, got 14
Mismatch for 1000000 : Expected 63, got 125
Mismatch for 10000000 : Expected 612, got 1241
Mismatch for 30000000 : Expected 1833, got 3721
Mismatch for 33000000 : Expected 2016, got 4092
Mismatch for 33500000 : Expected 2046, got 4153
Mismatch for 33570000 : Expected 2050, got 4162
Mismatch for 33570900 : Expected 2050, got 4163
Mismatch for 33570910 : Expected 2050, got 4163
Mismatch for 33570912 : Expected 2050, got 4163
Mismatch for 33571000 : Expected 2053, got 4164
Mismatch for 33580000 : Expected 2053, got 4164
Mismatch for 33600000 : Expected 2054, got 4166
Mismatch for 34000000 : Expected 2079, got 4216
Mismatch for 40000000 : Expected 2445, got 4960
Mismatch for 50000000 : Expected 3055, got 6199
Mismatch for 100000000 : Expected 6108, got 12398
Mismatch for 1000000000 : Expected 61067, got 123979
Mismatch for 10000000000 : Expected 610652, got 1239777
Mismatch for 20000000000 : Expected 1221302, got 2479554
Mismatch for 30000000000 : Expected 1831951, got 3719330
Mismatch for 40000000000 : Expected 2442601, got 4959107
Mismatch for 50000000000 : Expected 3053250, got 6198883
Mismatch for 60000000000 : Expected 3663900, got 7438660
Mismatch for 68000000000 : Expected 4152419, got 8430481
Mismatch for 68700000000 : Expected 4195165, got 8517266
Mismatch for 68750000000 : Expected 4198217, got -1
Mismatch for 68752000000 : Expected 4198340, got -1
Mismatch for 68753000000 : Expected 4198401, got -1
Mismatch for 68753040000 : Expected 4198403, got -1
Mismatch for 68753047000 : Expected 4198403, got -1
Mismatch for 68753047600 : Expected 4198403, got -1
Mismatch for 68753047648 : Expected 4198403, got -1
//may be the last one
#include
#include
#include
#include
#include
#include
#include
#define SIBLOCKS 2048L
#define DIBLOCKS (2048L*2048L)
#define TIBLOCKS (2048L*2048L*2048L)
#define BLOCK_SIZE 8L*1024L
struct ext4_inode {
int size;
};
long computeOverheadBlocks(long diskblocks) {
long sb_size = BLOCK_SIZE;
long gdt_size = (diskblocks / 8) / 256 * BLOCK_SIZE;
long it_size = (diskblocks / 8) / 4 * sizeof(struct ext4_inode);
// Compute the number of indirect blocks required for the given number of disk blocks
long nindirect = 0;
long nblocks = diskblocks;
while (nblocks > 12) {
nindirect++;
nblocks -= BLOCK_SIZE / sizeof(int);
}
// Compute the size of the indirect block table
long ibt_size = nindirect * BLOCK_SIZE;
// Compute the total number of overhead blocks required
long overhead_blocks = (sb_size + gdt_size + it_size + ibt_size) / BLOCK_SIZE;
// Return the number of overhead blocks
return overhead_blocks;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: diskblocks \\");
return -1;
}
long filesize = atol(argv[1]);
long diskblocks = filesize / BLOCK_SIZE;//i use BLOCK_SIZE insted of 8;
if (filesize % BLOCK_SIZE)
diskblocks++;
// Check if the file size is too large to fit in the file system
if (diskblocks > TIBLOCKS) {
printf("-1\\");
return -1;
}
printf("%ld %ld\\", diskblocks, computeOverheadBlocks(diskblocks));
return 0;
}

1 Answer

2 votes

Final answer:

The computeOverheadBlocks function in your code probably contains a logic error or inaccurate calculations that's causing the program to output mismatched values. Review and revise this function, paying particular attention to the while loop and the calculations for sb_size, gdt_size, and it_size.

Step-by-step explanation:

It looks like your program is outputting mismatched values because the computeOverheadBlocks function isn't calculating the overhead blocks correctly, and because of that, the expected value doesn't equals the got value. You should mainly focus on revising the logic you use to compute the overhead blocks. From a preliminary look at the code, it appears that the while loop for calculating indirect blocks has an off-by-one error that causes it to undercount the number of overhead blocks required.

In addition, the calculations for sb_size, gdt_size, and it_size might not be accurate or might not reflect the actual architecture of the ext4 file system you're trying to model. You might want to double-check the specifications of the ext4 file system and adjust your calculations for these variables accordingly.

Learn more about computeOverheadBlocks

answered
User MrJD
by
8.4k points